Browse Source

Polishing in ResourceHttpMessageWriter

See gh-35536
pull/35587/head
rstoyanchev 3 months ago
parent
commit
fbdece6759
  1. 51
      spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java

51
spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java

@ -51,12 +51,14 @@ import org.springframework.lang.Nullable; @@ -51,12 +51,14 @@ import org.springframework.lang.Nullable;
import org.springframework.util.MimeTypeUtils;
/**
* {@code HttpMessageWriter} that can write a {@link Resource}.
* {@code HttpMessageWriter} that can write a {@link Resource} from both`` client
* and server perspectives.
*
* <p>Also an implementation of {@code HttpMessageWriter} with support for writing one
* or more {@link ResourceRegion}'s based on the HTTP ranges specified in the request.
* <p>From a server perspective, the server-side only write method supports
* writing one or more {@link ResourceRegion}'s based on HTTP ranges specified
* in the request.
*
* <p>For reading to a Resource, use {@link ResourceDecoder} wrapped with
* <p>To read a Resource, use {@link ResourceDecoder} wrapped with
* {@link DecoderHttpMessageReader}.
*
* @author Arjen Poutsma
@ -122,16 +124,19 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> { @@ -122,16 +124,19 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
if (result != null) {
return result;
}
else {
Mono<Resource> input = Mono.just(resource);
DataBufferFactory factory = message.bufferFactory();
Flux<DataBuffer> body = this.encoder.encode(input, factory, type, message.getHeaders().getContentType(), hints)
.subscribeOn(Schedulers.boundedElastic());
if (logger.isDebugEnabled()) {
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
}
return message.writeWith(body);
Mono<Resource> input = Mono.just(resource);
DataBufferFactory factory = message.bufferFactory();
MediaType contentType = message.getHeaders().getContentType();
Flux<DataBuffer> body = this.encoder.encode(input, factory, type, contentType, hints)
.subscribeOn(Schedulers.boundedElastic());
if (logger.isDebugEnabled()) {
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
}
return message.writeWith(body);
}));
}
@ -139,7 +144,10 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> { @@ -139,7 +144,10 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
* Adds the default headers for the given resource to the given message.
* @since 6.1
*/
public Mono<Void> addDefaultHeaders(ReactiveHttpOutputMessage message, Resource resource, @Nullable MediaType contentType, Map<String, Object> hints) {
public Mono<Void> addDefaultHeaders(
ReactiveHttpOutputMessage message, Resource resource, @Nullable MediaType contentType,
Map<String, Object> hints) {
return Mono.defer(() -> {
HttpHeaders headers = message.getHeaders();
MediaType resourceMediaType = getResourceMediaType(contentType, resource, hints);
@ -149,16 +157,15 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> { @@ -149,16 +157,15 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");
}
if (headers.getContentLength() < 0) {
return lengthOf(resource)
.flatMap(contentLength -> {
headers.setContentLength(contentLength);
return Mono.empty();
});
}
else {
if (headers.getContentLength() >= 0) {
return Mono.empty();
}
return lengthOf(resource)
.flatMap(contentLength -> {
headers.setContentLength(contentLength);
return Mono.empty();
});
});
}

Loading…
Cancel
Save