Skip to content

Commit 4a397f1

Browse files
committed
ResourceDecoder supports filename hint
Closes gh-22267
1 parent 5a3ff35 commit 4a397f1

File tree

7 files changed

+109
-32
lines changed

7 files changed

+109
-32
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -41,6 +41,10 @@
4141
*/
4242
public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
4343

44+
/** Name of hint with a filename for the resource(e.g. from "Content-Disposition" HTTP header). */
45+
public static String FILENAME_HINT = ResourceDecoder.class.getName() + ".filename";
46+
47+
4448
public ResourceDecoder() {
4549
super(MimeTypeUtils.ALL);
4650
}
@@ -72,11 +76,22 @@ protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elemen
7276
}
7377

7478
Class<?> clazz = elementType.toClass();
79+
String filename = hints != null ? (String) hints.get(FILENAME_HINT) : null;
7580
if (clazz == InputStreamResource.class) {
76-
return new InputStreamResource(new ByteArrayInputStream(bytes));
81+
return new InputStreamResource(new ByteArrayInputStream(bytes)) {
82+
@Override
83+
public String getFilename() {
84+
return filename;
85+
}
86+
};
7787
}
7888
else if (Resource.class.isAssignableFrom(clazz)) {
79-
return new ByteArrayResource(bytes);
89+
return new ByteArrayResource(bytes) {
90+
@Override
91+
public String getFilename() {
92+
return filename;
93+
}
94+
};
8095
}
8196
else {
8297
throw new IllegalStateException("Unsupported resource class: " + clazz);

spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
21+
import java.util.Collections;
2122
import java.util.Map;
2223

2324
import org.junit.Test;
@@ -100,23 +101,28 @@ protected void testDecodeError(Publisher<DataBuffer> input, ResolvableType outpu
100101
}
101102

102103
@Override
103-
public void decodeToMono() throws Exception {
104+
public void decodeToMono() {
104105
Flux<DataBuffer> input = Flux.concat(
105106
dataBuffer(this.fooBytes),
106107
dataBuffer(this.barBytes));
107108

108-
testDecodeToMonoAll(input, Resource.class, step -> step
109-
.consumeNextWith(resource -> {
110-
try {
111-
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
112-
assertEquals("foobar", new String(bytes));
113-
}
114-
catch (IOException e) {
115-
fail(e.getMessage());
116-
}
117-
})
118-
.expectComplete()
119-
.verify());
109+
testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class),
110+
step -> step
111+
.consumeNextWith(value -> {
112+
Resource resource = (Resource) value;
113+
try {
114+
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
115+
assertEquals("foobar", new String(bytes));
116+
assertEquals("testFile", resource.getFilename());
117+
}
118+
catch (IOException e) {
119+
fail(e.getMessage());
120+
}
121+
})
122+
.expectComplete()
123+
.verify(),
124+
null,
125+
Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
120126
}
121127

122128
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.http.codec;
17+
18+
import java.util.Map;
19+
20+
import org.springframework.core.ResolvableType;
21+
import org.springframework.core.codec.Hints;
22+
import org.springframework.core.codec.ResourceDecoder;
23+
import org.springframework.core.io.Resource;
24+
import org.springframework.http.server.reactive.ServerHttpRequest;
25+
import org.springframework.http.server.reactive.ServerHttpResponse;
26+
import org.springframework.util.StringUtils;
27+
28+
/**
29+
* Simple around around {@link ResourceDecoder} that extracts the filename from
30+
* the "Content-Disposition" header, if available, and passes it as the hint
31+
* {@link ResourceDecoder#FILENAME_HINT}.
32+
*
33+
* @author Rossen Stoyanchev
34+
* @since 5.2
35+
*/
36+
public class ResourceHttpMessageReader extends DecoderHttpMessageReader<Resource> {
37+
38+
39+
public ResourceHttpMessageReader() {
40+
super(new ResourceDecoder());
41+
}
42+
43+
public ResourceHttpMessageReader(ResourceDecoder resourceDecoder) {
44+
super(resourceDecoder);
45+
}
46+
47+
48+
@Override
49+
protected Map<String, Object> getReadHints(ResolvableType actualType, ResolvableType elementType,
50+
ServerHttpRequest request, ServerHttpResponse response) {
51+
52+
String name = request.getHeaders().getContentDisposition().getFilename();
53+
return StringUtils.hasText(name) ? Hints.from(ResourceDecoder.FILENAME_HINT, name) : Hints.none();
54+
}
55+
56+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -29,14 +29,14 @@
2929
import org.springframework.core.codec.DataBufferEncoder;
3030
import org.springframework.core.codec.Decoder;
3131
import org.springframework.core.codec.Encoder;
32-
import org.springframework.core.codec.ResourceDecoder;
3332
import org.springframework.core.codec.StringDecoder;
3433
import org.springframework.http.codec.CodecConfigurer;
3534
import org.springframework.http.codec.DecoderHttpMessageReader;
3635
import org.springframework.http.codec.EncoderHttpMessageWriter;
3736
import org.springframework.http.codec.FormHttpMessageReader;
3837
import org.springframework.http.codec.HttpMessageReader;
3938
import org.springframework.http.codec.HttpMessageWriter;
39+
import org.springframework.http.codec.ResourceHttpMessageReader;
4040
import org.springframework.http.codec.ResourceHttpMessageWriter;
4141
import org.springframework.http.codec.json.Jackson2JsonDecoder;
4242
import org.springframework.http.codec.json.Jackson2JsonEncoder;
@@ -158,7 +158,7 @@ final List<HttpMessageReader<?>> getTypedReaders() {
158158
readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
159159
readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
160160
readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder()));
161-
readers.add(new DecoderHttpMessageReader<>(new ResourceDecoder()));
161+
readers.add(new ResourceHttpMessageReader());
162162
readers.add(new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
163163
if (protobufPresent) {
164164
Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();

spring-web/src/test/java/org/springframework/http/codec/support/ClientCodecConfigurerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -36,7 +36,6 @@
3636
import org.springframework.core.codec.DataBufferEncoder;
3737
import org.springframework.core.codec.Decoder;
3838
import org.springframework.core.codec.Encoder;
39-
import org.springframework.core.codec.ResourceDecoder;
4039
import org.springframework.core.codec.StringDecoder;
4140
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
4241
import org.springframework.http.MediaType;
@@ -46,6 +45,7 @@
4645
import org.springframework.http.codec.FormHttpMessageReader;
4746
import org.springframework.http.codec.HttpMessageReader;
4847
import org.springframework.http.codec.HttpMessageWriter;
48+
import org.springframework.http.codec.ResourceHttpMessageReader;
4949
import org.springframework.http.codec.ResourceHttpMessageWriter;
5050
import org.springframework.http.codec.ServerSentEventHttpMessageReader;
5151
import org.springframework.http.codec.json.Jackson2JsonDecoder;
@@ -60,7 +60,7 @@
6060
import org.springframework.util.MimeTypeUtils;
6161

6262
import static org.junit.Assert.*;
63-
import static org.springframework.core.ResolvableType.forClass;
63+
import static org.springframework.core.ResolvableType.*;
6464

6565
/**
6666
* Unit tests for {@link ClientCodecConfigurer}.
@@ -81,7 +81,7 @@ public void defaultReaders() {
8181
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
8282
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
8383
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
84-
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
84+
assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
8585
assertStringDecoder(getNextDecoder(readers), true);
8686
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
8787
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); // SPR-16804

spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -32,7 +32,6 @@
3232
import org.springframework.core.codec.DataBufferEncoder;
3333
import org.springframework.core.codec.Decoder;
3434
import org.springframework.core.codec.Encoder;
35-
import org.springframework.core.codec.ResourceDecoder;
3635
import org.springframework.core.codec.StringDecoder;
3736
import org.springframework.http.MediaType;
3837
import org.springframework.http.codec.CodecConfigurer;
@@ -41,6 +40,7 @@
4140
import org.springframework.http.codec.FormHttpMessageReader;
4241
import org.springframework.http.codec.HttpMessageReader;
4342
import org.springframework.http.codec.HttpMessageWriter;
43+
import org.springframework.http.codec.ResourceHttpMessageReader;
4444
import org.springframework.http.codec.ResourceHttpMessageWriter;
4545
import org.springframework.http.codec.json.Jackson2JsonDecoder;
4646
import org.springframework.http.codec.json.Jackson2JsonEncoder;
@@ -76,7 +76,7 @@ public void defaultReaders() {
7676
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
7777
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
7878
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
79-
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
79+
assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
8080
assertStringDecoder(getNextDecoder(readers), true);
8181
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
8282
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
@@ -128,7 +128,7 @@ public void defaultAndCustomReaders() {
128128
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
129129
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
130130
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
131-
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
131+
assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
132132
assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
133133
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
134134
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());

spring-web/src/test/java/org/springframework/http/codec/support/ServerCodecConfigurerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -36,7 +36,6 @@
3636
import org.springframework.core.codec.DataBufferEncoder;
3737
import org.springframework.core.codec.Decoder;
3838
import org.springframework.core.codec.Encoder;
39-
import org.springframework.core.codec.ResourceDecoder;
4039
import org.springframework.core.codec.StringDecoder;
4140
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
4241
import org.springframework.http.MediaType;
@@ -45,6 +44,7 @@
4544
import org.springframework.http.codec.FormHttpMessageReader;
4645
import org.springframework.http.codec.HttpMessageReader;
4746
import org.springframework.http.codec.HttpMessageWriter;
47+
import org.springframework.http.codec.ResourceHttpMessageReader;
4848
import org.springframework.http.codec.ResourceHttpMessageWriter;
4949
import org.springframework.http.codec.ServerCodecConfigurer;
5050
import org.springframework.http.codec.ServerSentEventHttpMessageWriter;
@@ -61,7 +61,7 @@
6161
import org.springframework.util.MimeTypeUtils;
6262

6363
import static org.junit.Assert.*;
64-
import static org.springframework.core.ResolvableType.forClass;
64+
import static org.springframework.core.ResolvableType.*;
6565

6666
/**
6767
* Unit tests for {@link ServerCodecConfigurer}.
@@ -82,7 +82,7 @@ public void defaultReaders() {
8282
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
8383
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
8484
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
85-
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass());
85+
assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
8686
assertStringDecoder(getNextDecoder(readers), true);
8787
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
8888
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());

0 commit comments

Comments
 (0)