content-length support in EncoderHttpMessageWriter
EncoderHttpMessageWriter checks explicitly for Mono publishers and sets the content length, if it is known for the given data item. Issue: SPR-16542
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ 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;
|
||||
@@ -91,11 +92,25 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
return this.encoder.canEncode(elementType, mediaType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
|
||||
@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
|
||||
|
||||
MediaType contentType = updateContentType(message, mediaType);
|
||||
HttpHeaders headers = message.getHeaders();
|
||||
|
||||
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
|
||||
if (inputStream instanceof Mono) {
|
||||
// This works because we don't actually commit until after the first signal...
|
||||
inputStream = ((Mono<T>) inputStream).doOnNext(data -> {
|
||||
Long contentLength = this.encoder.getContentLength(data, contentType);
|
||||
if (contentLength != null) {
|
||||
headers.setContentLength(contentLength);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Flux<DataBuffer> body = this.encoder.encode(
|
||||
inputStream, message.bufferFactory(), elementType, contentType, hints);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,7 +22,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -32,7 +31,6 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.ResourceDecoder;
|
||||
import org.springframework.core.codec.ResourceEncoder;
|
||||
import org.springframework.core.codec.ResourceRegionEncoder;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
@@ -49,7 +47,7 @@ import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.*;
|
||||
|
||||
/**
|
||||
* {@code HttpMessageWriter} that can write a {@link Resource}.
|
||||
@@ -121,7 +119,10 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
headers.setContentType(resourceMediaType);
|
||||
|
||||
if (headers.getContentLength() < 0) {
|
||||
lengthOf(resource).ifPresent(headers::setContentLength);
|
||||
Long contentLength = this.encoder.getContentLength(resource, mediaType);
|
||||
if (contentLength != null) {
|
||||
headers.setContentLength(contentLength);
|
||||
}
|
||||
}
|
||||
|
||||
return zeroCopy(resource, null, message)
|
||||
@@ -140,18 +141,6 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
private static OptionalLong lengthOf(Resource resource) {
|
||||
// Don't consume InputStream...
|
||||
if (InputStreamResource.class != resource.getClass()) {
|
||||
try {
|
||||
return OptionalLong.of(resource.contentLength());
|
||||
}
|
||||
catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
return OptionalLong.empty();
|
||||
}
|
||||
|
||||
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
|
||||
ReactiveHttpOutputMessage message) {
|
||||
|
||||
@@ -205,13 +194,14 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
if (regions.size() == 1){
|
||||
ResourceRegion region = regions.get(0);
|
||||
headers.setContentType(resourceMediaType);
|
||||
lengthOf(resource).ifPresent(length -> {
|
||||
Long contentLength = this.encoder.getContentLength(resource, mediaType);
|
||||
if (contentLength != null) {
|
||||
long start = region.getPosition();
|
||||
long end = start + region.getCount() - 1;
|
||||
end = Math.min(end, length - 1);
|
||||
headers.add("Content-Range", "bytes " + start + '-' + end + '/' + length);
|
||||
end = Math.min(end, contentLength - 1);
|
||||
headers.add("Content-Range", "bytes " + start + '-' + end + '/' + contentLength);
|
||||
headers.setContentLength(end - start + 1);
|
||||
});
|
||||
}
|
||||
return writeSingleRegion(region, response);
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user