Browse Source

Polish EncoderHttpMessageWriter

pull/1363/head
Rossen Stoyanchev 9 years ago
parent
commit
35805995c2
  1. 32
      spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

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

@ -29,7 +29,6 @@ import reactor.core.publisher.Mono; @@ -29,7 +29,6 @@ import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Encoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
@ -123,25 +122,30 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> { @@ -123,25 +122,30 @@ public class EncoderHttpMessageWriter<T> implements ServerHttpMessageWriter<T> {
MediaType mediaType, ReactiveHttpOutputMessage message,
Map<String, Object> hints) {
HttpHeaders headers = message.getHeaders();
MediaType contentType = updateContentType(message, mediaType);
if (headers.getContentType() == null) {
MediaType fallback = this.defaultMediaType;
mediaType = useFallback(mediaType, fallback) ? fallback : mediaType;
if (mediaType != null) {
mediaType = addDefaultCharset(mediaType, fallback);
headers.setContentType(mediaType);
}
}
Flux<DataBuffer> body = this.encoder.encode(inputStream,
message.bufferFactory(), elementType, headers.getContentType(), hints);
Flux<DataBuffer> body = this.encoder.encode(
inputStream, message.bufferFactory(), elementType, contentType, hints);
return isStreamingMediaType(headers.getContentType()) ?
return isStreamingMediaType(contentType) ?
message.writeAndFlushWith(body.map(Flux::just)) :
message.writeWith(body);
}
private MediaType updateContentType(ReactiveHttpOutputMessage message, MediaType mediaType) {
MediaType result = message.getHeaders().getContentType();
if (result != null) {
return result;
}
MediaType fallback = this.defaultMediaType;
result = useFallback(mediaType, fallback) ? fallback : mediaType;
if (result != null) {
result = addDefaultCharset(result, fallback);
message.getHeaders().setContentType(result);
}
return result;
}
private static boolean useFallback(MediaType main, MediaType fallback) {
return main == null || !main.isConcrete() ||
main.equals(MediaType.APPLICATION_OCTET_STREAM) && fallback != null;

Loading…
Cancel
Save