Disable streaming when reading to Resources in RestTemplate
Prior to this commit, the `ResourceHttpMessageConverter` would support converting from an `HttpInputMessage` to a `InputStreamResource`. This is valid when reading resources on the server side, but it's not compatible with the way `RestTemplate` works. The API exposed by `RestOperations` imply that the HTTP server response should be fully consumed and properly closed by the time the `exchange` method returns. In other words, this HTTP client does not support streaming the HTTP response. This commit allows the `ResourceHttpMessageConverter` to be configured to disable read streaming when used in `RestTemplate`. Issue: SPR-14882
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -21,7 +21,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -47,6 +49,9 @@ public class ResourceHttpMessageConverterTests {
|
||||
|
||||
private final ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void canReadResource() {
|
||||
@@ -79,6 +84,17 @@ public class ResourceHttpMessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // SPR-14882
|
||||
public void shouldNotReadInputStreamResource() throws IOException {
|
||||
ResourceHttpMessageConverter noStreamConverter = new ResourceHttpMessageConverter(false);
|
||||
try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
|
||||
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
|
||||
noStreamConverter.read(InputStreamResource.class, inputMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldWriteImageResource() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
|
||||
Reference in New Issue
Block a user