Consistent use of Mediatype in EncoderHttpMessageWriter

EncoderHttpMessageWriter now consistently uses the same MediaType to
set on the response and to pass to the encoder.

Issue: SPR-15357
This commit is contained in:
Rossen Stoyanchev
2017-03-17 16:33:33 -04:00
parent 91a75ed772
commit 2979b37ae3
4 changed files with 125 additions and 39 deletions

View File

@@ -26,7 +26,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.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
@@ -94,20 +93,20 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
if (headers.getContentType() == null) {
MediaType fallback = this.defaultMediaType;
MediaType selected = useFallback(mediaType, fallback) ? fallback : mediaType;
if (selected != null) {
selected = addDefaultCharset(selected, fallback);
headers.setContentType(selected);
mediaType = useFallback(mediaType, fallback) ? fallback : mediaType;
if (mediaType != null) {
mediaType = addDefaultCharset(mediaType, fallback);
headers.setContentType(mediaType);
}
}
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
Flux<DataBuffer> body = this.encoder.encode(inputStream, bufferFactory, elementType, mediaType, hints);
Flux<DataBuffer> body = this.encoder.encode(inputStream,
outputMessage.bufferFactory(), elementType, headers.getContentType(), hints);
return (hints.get(FLUSHING_STRATEGY_HINT) == AFTER_EACH_ELEMENT ?
outputMessage.writeAndFlushWith(body.map(Flux::just)) : outputMessage.writeWith(body));
}
private static boolean useFallback(MediaType main, MediaType fallback) {
return main == null || !main.isConcrete() ||
main.equals(MediaType.APPLICATION_OCTET_STREAM) && fallback != null;