Spring for Android

org.springframework.http.client
Class HttpComponentsClientHttpRequestFactory

java.lang.Object
  extended by org.springframework.http.client.HttpComponentsClientHttpRequestFactory
All Implemented Interfaces:
DisposableBean, ClientHttpRequestFactory

public class HttpComponentsClientHttpRequestFactory
extends java.lang.Object
implements ClientHttpRequestFactory, DisposableBean

ClientHttpRequestFactory implementation that uses Apache HttpComponents Android HttpClient to create requests.

Allows to use a pre-configured HttpClient instance - potentially with authentication, HTTP connection pooling, etc.

NOTE: Requires Apache HttpComponents Android HttpClient 4.3 or higher.

Since:
1.0
Author:
Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll

Constructor Summary
HttpComponentsClientHttpRequestFactory()
          Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient.
HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
          Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.
 
Method Summary
protected  org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, java.net.URI uri)
          Template methods that creates a HttpContext for the given HTTP method and URI.
protected  HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, java.net.URI uri)
          Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
 ClientHttpRequest createRequest(java.net.URI uri, HttpMethod httpMethod)
          Create a new ClientHttpRequest for the specified URI and HTTP method.
 void destroy()
          Shutdown hook that closes the underlying ClientConnectionManager's connection pool, if any.
 HttpClient getHttpClient()
          Return the HttpClient used for synchronous execution.
protected  void postProcessHttpRequest(HttpUriRequest request)
          Template method that allows for manipulating the HttpUriRequest before it is returned as part of a HttpComponentsClientHttpRequest.
 void setBufferRequestBody(boolean bufferRequestBody)
          Indicates whether this request factory should buffer the request body internally.
 void setConnectTimeout(int timeout)
          Set the connection timeout for the underlying HttpClient.
 void setHttpClient(HttpClient httpClient)
          Set the HttpClient used for
 void setReadTimeout(int timeout)
          Set the socket read timeout for the underlying HttpClient.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpComponentsClientHttpRequestFactory

public HttpComponentsClientHttpRequestFactory()
Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient.


HttpComponentsClientHttpRequestFactory

public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.

As of Spring for Android 2.0, the given client is expected to be of type CloseableHttpClient (requiring Android HttpClient 4.3+).

Parameters:
httpClient - the HttpClient instance to use for this request factory
Method Detail

setHttpClient

public void setHttpClient(HttpClient httpClient)
Set the HttpClient used for

As of Spring Framework 4.0, the given client is expected to be of type CloseableHttpClient (requiring HttpClient 4.3+).


getHttpClient

public HttpClient getHttpClient()
Return the HttpClient used for synchronous execution.


setConnectTimeout

public void setConnectTimeout(int timeout)
Set the connection timeout for the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.

Parameters:
timeout - the timeout value in milliseconds

setReadTimeout

public void setReadTimeout(int timeout)
Set the socket read timeout for the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.

Parameters:
timeout - the timeout value in milliseconds

setBufferRequestBody

public void setBufferRequestBody(boolean bufferRequestBody)
Indicates whether this request factory should buffer the request body internally.

Default is true. When sending large amounts of data via POST or PUT, it is recommended to change this property to false, so as not to run out of memory.


createRequest

public ClientHttpRequest createRequest(java.net.URI uri,
                                       HttpMethod httpMethod)
                                throws java.io.IOException
Description copied from interface: ClientHttpRequestFactory
Create a new ClientHttpRequest for the specified URI and HTTP method.

The returned request can be written to, and then executed by calling ClientHttpRequest.execute().

Specified by:
createRequest in interface ClientHttpRequestFactory
Parameters:
uri - the URI to create a request for
httpMethod - the HTTP method to execute
Returns:
the created request
Throws:
java.io.IOException - in case of I/O errors

createHttpUriRequest

protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod,
                                              java.net.URI uri)
Create a Commons HttpMethodBase object for the given HTTP method and URI specification.

Parameters:
httpMethod - the HTTP method
uri - the URI
Returns:
the Commons HttpMethodBase object

postProcessHttpRequest

protected void postProcessHttpRequest(HttpUriRequest request)
Template method that allows for manipulating the HttpUriRequest before it is returned as part of a HttpComponentsClientHttpRequest.

The default implementation is empty.

Parameters:
request - the request to process

createHttpContext

protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod,
                                                                 java.net.URI uri)
Template methods that creates a HttpContext for the given HTTP method and URI.

The default implementation returns null.

Parameters:
httpMethod - the HTTP method
uri - the URI
Returns:
the http context

destroy

public void destroy()
             throws java.lang.Exception
Shutdown hook that closes the underlying ClientConnectionManager's connection pool, if any.

Specified by:
destroy in interface DisposableBean
Throws:
java.lang.Exception - in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources too.

Spring for Android