Skip to content

Commit 2781584

Browse files
committed
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
1 parent 7a8e0ff commit 2781584

File tree

9 files changed

+104
-39
lines changed

9 files changed

+104
-39
lines changed

spring-core/src/main/java/org/springframework/core/codec/ByteArrayEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,4 +55,8 @@ public Flux<DataBuffer> encode(Publisher<? extends byte[]> inputStream,
5555
return Flux.from(inputStream).map(bufferFactory::wrap);
5656
}
5757

58+
@Override
59+
public Long getContentLength(byte[] bytes, @Nullable MimeType mimeType) {
60+
return (long) bytes.length;
61+
}
5862
}

spring-core/src/main/java/org/springframework/core/codec/ByteBufferEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,4 +56,8 @@ public Flux<DataBuffer> encode(Publisher<? extends ByteBuffer> inputStream,
5656
return Flux.from(inputStream).map(bufferFactory::wrap);
5757
}
5858

59+
@Override
60+
public Long getContentLength(ByteBuffer byteBuffer, @Nullable MimeType mimeType) {
61+
return (long) byteBuffer.array().length;
62+
}
5963
}

spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,20 +62,30 @@ public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
6262
DataBufferFactory bufferFactory, ResolvableType elementType,
6363
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
6464

65+
Charset charset = getCharset(mimeType);
66+
67+
return Flux.from(inputStream).map(charSequence -> {
68+
CharBuffer charBuffer = CharBuffer.wrap(charSequence);
69+
ByteBuffer byteBuffer = charset.encode(charBuffer);
70+
return bufferFactory.wrap(byteBuffer);
71+
});
72+
}
73+
74+
private Charset getCharset(@Nullable MimeType mimeType) {
6575
Charset charset;
6676
if (mimeType != null && mimeType.getCharset() != null) {
6777
charset = mimeType.getCharset();
6878
}
6979
else {
7080
charset = DEFAULT_CHARSET;
7181
}
72-
return Flux.from(inputStream).map(charSequence -> {
73-
CharBuffer charBuffer = CharBuffer.wrap(charSequence);
74-
ByteBuffer byteBuffer = charset.encode(charBuffer);
75-
return bufferFactory.wrap(byteBuffer);
76-
});
82+
return charset;
7783
}
7884

85+
@Override
86+
public Long getContentLength(CharSequence data, @Nullable MimeType mimeType) {
87+
return (long) data.toString().getBytes(getCharset(mimeType)).length;
88+
}
7989

8090
/**
8191
* Create a {@code CharSequenceEncoder} that supports only "text/plain".

spring-core/src/main/java/org/springframework/core/codec/DataBufferEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,4 +55,8 @@ public Flux<DataBuffer> encode(Publisher<? extends DataBuffer> inputStream,
5555
return Flux.from(inputStream);
5656
}
5757

58+
@Override
59+
public Long getContentLength(DataBuffer dataBuffer, MimeType mimeType) {
60+
return (long) dataBuffer.readableByteCount();
61+
}
5862
}

spring-core/src/main/java/org/springframework/core/codec/Encoder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,6 +67,17 @@ public interface Encoder<T> {
6767
Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
6868
ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints);
6969

70+
/**
71+
* Return the length for the given item, if known.
72+
* @param t the item to check
73+
* @return the length in bytes, or {@code null} if not known.
74+
* @since 5.0.5
75+
*/
76+
@Nullable
77+
default Long getContentLength(T t, @Nullable MimeType mimeType) {
78+
return null;
79+
}
80+
7081
/**
7182
* Return the list of mime types this encoder supports.
7283
*/

spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
1616

1717
package org.springframework.core.codec;
1818

19+
import java.io.IOException;
1920
import java.util.Map;
21+
import java.util.OptionalLong;
2022

2123
import reactor.core.publisher.Flux;
2224

2325
import org.springframework.core.ResolvableType;
26+
import org.springframework.core.io.InputStreamResource;
2427
import org.springframework.core.io.Resource;
2528
import org.springframework.core.io.buffer.DataBuffer;
2629
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -68,4 +71,17 @@ protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBuffe
6871
return DataBufferUtils.read(resource, dataBufferFactory, this.bufferSize);
6972
}
7073

74+
@Override
75+
public Long getContentLength(Resource resource, @Nullable MimeType mimeType) {
76+
// Don't consume InputStream...
77+
if (InputStreamResource.class != resource.getClass()) {
78+
try {
79+
return resource.contentLength();
80+
}
81+
catch (IOException ignored) {
82+
}
83+
}
84+
return null;
85+
}
86+
7187
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.core.ResolvableType;
2929
import org.springframework.core.codec.Encoder;
3030
import org.springframework.core.io.buffer.DataBuffer;
31+
import org.springframework.http.HttpHeaders;
3132
import org.springframework.http.MediaType;
3233
import org.springframework.http.ReactiveHttpOutputMessage;
3334
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -91,11 +92,25 @@ public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaTyp
9192
return this.encoder.canEncode(elementType, mediaType);
9293
}
9394

95+
@SuppressWarnings("unchecked")
9496
@Override
9597
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
9698
@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
9799

98100
MediaType contentType = updateContentType(message, mediaType);
101+
HttpHeaders headers = message.getHeaders();
102+
103+
if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
104+
if (inputStream instanceof Mono) {
105+
// This works because we don't actually commit until after the first signal...
106+
inputStream = ((Mono<T>) inputStream).doOnNext(data -> {
107+
Long contentLength = this.encoder.getContentLength(data, contentType);
108+
if (contentLength != null) {
109+
headers.setContentLength(contentLength);
110+
}
111+
});
112+
}
113+
}
99114

100115
Flux<DataBuffer> body = this.encoder.encode(
101116
inputStream, message.bufferFactory(), elementType, contentType, hints);

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Optional;
25-
import java.util.OptionalLong;
2625

2726
import org.reactivestreams.Publisher;
2827
import reactor.core.publisher.Flux;
@@ -32,7 +31,6 @@
3231
import org.springframework.core.codec.ResourceDecoder;
3332
import org.springframework.core.codec.ResourceEncoder;
3433
import org.springframework.core.codec.ResourceRegionEncoder;
35-
import org.springframework.core.io.InputStreamResource;
3634
import org.springframework.core.io.Resource;
3735
import org.springframework.core.io.buffer.DataBuffer;
3836
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -49,7 +47,7 @@
4947
import org.springframework.lang.Nullable;
5048
import org.springframework.util.MimeTypeUtils;
5149

52-
import static java.util.Collections.emptyMap;
50+
import static java.util.Collections.*;
5351

5452
/**
5553
* {@code HttpMessageWriter} that can write a {@link Resource}.
@@ -121,7 +119,10 @@ private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullab
121119
headers.setContentType(resourceMediaType);
122120

123121
if (headers.getContentLength() < 0) {
124-
lengthOf(resource).ifPresent(headers::setContentLength);
122+
Long contentLength = this.encoder.getContentLength(resource, mediaType);
123+
if (contentLength != null) {
124+
headers.setContentLength(contentLength);
125+
}
125126
}
126127

127128
return zeroCopy(resource, null, message)
@@ -140,18 +141,6 @@ private static MediaType getResourceMediaType(@Nullable MediaType mediaType, Res
140141
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
141142
}
142143

143-
private static OptionalLong lengthOf(Resource resource) {
144-
// Don't consume InputStream...
145-
if (InputStreamResource.class != resource.getClass()) {
146-
try {
147-
return OptionalLong.of(resource.contentLength());
148-
}
149-
catch (IOException ignored) {
150-
}
151-
}
152-
return OptionalLong.empty();
153-
}
154-
155144
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
156145
ReactiveHttpOutputMessage message) {
157146

@@ -205,13 +194,14 @@ public Mono<Void> write(Publisher<? extends Resource> inputStream, @Nullable Res
205194
if (regions.size() == 1){
206195
ResourceRegion region = regions.get(0);
207196
headers.setContentType(resourceMediaType);
208-
lengthOf(resource).ifPresent(length -> {
197+
Long contentLength = this.encoder.getContentLength(resource, mediaType);
198+
if (contentLength != null) {
209199
long start = region.getPosition();
210200
long end = start + region.getCount() - 1;
211-
end = Math.min(end, length - 1);
212-
headers.add("Content-Range", "bytes " + start + '-' + end + '/' + length);
201+
end = Math.min(end, contentLength - 1);
202+
headers.add("Content-Range", "bytes " + start + '-' + end + '/' + contentLength);
213203
headers.setContentLength(end - start + 1);
214-
});
204+
}
215205
return writeSingleRegion(region, response);
216206
}
217207
else {

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingMessageConversionIntegrationTests.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,10 +59,9 @@
5959
import org.springframework.web.bind.annotation.RestController;
6060
import org.springframework.web.reactive.config.EnableWebFlux;
6161

62-
import static java.util.Arrays.asList;
63-
import static org.junit.Assert.assertEquals;
64-
import static org.junit.Assert.assertTrue;
65-
import static org.springframework.http.MediaType.APPLICATION_XML;
62+
import static java.util.Arrays.*;
63+
import static org.junit.Assert.*;
64+
import static org.springframework.http.MediaType.*;
6665

6766
/**
6867
* {@code @RequestMapping} integration tests focusing on serialization and
@@ -87,7 +86,6 @@ protected ApplicationContext initApplicationContext() {
8786
return wac;
8887
}
8988

90-
9189
@Test
9290
public void byteBufferResponseBodyWithPublisher() throws Exception {
9391
Person expected = new Person("Robert");
@@ -100,6 +98,14 @@ public void byteBufferResponseBodyWithFlux() throws Exception {
10098
assertEquals(expected, performGet("/raw-response/flux", new HttpHeaders(), String.class).getBody());
10199
}
102100

101+
@Test
102+
public void byteBufferResponseBodyWithMono() throws Exception {
103+
String expected = "Hello!";
104+
ResponseEntity<String> responseEntity = performGet("/raw-response/mono", new HttpHeaders(), String.class);
105+
assertEquals(6, responseEntity.getHeaders().getContentLength());
106+
assertEquals(expected, responseEntity.getBody());
107+
}
108+
103109
@Test
104110
public void byteBufferResponseBodyWithObservable() throws Exception {
105111
String expected = "Hello!";
@@ -422,6 +428,11 @@ public Flux<ByteBuffer> getFlux() {
422428
return Flux.just(ByteBuffer.wrap("Hello!".getBytes()));
423429
}
424430

431+
@GetMapping("/mono")
432+
public Mono<ByteBuffer> getMonoString() {
433+
return Mono.just(ByteBuffer.wrap("Hello!".getBytes()));
434+
}
435+
425436
@GetMapping("/observable")
426437
public Observable<ByteBuffer> getObservable() {
427438
return Observable.just(ByteBuffer.wrap("Hello!".getBytes()));

0 commit comments

Comments
 (0)