ResourceHttpMessageConverter reads Content-Disposition header to expose filename through Resource
Issue: SPR-15191
This commit is contained in:
parent
f51b896bd9
commit
768802fa96
|
|
@ -82,12 +82,23 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
|
|||
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
|
||||
throws IOException, HttpMessageNotReadableException {
|
||||
|
||||
final String filename = inputMessage.getHeaders().getContentDisposition().getFilename();
|
||||
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
|
||||
return new InputStreamResource(inputMessage.getBody());
|
||||
return new InputStreamResource(inputMessage.getBody()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
};
|
||||
}
|
||||
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
|
||||
byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
|
||||
return new ByteArrayResource(body);
|
||||
return new ByteArrayResource(body) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Unsupported resource class: " + clazz);
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import org.springframework.core.io.ByteArrayResource;
|
|||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
|
@ -69,8 +70,11 @@ public class ResourceHttpMessageConverterTests {
|
|||
byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg"));
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
|
||||
inputMessage.getHeaders().setContentDisposition(
|
||||
ContentDisposition.builder("attachment").filename("yourlogo.jpg").build());
|
||||
Resource actualResource = converter.read(Resource.class, inputMessage);
|
||||
assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body));
|
||||
assertEquals("yourlogo.jpg", actualResource.getFilename());
|
||||
}
|
||||
|
||||
@Test // SPR-13443
|
||||
|
|
@ -78,9 +82,12 @@ public class ResourceHttpMessageConverterTests {
|
|||
try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) {
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
|
||||
inputMessage.getHeaders().setContentDisposition(
|
||||
ContentDisposition.builder("attachment").filename("yourlogo.jpg").build());
|
||||
Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
|
||||
assertThat(actualResource, instanceOf(InputStreamResource.class));
|
||||
assertThat(actualResource.getInputStream(), is(body));
|
||||
assertEquals("yourlogo.jpg", actualResource.getFilename());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -100,6 +107,7 @@ public class ResourceHttpMessageConverterTests {
|
|||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Resource body = new ClassPathResource("logo.jpg", getClass());
|
||||
converter.write(body, null, outputMessage);
|
||||
|
||||
assertEquals("Invalid content-type", MediaType.IMAGE_JPEG,
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength());
|
||||
|
|
@ -111,23 +119,22 @@ public class ResourceHttpMessageConverterTests {
|
|||
byte[] byteArray = {1, 2, 3};
|
||||
Resource body = new ByteArrayResource(byteArray);
|
||||
converter.write(body, null, outputMessage);
|
||||
|
||||
assertTrue(Arrays.equals(byteArray, outputMessage.getBodyAsBytes()));
|
||||
}
|
||||
|
||||
// SPR-12999
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
@Test // SPR-12999
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeContentNotGettingInputStream() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Resource resource = mock(Resource.class);
|
||||
given(resource.getInputStream()).willThrow(FileNotFoundException.class);
|
||||
|
||||
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
|
||||
|
||||
assertEquals(0, outputMessage.getHeaders().getContentLength());
|
||||
}
|
||||
|
||||
// SPR-12999
|
||||
@Test
|
||||
@Test // SPR-12999
|
||||
public void writeContentNotClosingInputStream() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Resource resource = mock(Resource.class);
|
||||
|
|
@ -135,21 +142,19 @@ public class ResourceHttpMessageConverterTests {
|
|||
given(resource.getInputStream()).willReturn(inputStream);
|
||||
given(inputStream.read(any())).willReturn(-1);
|
||||
doThrow(new NullPointerException()).when(inputStream).close();
|
||||
|
||||
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
|
||||
|
||||
assertEquals(0, outputMessage.getHeaders().getContentLength());
|
||||
}
|
||||
|
||||
// SPR-13620
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
@Test // SPR-13620
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeContentInputStreamThrowingNullPointerException() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Resource resource = mock(Resource.class);
|
||||
InputStream in = mock(InputStream.class);
|
||||
given(resource.getInputStream()).willReturn(in);
|
||||
given(in.read(any())).willThrow(NullPointerException.class);
|
||||
|
||||
converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage);
|
||||
|
||||
assertEquals(0, outputMessage.getHeaders().getContentLength());
|
||||
|
|
|
|||
Loading…
Reference in New Issue