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