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:
Brian Clozel
2017-01-05 14:48:35 +01:00
parent 848a2b2ef8
commit afd93a0b52
3 changed files with 39 additions and 5 deletions

View File

@@ -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.
@@ -49,9 +49,27 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
private static final boolean jafPresent = ClassUtils.isPresent(
"javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
private final boolean supportsReadStreaming;
/**
* Create a new instance of the {@code ResourceHttpMessageConverter}
* that supports read streaming, i.e. can convert an
* {@code HttpInputMessage} to {@code InputStreamResource}.
*/
public ResourceHttpMessageConverter() {
super(MediaType.ALL);
this.supportsReadStreaming = true;
}
/**
* Create a new instance of the {@code ResourceHttpMessageConverter}
* @param supportsReadStreaming whether the converter should support
* read streaming, i.e. convert to {@code InputStreamResource}.
* @since 5.0
*/
public ResourceHttpMessageConverter(boolean supportsReadStreaming) {
super(MediaType.ALL);
this.supportsReadStreaming = supportsReadStreaming;
}
@@ -64,7 +82,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
if (InputStreamResource.class == clazz) {
if (supportsReadStreaming && InputStreamResource.class == clazz) {
return new InputStreamResource(inputMessage.getBody());
}
else if (clazz.isAssignableFrom(ByteArrayResource.class)) {

View File

@@ -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.
@@ -161,7 +161,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
this.messageConverters.add(new ResourceHttpMessageConverter());
this.messageConverters.add(new ResourceHttpMessageConverter(false));
this.messageConverters.add(new SourceHttpMessageConverter<>());
this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());