From 1feb757c54c9f7dedcbf85e0d93fc088ad7cb916 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 8 Sep 2015 11:04:34 +0200 Subject: [PATCH] ResourceHttpMessageConverter allows for using InputStreamResource Issue: SPR-13443 --- .../ResourceHttpMessageConverter.java | 14 ++++++-- .../ResourceHttpMessageConverterTests.java | 32 ++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java index 9eebbbf322..cc8da3601a 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java @@ -40,6 +40,8 @@ import org.springframework.util.StringUtils; * If JAF is not available, {@code application/octet-stream} is used. * * @author Arjen Poutsma + * @author Juergen Hoeller + * @author Kazuki Shimizu * @since 3.0.2 */ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter { @@ -62,8 +64,16 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); - return new ByteArrayResource(body); + if (InputStreamResource.class == clazz){ + return new InputStreamResource(inputMessage.getBody()); + } + else if (clazz.isAssignableFrom(ByteArrayResource.class)) { + byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); + return new ByteArrayResource(body); + } + else { + throw new IllegalStateException("Unsupported resource class: " + clazz); + } } @Override diff --git a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java index 6e57854f2e..10752f22ea 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2015 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. @@ -17,32 +17,32 @@ package org.springframework.http.converter; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; -import org.junit.Before; import org.junit.Test; 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.MediaType; import org.springframework.http.MockHttpInputMessage; import org.springframework.http.MockHttpOutputMessage; import org.springframework.util.FileCopyUtils; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsInstanceOf.*; import static org.junit.Assert.*; /** * @author Arjen Poutsma + * @author Kazuki Shimizu */ public class ResourceHttpMessageConverterTests { - private ResourceHttpMessageConverter converter; + private final ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter(); - @Before - public void setUp() { - converter = new ResourceHttpMessageConverter(); - } @Test public void canRead() { @@ -60,7 +60,19 @@ public class ResourceHttpMessageConverterTests { byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg")); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); - converter.read(Resource.class, inputMessage); + Resource actualResource = converter.read(Resource.class, inputMessage); + assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body)); + } + + @Test // SPR-13443 + public void readWithInputStreamResource() throws IOException { + try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); + inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); + Resource actualResource = converter.read(InputStreamResource.class, inputMessage); + assertThat(actualResource, instanceOf(InputStreamResource.class)); + assertThat(actualResource.getInputStream(), is(body)); + } } @Test @@ -73,9 +85,7 @@ public class ResourceHttpMessageConverterTests { assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength()); } - // SPR-10848 - - @Test + @Test // SPR-10848 public void writeByteArrayNullMediaType() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); byte[] byteArray = {1, 2, 3};