Skip to content

InterceptingClientHttpRequest is always buffered despite the delegate [SPR-16380] #20926

@spring-projects-issues

Description

@spring-projects-issues

Wyatt Smith opened SPR-16380 and commented

When writing an application, I had to upload a large file (~600 MB) using the RestTemplate, as well as use an oAuth2 bearer header. To do this, I added a custom interceptor to write the oauth header:

restTemplate.getInterceptors().add(new OauthAuthorizationInterceptor(accessToken));

Because the file is so large, I was getting an OutOfMemoryException, so I disabled buffering:

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate = new RestTemplate(requestFactory);

The application continued getting the memory exception. Upon investigation, it appears as though the ClientHttpRequestFactory is wrapped by an InterceptingClientHttpRequestFactory because of the interceptor (InterceptingHttpAccessor#getRequestFactory). InterceptingClientHttpRequestFactory.getRequestFactory() returns a InterceptingClientHttpRequest, which is a AbstractBufferingClientHttpRequest (InterceptingClientHttpRequestFactory#createRequest). Despite requestFactory.setBufferRequestBody(false), a buffered request is made.

My temporary solution is to manually add the Authorization header everywhere and eliminate the interceptor so InterceptingHttpAccessor#getRequestFactory returns my original HttpComponentsClientHttpRequestFactory with buffering disabled instead of a InterceptingClientHttpRequestFactory wrapping it.

The long term solution should be for the InterceptingClientHttpRequestFactory to be allowed to be streaming instead of buffered.


Affects: 4.3.9

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions