Eliminate the need for Encoder#getContentLength
Issue: SPR-16892
This commit is contained in:
@@ -55,8 +55,4 @@ public class ByteArrayEncoder extends AbstractEncoder<byte[]> {
|
||||
return Flux.from(inputStream).map(bufferFactory::wrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getContentLength(byte[] bytes, @Nullable MimeType mimeType) {
|
||||
return (long) bytes.length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,4 @@ public class ByteBufferEncoder extends AbstractEncoder<ByteBuffer> {
|
||||
return Flux.from(inputStream).map(bufferFactory::wrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getContentLength(ByteBuffer byteBuffer, @Nullable MimeType mimeType) {
|
||||
return (long) byteBuffer.array().length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,11 +82,6 @@ public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
|
||||
return charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getContentLength(CharSequence data, @Nullable MimeType mimeType) {
|
||||
return (long) data.toString().getBytes(getCharset(mimeType)).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@code CharSequenceEncoder} that supports only "text/plain".
|
||||
*/
|
||||
|
||||
@@ -55,9 +55,4 @@ public class DataBufferEncoder extends AbstractEncoder<DataBuffer> {
|
||||
return Flux.from(inputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getContentLength(DataBuffer dataBuffer, @Nullable MimeType mimeType) {
|
||||
return (long) dataBuffer.readableByteCount();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,17 +67,6 @@ public interface Encoder<T> {
|
||||
Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
|
||||
ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints);
|
||||
|
||||
/**
|
||||
* Return the length for the given item, if known.
|
||||
* @param t the item to check
|
||||
* @return the length in bytes, or {@code null} if not known.
|
||||
* @since 5.0.5
|
||||
*/
|
||||
@Nullable
|
||||
default Long getContentLength(T t, @Nullable MimeType mimeType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of mime types this encoder supports.
|
||||
*/
|
||||
|
||||
@@ -16,13 +16,11 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
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;
|
||||
@@ -70,17 +68,4 @@ public class ResourceEncoder extends AbstractSingleValueEncoder<Resource> {
|
||||
return DataBufferUtils.read(resource, dataBufferFactory, this.bufferSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getContentLength(Resource resource, @Nullable MimeType mimeType) {
|
||||
// Don't consume InputStream...
|
||||
if (InputStreamResource.class != resource.getClass()) {
|
||||
try {
|
||||
return resource.contentLength();
|
||||
}
|
||||
catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ReactiveHttpOutputMessage;
|
||||
@@ -98,23 +99,18 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
|
||||
@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);
|
||||
|
||||
// Response is not committed until the first signal...
|
||||
if (inputStream instanceof Mono) {
|
||||
HttpHeaders headers = message.getHeaders();
|
||||
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
|
||||
body = body.doOnNext(data -> headers.setContentLength(data.readableByteCount()));
|
||||
}
|
||||
}
|
||||
|
||||
return (isStreamingMediaType(contentType) ?
|
||||
message.writeAndFlushWith(body.map(Flux::just)) : message.writeWith(body));
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ 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;
|
||||
@@ -119,9 +120,9 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
headers.setContentType(resourceMediaType);
|
||||
|
||||
if (headers.getContentLength() < 0) {
|
||||
Long contentLength = this.encoder.getContentLength(resource, mediaType);
|
||||
if (contentLength != null) {
|
||||
headers.setContentLength(contentLength);
|
||||
long length = lengthOf(resource);
|
||||
if (length != -1) {
|
||||
headers.setContentLength(length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +142,18 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
private static long lengthOf(Resource resource) {
|
||||
// Don't consume InputStream...
|
||||
if (InputStreamResource.class != resource.getClass()) {
|
||||
try {
|
||||
return resource.contentLength();
|
||||
}
|
||||
catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
|
||||
ReactiveHttpOutputMessage message) {
|
||||
|
||||
@@ -192,8 +205,8 @@ public class ResourceHttpMessageWriter implements HttpMessageWriter<Resource> {
|
||||
if (regions.size() == 1){
|
||||
ResourceRegion region = regions.get(0);
|
||||
headers.setContentType(resourceMediaType);
|
||||
Long contentLength = this.encoder.getContentLength(resource, mediaType);
|
||||
if (contentLength != null) {
|
||||
long contentLength = lengthOf(resource);
|
||||
if (contentLength != -1) {
|
||||
long start = region.getPosition();
|
||||
long end = start + region.getCount() - 1;
|
||||
end = Math.min(end, contentLength - 1);
|
||||
|
||||
@@ -128,45 +128,59 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
|
||||
@Test
|
||||
public void personResponseBody() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/person", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> responseEntity = performGet("/person-response/person", JSON, Person.class);
|
||||
assertEquals(17, responseEntity.getHeaders().getContentLength());
|
||||
assertEquals(expected, responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithCompletableFuture() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/completable-future", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> responseEntity = performGet("/person-response/completable-future", JSON, Person.class);
|
||||
assertEquals(17, responseEntity.getHeaders().getContentLength());
|
||||
assertEquals(expected, responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithMono() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/mono", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> responseEntity = performGet("/person-response/mono", JSON, Person.class);
|
||||
assertEquals(17, responseEntity.getHeaders().getContentLength());
|
||||
assertEquals(expected, responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithMonoDeclaredAsObject() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/mono-declared-as-object", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> entity = performGet("/person-response/mono-declared-as-object", JSON, Person.class);
|
||||
assertEquals(17, entity.getHeaders().getContentLength());
|
||||
assertEquals(expected, entity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithSingle() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/single", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> entity = performGet("/person-response/single", JSON, Person.class);
|
||||
assertEquals(17, entity.getHeaders().getContentLength());
|
||||
assertEquals(expected, entity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithMonoResponseEntity() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person-response/mono-response-entity", JSON, Person.class).getBody());
|
||||
ResponseEntity<Person> entity = performGet("/person-response/mono-response-entity", JSON, Person.class);
|
||||
assertEquals(17, entity.getHeaders().getContentLength());
|
||||
assertEquals(expected, entity.getBody());
|
||||
}
|
||||
|
||||
@Test // SPR-16172
|
||||
public void personResponseBodyWithMonoResponseEntityXml() throws Exception {
|
||||
|
||||
String actual = performGet("/person-response/mono-response-entity-xml",
|
||||
new HttpHeaders(), String.class).getBody();
|
||||
String url = "/person-response/mono-response-entity-xml";
|
||||
ResponseEntity<String> entity = performGet(url, new HttpHeaders(), String.class);
|
||||
String actual = entity.getBody();
|
||||
|
||||
assertEquals(91, entity.getHeaders().getContentLength());
|
||||
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
|
||||
"<person><name>Robert</name></person>", actual);
|
||||
}
|
||||
@@ -174,13 +188,17 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
|
||||
@Test
|
||||
public void personResponseBodyWithList() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/person-response/list", JSON, PERSON_LIST).getBody());
|
||||
ResponseEntity<List<Person>> entity = performGet("/person-response/list", JSON, PERSON_LIST);
|
||||
assertEquals(36, entity.getHeaders().getContentLength());
|
||||
assertEquals(expected, entity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void personResponseBodyWithPublisher() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/person-response/publisher", JSON, PERSON_LIST).getBody());
|
||||
ResponseEntity<List<Person>> entity = performGet("/person-response/publisher", JSON, PERSON_LIST);
|
||||
assertEquals(-1, entity.getHeaders().getContentLength());
|
||||
assertEquals(expected, entity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user