Refinements related to a committed response

1. setComplete checks the isCommitted flag to avoid an unnecessary
debug message. This method is meant to be safe to call many times.

2. setStatusCode lowers log message to TRACE, since the return value
communicates the outcome it's arguably much less critical.

3. Add comment and test case for ResponseStatusExceptionHandler.
A ResponseStatusException is clearly meant to be handled by this
handler so don't let it pass through even if the respones is
committed.

Issue: SPR-16231
This commit is contained in:
Rossen Stoyanchev
2017-11-27 16:31:49 -05:00
parent e30f1fbe89
commit dc3d834026
3 changed files with 18 additions and 8 deletions

View File

@@ -89,9 +89,9 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
@Override
public boolean setStatusCode(@Nullable HttpStatus statusCode) {
if (this.state.get() == State.COMMITTED) {
if (logger.isDebugEnabled()) {
logger.debug("Can't set the status " + (statusCode != null ? statusCode.toString() : "null") +
" because the HTTP response has already been committed");
if (logger.isTraceEnabled()) {
logger.trace("HTTP response already committed. " +
"Status not set to " + (statusCode != null ? statusCode.toString() : "null"));
}
return false;
}
@@ -183,7 +183,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
@Override
public Mono<Void> setComplete() {
return doCommit(null);
return !isCommitted() ? doCommit(null) : Mono.empty();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,11 +39,12 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResponseStatusException) {
exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus());
// Response may be committed but we'll try..
logger.debug(ex.getMessage());
exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus());
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
}