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