1
votes

We are using RestEasy Client version 3.0*.
We are trying to post very large content in the request body.
When the content length is less than 1 MB the following code is running correctly.
When the content length is very large (about 500 MB) the request is getting stuck.
The code is looking like this:

InputStream inputStream = new FileInputStream(tempFile);
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("<SomeUrl>");
response = target.request(MediaType.WILDCARD).post(Entity.entity(inputStream, MediaType.WILDCARD));

How should I change the code to support large content?

After a week...
Since nobody answered me - I used the good old Spring solution (see below).
Is Spring better than RestEasy client?

InputStream inputStream = new FileInputStream(tempFile);
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);
RestTemplate r = new RestTemplate(factory);

RequestCallback requestCallback = new RequestCallback() {
     @Override
     public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
         List<org.springframework.http.MediaType> MEDIA_TYPES = Collections.unmodifiableList(Arrays.asList(org.springframework.http.MediaType.ALL));
         clientHttpRequest.getHeaders().setAccept(MEDIA_TYPES);
         OutputStream requestOutputStream = clientHttpRequest.getBody();
         try {
             int copiedBytes = IOUtils.copy(inputStream, requestOutputStream);
         } finally {
             IOUtils.closeQuietly(requestOutputStream);
         }
      }
};

ResponseExtractor<Response> responseExtractor = new ResponseExtractor<Response>() {
    @Override
    public Response extractData(ClientHttpResponse response) throws IOException {
        return Response.status(response.getRawStatusCode()).build();
    }
};

response = r.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
1
I have the same question...wikier

1 Answers

0
votes

I guess in Spring 4.0.1 you can use Apache HTTP client and HttpComponentsClientHttpRequestFactory instead of SimpleClientHttpRequestFactory. Here is the original note: SPR-10728

<bean id="restTemplateStreaming" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
            <property name="bufferRequestBody" value="false" />
            <property name="httpClient">
                <bean class="com.spotfire.server.http.spring.HttpClientFactory">
                    <property name="clientBuilder">
                        <bean class="org.apache.http.impl.client.HttpClientBuilder">
                            <property name="connectionManager" ref="poolingHttpClientConnectionManager" />
                            <property name="redirectStrategy">
                                <bean class="org.apache.http.impl.client.LaxRedirectStrategy" />
                            </property>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </constructor-arg>      
</bean>

Note the However callback implementation changes as well. You no longer can call clientHttpRequest.getBody(); Instead you have to use a bit convoluted structure as shown below:

class StreamingRequestCallback implements RequestCallback {

private final InputStream inputStream;

StreamingRequestCallback(InputStream inputStream) {
  this.inputStream = inputStream;
}

@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
  request.getHeaders().add("Content-type", MediaType.APPLICATION_OCTET_STREAM_VALUE);

  ((StreamingHttpOutputMessage) request).setBody(new StreamingHttpOutputMessage.Body() {
    @Override
    public void writeTo(final OutputStream outputStream) throws IOException {
      IOUtils.copy(inputStream, outputStream);
    }
  });
}

};