Browse Source

Polish

See gh-24238
pull/24314/head
Brian Clozel 5 years ago
parent
commit
ffc1f960f9
  1. 32
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 5
      spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java
  3. 12
      spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java
  4. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java

32
spring-web/src/main/java/org/springframework/http/HttpHeaders.java

@ -1522,6 +1522,22 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
return Collections.emptyList(); return Collections.emptyList();
} }
/**
* Remove the well-known {@code "Content-*"} HTTP headers.
* <p>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. * Retrieve a combined result from the field values of the ETag header.
* @param headerName the header name * @param headerName the header name
@ -1827,22 +1843,6 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
return new String(encodedBytes, charset); return new String(encodedBytes, charset);
} }
/**
* Remove the well-known {@code "Content-*"} HTTP headers from the given instance.
* <p>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 // Package-private: used in ResponseCookie
static String formatDate(long date) { static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date); Instant instant = Instant.ofEpochMilli(date);

5
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 @Override
public List<String> get(Object key) { public List<String> get(Object key) {
List<String> values = this.headers.get(key); List<String> values = this.headers.get(key);

12
spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java

@ -182,22 +182,16 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
return ((Mono<? extends DataBuffer>) body).flatMap(buffer -> return ((Mono<? extends DataBuffer>) body).flatMap(buffer ->
doCommit(() -> writeWithInternal(Mono.just(buffer))) doCommit(() -> writeWithInternal(Mono.just(buffer)))
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)) .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release))
.doOnError(t -> clearContentHeaders()); .doOnError(t -> this.getHeaders().clearContentHeaders());
} }
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner))) return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner)))
.doOnError(t -> clearContentHeaders()); .doOnError(t -> this.getHeaders().clearContentHeaders());
} }
@Override @Override
public final Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) { public final Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeAndFlushWithInternal(inner))) return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeAndFlushWithInternal(inner)))
.doOnError(t -> clearContentHeaders()); .doOnError(t -> this.getHeaders().clearContentHeaders());
}
private void clearContentHeaders() {
if (!this.isCommitted()) {
HttpHeaders.clearContentHeaders(this.getHeaders());
}
} }
@Override @Override

5
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.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.HttpHeaders;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -210,9 +209,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
// Success and error responses may use different content types // Success and error responses may use different content types
exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().clearContentHeaders();
HttpHeaders.clearContentHeaders(exchange.getResponse().getHeaders());
}
InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod); InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod);
if (invocable != null) { if (invocable != null) {

Loading…
Cancel
Save