From ffc1f960f9b87aadab071aa46fecc59f29a9bb28 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 7 Jan 2020 09:05:49 +0100 Subject: [PATCH] Polish See gh-24238 --- .../org/springframework/http/HttpHeaders.java | 32 +++++++++---------- .../http/ReadOnlyHttpHeaders.java | 5 +++ .../reactive/AbstractServerHttpResponse.java | 12 ++----- .../RequestMappingHandlerAdapter.java | 5 +-- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 68e693375d..68c4ab013c 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1522,6 +1522,22 @@ public class HttpHeaders implements MultiValueMap, Serializable return Collections.emptyList(); } + /** + * Remove the well-known {@code "Content-*"} HTTP headers. + *

Such headers should be cleared from the response if the intended + * body can't be written due to errors. + * @since 5.2.3 + */ + public void clearContentHeaders() { + this.headers.remove(HttpHeaders.CONTENT_DISPOSITION); + this.headers.remove(HttpHeaders.CONTENT_ENCODING); + this.headers.remove(HttpHeaders.CONTENT_LANGUAGE); + this.headers.remove(HttpHeaders.CONTENT_LENGTH); + this.headers.remove(HttpHeaders.CONTENT_LOCATION); + this.headers.remove(HttpHeaders.CONTENT_RANGE); + this.headers.remove(HttpHeaders.CONTENT_TYPE); + } + /** * Retrieve a combined result from the field values of the ETag header. * @param headerName the header name @@ -1827,22 +1843,6 @@ public class HttpHeaders implements MultiValueMap, Serializable return new String(encodedBytes, charset); } - /** - * Remove the well-known {@code "Content-*"} HTTP headers from the given instance. - *

Such headers should be cleared, if possible, from the response if the intended - * body can't be written due to errors. - * @since 5.2.3 - */ - public static void clearContentHeaders(HttpHeaders headers) { - headers.remove(HttpHeaders.CONTENT_DISPOSITION); - headers.remove(HttpHeaders.CONTENT_ENCODING); - headers.remove(HttpHeaders.CONTENT_LANGUAGE); - headers.remove(HttpHeaders.CONTENT_LENGTH); - headers.remove(HttpHeaders.CONTENT_LOCATION); - headers.remove(HttpHeaders.CONTENT_RANGE); - headers.remove(HttpHeaders.CONTENT_TYPE); - } - // Package-private: used in ResponseCookie static String formatDate(long date) { Instant instant = Instant.ofEpochMilli(date); diff --git a/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java b/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java index ff129f7a0c..3fb7340863 100644 --- a/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java @@ -75,6 +75,11 @@ class ReadOnlyHttpHeaders extends HttpHeaders { } } + @Override + public void clearContentHeaders() { + // No-op. + } + @Override public List get(Object key) { List values = this.headers.get(key); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java index e53366e276..a871267914 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java @@ -182,22 +182,16 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { return ((Mono) body).flatMap(buffer -> doCommit(() -> writeWithInternal(Mono.just(buffer))) .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)) - .doOnError(t -> clearContentHeaders()); + .doOnError(t -> this.getHeaders().clearContentHeaders()); } return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner))) - .doOnError(t -> clearContentHeaders()); + .doOnError(t -> this.getHeaders().clearContentHeaders()); } @Override public final Mono writeAndFlushWith(Publisher> body) { return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeAndFlushWithInternal(inner))) - .doOnError(t -> clearContentHeaders()); - } - - private void clearContentHeaders() { - if (!this.isCommitted()) { - HttpHeaders.clearContentHeaders(this.getHeaders()); - } + .doOnError(t -> this.getHeaders().clearContentHeaders()); } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java index a8f5a1efea..094c8a7287 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java @@ -29,7 +29,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.ReactiveAdapterRegistry; -import org.springframework.http.HttpHeaders; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.lang.Nullable; @@ -210,9 +209,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application // Success and error responses may use different content types exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); - if (!exchange.getResponse().isCommitted()) { - HttpHeaders.clearContentHeaders(exchange.getResponse().getHeaders()); - } + exchange.getResponse().getHeaders().clearContentHeaders(); InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod); if (invocable != null) {