Browse Source

ResourceHttpMessageConverter allows for using InputStreamResource

Issue: SPR-13443
pull/860/merge
Juergen Hoeller 9 years ago
parent
commit
1feb757c54
  1. 14
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java
  2. 32
      spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java

14
spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

@ -40,6 +40,8 @@ import org.springframework.util.StringUtils; @@ -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<Resource> {
@ -62,8 +64,16 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -62,8 +64,16 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
protected Resource readInternal(Class<? extends Resource> 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

32
spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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 { @@ -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};

Loading…
Cancel
Save