Skip to content

Commit fbdece6

Browse files
committed
Polishing in ResourceHttpMessageWriter
See gh-35536
1 parent 64d42fe commit fbdece6

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@
5151
import org.springframework.util.MimeTypeUtils;
5252

5353
/**
54-
* {@code HttpMessageWriter} that can write a {@link Resource}.
54+
* {@code HttpMessageWriter} that can write a {@link Resource} from both`` client
55+
* and server perspectives.
5556
*
56-
* <p>Also an implementation of {@code HttpMessageWriter} with support for writing one
57-
* or more {@link ResourceRegion}'s based on the HTTP ranges specified in the request.
57+
* <p>From a server perspective, the server-side only write method supports
58+
* writing one or more {@link ResourceRegion}'s based on HTTP ranges specified
59+
* in the request.
5860
*
59-
* <p>For reading to a Resource, use {@link ResourceDecoder} wrapped with
61+
* <p>To read a Resource, use {@link ResourceDecoder} wrapped with
6062
* {@link DecoderHttpMessageReader}.
6163
*
6264
* @author Arjen Poutsma
@@ -122,24 +124,30 @@ private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullab
122124
if (result != null) {
123125
return result;
124126
}
125-
else {
126-
Mono<Resource> input = Mono.just(resource);
127-
DataBufferFactory factory = message.bufferFactory();
128-
Flux<DataBuffer> body = this.encoder.encode(input, factory, type, message.getHeaders().getContentType(), hints)
129-
.subscribeOn(Schedulers.boundedElastic());
130-
if (logger.isDebugEnabled()) {
131-
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
132-
}
133-
return message.writeWith(body);
127+
128+
Mono<Resource> input = Mono.just(resource);
129+
DataBufferFactory factory = message.bufferFactory();
130+
MediaType contentType = message.getHeaders().getContentType();
131+
132+
Flux<DataBuffer> body = this.encoder.encode(input, factory, type, contentType, hints)
133+
.subscribeOn(Schedulers.boundedElastic());
134+
135+
if (logger.isDebugEnabled()) {
136+
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
134137
}
138+
139+
return message.writeWith(body);
135140
}));
136141
}
137142

138143
/**
139144
* Adds the default headers for the given resource to the given message.
140145
* @since 6.1
141146
*/
142-
public Mono<Void> addDefaultHeaders(ReactiveHttpOutputMessage message, Resource resource, @Nullable MediaType contentType, Map<String, Object> hints) {
147+
public Mono<Void> addDefaultHeaders(
148+
ReactiveHttpOutputMessage message, Resource resource, @Nullable MediaType contentType,
149+
Map<String, Object> hints) {
150+
143151
return Mono.defer(() -> {
144152
HttpHeaders headers = message.getHeaders();
145153
MediaType resourceMediaType = getResourceMediaType(contentType, resource, hints);
@@ -149,16 +157,15 @@ public Mono<Void> addDefaultHeaders(ReactiveHttpOutputMessage message, Resource
149157
headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");
150158
}
151159

152-
if (headers.getContentLength() < 0) {
153-
return lengthOf(resource)
154-
.flatMap(contentLength -> {
155-
headers.setContentLength(contentLength);
156-
return Mono.empty();
157-
});
158-
}
159-
else {
160+
if (headers.getContentLength() >= 0) {
160161
return Mono.empty();
161162
}
163+
164+
return lengthOf(resource)
165+
.flatMap(contentLength -> {
166+
headers.setContentLength(contentLength);
167+
return Mono.empty();
168+
});
162169
});
163170
}
164171

0 commit comments

Comments
 (0)