Support filename hint for client side too
Closes gh-25516
This commit is contained in:
parent
36553264ce
commit
a7f71f4d9b
|
|
@ -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<T> implements HttpMessageReader<T> {
|
|||
@Override
|
||||
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
|
||||
MediaType contentType = getContentType(message);
|
||||
return this.decoder.decode(message.getBody(), elementType, contentType, hints);
|
||||
Map<String, Object> allHints = Hints.merge(hints, getReadHints(elementType, message));
|
||||
return this.decoder.decode(message.getBody(), elementType, contentType, allHints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
|
||||
MediaType contentType = getContentType(message);
|
||||
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints);
|
||||
Map<String, Object> allHints = Hints.merge(hints, getReadHints(elementType, message));
|
||||
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, allHints);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,6 +120,14 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
|
|||
return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get additional hints for decoding based on the input HTTP message.
|
||||
* @since 5.3
|
||||
*/
|
||||
protected Map<String, Object> getReadHints(ResolvableType elementType, ReactiveHttpInputMessage message) {
|
||||
return Hints.none();
|
||||
}
|
||||
|
||||
|
||||
// Server-side only...
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Resource
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue