提交 4a397f10 编写于 作者: R Rossen Stoyanchev

ResourceDecoder supports filename hint

Closes gh-22267
上级 5a3ff352
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -41,6 +41,10 @@ import org.springframework.util.MimeTypeUtils; ...@@ -41,6 +41,10 @@ import org.springframework.util.MimeTypeUtils;
*/ */
public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> { public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
/** Name of hint with a filename for the resource(e.g. from "Content-Disposition" HTTP header). */
public static String FILENAME_HINT = ResourceDecoder.class.getName() + ".filename";
public ResourceDecoder() { public ResourceDecoder() {
super(MimeTypeUtils.ALL); super(MimeTypeUtils.ALL);
} }
...@@ -72,11 +76,22 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> { ...@@ -72,11 +76,22 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
} }
Class<?> clazz = elementType.toClass(); Class<?> clazz = elementType.toClass();
String filename = hints != null ? (String) hints.get(FILENAME_HINT) : null;
if (clazz == InputStreamResource.class) { if (clazz == InputStreamResource.class) {
return new InputStreamResource(new ByteArrayInputStream(bytes)); return new InputStreamResource(new ByteArrayInputStream(bytes)) {
@Override
public String getFilename() {
return filename;
}
};
} }
else if (Resource.class.isAssignableFrom(clazz)) { else if (Resource.class.isAssignableFrom(clazz)) {
return new ByteArrayResource(bytes); return new ByteArrayResource(bytes) {
@Override
public String getFilename() {
return filename;
}
};
} }
else { else {
throw new IllegalStateException("Unsupported resource class: " + clazz); throw new IllegalStateException("Unsupported resource class: " + clazz);
......
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,6 +18,7 @@ package org.springframework.core.codec; ...@@ -18,6 +18,7 @@ package org.springframework.core.codec;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
...@@ -100,23 +101,28 @@ public class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecode ...@@ -100,23 +101,28 @@ public class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecode
} }
@Override @Override
public void decodeToMono() throws Exception { public void decodeToMono() {
Flux<DataBuffer> input = Flux.concat( Flux<DataBuffer> input = Flux.concat(
dataBuffer(this.fooBytes), dataBuffer(this.fooBytes),
dataBuffer(this.barBytes)); dataBuffer(this.barBytes));
testDecodeToMonoAll(input, Resource.class, step -> step testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class),
.consumeNextWith(resource -> { step -> step
try { .consumeNextWith(value -> {
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream()); Resource resource = (Resource) value;
assertEquals("foobar", new String(bytes)); try {
} byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
catch (IOException e) { assertEquals("foobar", new String(bytes));
fail(e.getMessage()); assertEquals("testFile", resource.getFilename());
} }
}) catch (IOException e) {
.expectComplete() fail(e.getMessage());
.verify()); }
})
.expectComplete()
.verify(),
null,
Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
} }
} }
/*
* Copyright 2002-2019 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.util.Map;
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.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.StringUtils;
/**
* Simple around around {@link ResourceDecoder} that extracts the filename from
* the "Content-Disposition" header, if available, and passes it as the hint
* {@link ResourceDecoder#FILENAME_HINT}.
*
* @author Rossen Stoyanchev
* @since 5.2
*/
public class ResourceHttpMessageReader extends DecoderHttpMessageReader<Resource> {
public ResourceHttpMessageReader() {
super(new ResourceDecoder());
}
public ResourceHttpMessageReader(ResourceDecoder resourceDecoder) {
super(resourceDecoder);
}
@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();
}
}
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -29,7 +29,6 @@ import org.springframework.core.codec.DataBufferDecoder; ...@@ -29,7 +29,6 @@ import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.DataBufferEncoder; import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.Decoder; import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.codec.CodecConfigurer;
import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.DecoderHttpMessageReader;
...@@ -37,6 +36,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter; ...@@ -37,6 +36,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.FormHttpMessageReader; import org.springframework.http.codec.FormHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageReader;
import org.springframework.http.codec.ResourceHttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter;
import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.http.codec.json.Jackson2JsonEncoder;
...@@ -158,7 +158,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs { ...@@ -158,7 +158,7 @@ class BaseDefaultCodecs implements CodecConfigurer.DefaultCodecs {
readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder())); readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder())); readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder())); readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder()));
readers.add(new DecoderHttpMessageReader<>(new ResourceDecoder())); readers.add(new ResourceHttpMessageReader());
readers.add(new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly())); readers.add(new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
if (protobufPresent) { if (protobufPresent) {
Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder(); Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();
......
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -36,7 +36,6 @@ import org.springframework.core.codec.DataBufferDecoder; ...@@ -36,7 +36,6 @@ import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.DataBufferEncoder; import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.Decoder; import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -46,6 +45,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter; ...@@ -46,6 +45,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.FormHttpMessageReader; import org.springframework.http.codec.FormHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageReader;
import org.springframework.http.codec.ResourceHttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter;
import org.springframework.http.codec.ServerSentEventHttpMessageReader; import org.springframework.http.codec.ServerSentEventHttpMessageReader;
import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.json.Jackson2JsonDecoder;
...@@ -60,7 +60,7 @@ import org.springframework.http.codec.xml.Jaxb2XmlEncoder; ...@@ -60,7 +60,7 @@ import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.forClass; import static org.springframework.core.ResolvableType.*;
/** /**
* Unit tests for {@link ClientCodecConfigurer}. * Unit tests for {@link ClientCodecConfigurer}.
...@@ -81,7 +81,7 @@ public class ClientCodecConfigurerTests { ...@@ -81,7 +81,7 @@ public class ClientCodecConfigurerTests {
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
assertStringDecoder(getNextDecoder(readers), true); assertStringDecoder(getNextDecoder(readers), true);
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); // SPR-16804 assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); // SPR-16804
......
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -32,7 +32,6 @@ import org.springframework.core.codec.DataBufferDecoder; ...@@ -32,7 +32,6 @@ import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.DataBufferEncoder; import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.Decoder; import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.codec.CodecConfigurer;
...@@ -41,6 +40,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter; ...@@ -41,6 +40,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.FormHttpMessageReader; import org.springframework.http.codec.FormHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageReader;
import org.springframework.http.codec.ResourceHttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter;
import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.http.codec.json.Jackson2JsonEncoder;
...@@ -76,7 +76,7 @@ public class CodecConfigurerTests { ...@@ -76,7 +76,7 @@ public class CodecConfigurerTests {
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
assertStringDecoder(getNextDecoder(readers), true); assertStringDecoder(getNextDecoder(readers), true);
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
...@@ -128,7 +128,7 @@ public class CodecConfigurerTests { ...@@ -128,7 +128,7 @@ public class CodecConfigurerTests {
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
assertEquals(StringDecoder.class, getNextDecoder(readers).getClass()); assertEquals(StringDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
......
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -36,7 +36,6 @@ import org.springframework.core.codec.DataBufferDecoder; ...@@ -36,7 +36,6 @@ import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.DataBufferEncoder; import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.Decoder; import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder; import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -45,6 +44,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter; ...@@ -45,6 +44,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.FormHttpMessageReader; import org.springframework.http.codec.FormHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageReader;
import org.springframework.http.codec.ResourceHttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.ServerSentEventHttpMessageWriter; import org.springframework.http.codec.ServerSentEventHttpMessageWriter;
...@@ -61,7 +61,7 @@ import org.springframework.http.codec.xml.Jaxb2XmlEncoder; ...@@ -61,7 +61,7 @@ import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.core.ResolvableType.forClass; import static org.springframework.core.ResolvableType.*;
/** /**
* Unit tests for {@link ServerCodecConfigurer}. * Unit tests for {@link ServerCodecConfigurer}.
...@@ -82,7 +82,7 @@ public class ServerCodecConfigurerTests { ...@@ -82,7 +82,7 @@ public class ServerCodecConfigurerTests {
assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteArrayDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ByteBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass()); assertEquals(DataBufferDecoder.class, getNextDecoder(readers).getClass());
assertEquals(ResourceDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ResourceHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
assertStringDecoder(getNextDecoder(readers), true); assertStringDecoder(getNextDecoder(readers), true);
assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass()); assertEquals(ProtobufDecoder.class, getNextDecoder(readers).getClass());
assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass()); assertEquals(FormHttpMessageReader.class, readers.get(this.index.getAndIncrement()).getClass());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册