diff --git a/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java index 96cd774f59f..27c2672baad 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -96,13 +96,15 @@ public class DecoderHttpMessageReader implements HttpMessageReader { @Override public Flux read(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) { MediaType contentType = getContentType(message); - return this.decoder.decode(message.getBody(), elementType, contentType, hints); + Map allHints = Hints.merge(hints, getReadHints(elementType, message)); + return this.decoder.decode(message.getBody(), elementType, contentType, allHints); } @Override public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) { MediaType contentType = getContentType(message); - return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints); + Map allHints = Hints.merge(hints, getReadHints(elementType, message)); + return this.decoder.decodeToMono(message.getBody(), elementType, contentType, allHints); } /** @@ -118,6 +120,14 @@ public class DecoderHttpMessageReader implements HttpMessageReader { return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); } + /** + * Get additional hints for decoding based on the input HTTP message. + * @since 5.3 + */ + protected Map getReadHints(ResolvableType elementType, ReactiveHttpInputMessage message) { + return Hints.none(); + } + // Server-side only... diff --git a/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageReader.java index 8a51ab296df..c7eaa23362e 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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,6 +22,7 @@ import org.springframework.core.ResolvableType; import org.springframework.core.codec.Hints; import org.springframework.core.codec.ResourceDecoder; import org.springframework.core.io.Resource; +import org.springframework.http.ReactiveHttpInputMessage; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.StringUtils; @@ -45,12 +46,18 @@ public class ResourceHttpMessageReader extends DecoderHttpMessageReader getReadHints(ResolvableType elementType, ReactiveHttpInputMessage message) { + String filename = message.getHeaders().getContentDisposition().getFilename(); + return (StringUtils.hasText(filename) ? + Hints.from(ResourceDecoder.FILENAME_HINT, filename) : Hints.none()); + } + @Override protected Map getReadHints(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request, ServerHttpResponse response) { - String name = request.getHeaders().getContentDisposition().getFilename(); - return StringUtils.hasText(name) ? Hints.from(ResourceDecoder.FILENAME_HINT, name) : Hints.none(); + return getReadHints(elementType, request); } } diff --git a/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageReaderTests.java b/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageReaderTests.java new file mode 100644 index 00000000000..1f076897b1d --- /dev/null +++ b/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageReaderTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2002-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.http.codec; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Collections; + +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; + +import org.springframework.core.ResolvableType; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests; +import org.springframework.http.ContentDisposition; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link ResourceHttpMessageReader}. + * @author Rossen Stoyanchev + */ +public class ResourceHttpMessageReaderTests extends AbstractLeakCheckingTests { + + private final ResourceHttpMessageReader reader = new ResourceHttpMessageReader(); + + @Test + void readResourceAsMono() throws IOException { + String filename = "test.txt"; + String body = "Test resource content"; + + ContentDisposition contentDisposition = + ContentDisposition.builder("attachment").name("file").filename(filename).build(); + + MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); + response.getHeaders().setContentType(MediaType.TEXT_PLAIN); + response.getHeaders().setContentDisposition(contentDisposition); + response.setBody(Mono.just(stringBuffer(body))); + + Resource resource = reader.readMono( + ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block(); + + assertThat(resource).isNotNull(); + assertThat(resource.getFilename()).isEqualTo(filename); + assertThat(resource.getInputStream()).hasContent(body); + } + + private DataBuffer stringBuffer(String value) { + byte[] bytes = value.getBytes(StandardCharsets.UTF_8); + DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); + buffer.write(bytes); + return buffer; + } + +}