Skip to content

Commit 1feb757

Browse files
committed
ResourceHttpMessageConverter allows for using InputStreamResource
Issue: SPR-13443
1 parent e393c7b commit 1feb757

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
* If JAF is not available, {@code application/octet-stream} is used.
4141
*
4242
* @author Arjen Poutsma
43+
* @author Juergen Hoeller
44+
* @author Kazuki Shimizu
4345
* @since 3.0.2
4446
*/
4547
public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<Resource> {
@@ -62,8 +64,16 @@ protected boolean supports(Class<?> clazz) {
6264
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
6365
throws IOException, HttpMessageNotReadableException {
6466

65-
byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
66-
return new ByteArrayResource(body);
67+
if (InputStreamResource.class == clazz){
68+
return new InputStreamResource(inputMessage.getBody());
69+
}
70+
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
71+
byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody());
72+
return new ByteArrayResource(body);
73+
}
74+
else {
75+
throw new IllegalStateException("Unsupported resource class: " + clazz);
76+
}
6777
}
6878

6979
@Override

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,32 +17,32 @@
1717
package org.springframework.http.converter;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.util.Arrays;
2122

22-
import org.junit.Before;
2323
import org.junit.Test;
2424

2525
import org.springframework.core.io.ByteArrayResource;
2626
import org.springframework.core.io.ClassPathResource;
27+
import org.springframework.core.io.InputStreamResource;
2728
import org.springframework.core.io.Resource;
2829
import org.springframework.http.MediaType;
2930
import org.springframework.http.MockHttpInputMessage;
3031
import org.springframework.http.MockHttpOutputMessage;
3132
import org.springframework.util.FileCopyUtils;
3233

34+
import static org.hamcrest.core.Is.*;
35+
import static org.hamcrest.core.IsInstanceOf.*;
3336
import static org.junit.Assert.*;
3437

3538
/**
3639
* @author Arjen Poutsma
40+
* @author Kazuki Shimizu
3741
*/
3842
public class ResourceHttpMessageConverterTests {
3943

40-
private ResourceHttpMessageConverter converter;
44+
private final ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter();
4145

42-
@Before
43-
public void setUp() {
44-
converter = new ResourceHttpMessageConverter();
45-
}
4646

4747
@Test
4848
public void canRead() {
@@ -60,7 +60,19 @@ public void read() throws IOException {
6060
byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg"));
6161
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
6262
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
63-
converter.read(Resource.class, inputMessage);
63+
Resource actualResource = converter.read(Resource.class, inputMessage);
64+
assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body));
65+
}
66+
67+
@Test // SPR-13443
68+
public void readWithInputStreamResource() throws IOException {
69+
try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) {
70+
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
71+
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
72+
Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
73+
assertThat(actualResource, instanceOf(InputStreamResource.class));
74+
assertThat(actualResource.getInputStream(), is(body));
75+
}
6476
}
6577

6678
@Test
@@ -73,9 +85,7 @@ public void write() throws IOException {
7385
assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength());
7486
}
7587

76-
// SPR-10848
77-
78-
@Test
88+
@Test // SPR-10848
7989
public void writeByteArrayNullMediaType() throws IOException {
8090
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
8191
byte[] byteArray = {1, 2, 3};

0 commit comments

Comments
 (0)