Class HttpComponentsClientHttpRequestFactory
- java.lang.Object
- org.springframework.http.client.HttpComponentsClientHttpRequestFactory
- All Implemented Interfaces:
DisposableBean,ClientHttpRequestFactory
- Direct Known Subclasses:
HttpComponentsAsyncClientHttpRequestFactory
public class HttpComponentsClientHttpRequestFactory extends Object implements ClientHttpRequestFactory, DisposableBean
ClientHttpRequestFactoryimplementation that uses Apache HttpComponents HttpClient to create requests.Allows to use a pre-configured
HttpClientinstance - potentially with authentication, HTTP connection pooling, etc.NOTE: Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
- Since:
- 3.1
- Author:
- Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll, Juergen Hoeller
Constructor Summary
Constructors Constructor Description HttpComponentsClientHttpRequestFactory()Create a new instance of theHttpComponentsClientHttpRequestFactorywith a defaultHttpClient.HttpComponentsClientHttpRequestFactory(HttpClient httpClient)Create a new instance of theHttpComponentsClientHttpRequestFactorywith the givenHttpClientinstance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.apache.http.protocol.HttpContextcreateHttpContext(HttpMethod httpMethod, URI uri)Template methods that creates aHttpContextfor the given HTTP method and URI.protected HttpUriRequestcreateHttpUriRequest(HttpMethod httpMethod, URI uri)Create a Commons HttpMethodBase object for the given HTTP method and URI specification.ClientHttpRequestcreateRequest(URI uri, HttpMethod httpMethod)Create a newClientHttpRequestfor the specified URI and HTTP method.protected RequestConfigcreateRequestConfig(Object client)Create a defaultRequestConfigto use with the given client.voiddestroy()Shutdown hook that closes the underlyingClientConnectionManager's connection pool, if any.HttpClientgetHttpClient()Return theHttpClientused for synchronous execution.protected RequestConfigmergeRequestConfig(RequestConfig clientConfig)protected voidpostProcessHttpRequest(HttpUriRequest request)Template method that allows for manipulating theHttpUriRequestbefore it is returned as part of aHttpComponentsClientHttpRequest.voidsetBufferRequestBody(boolean bufferRequestBody)Indicates whether this request factory should buffer the request body internally.voidsetConnectionRequestTimeout(int connectionRequestTimeout)Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying HttpClient.voidsetConnectTimeout(int timeout)Set the connection timeout for the underlying HttpClient.voidsetHttpClient(HttpClient httpClient)Set theHttpClientused for synchronous execution.voidsetReadTimeout(int timeout)Set the socket read timeout for the underlying HttpClient.
Constructor Detail
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory()
Create a new instance of theHttpComponentsClientHttpRequestFactorywith a defaultHttpClient.
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
Create a new instance of theHttpComponentsClientHttpRequestFactorywith the givenHttpClientinstance.- Parameters:
httpClient- the HttpClient instance to use for this request factory
Method Detail
setHttpClient
public void setHttpClient(HttpClient httpClient)
Set theHttpClientused for synchronous execution.
getHttpClient
public HttpClient getHttpClient()
Return theHttpClientused 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.Additional properties can be configured by specifying a
RequestConfiginstance on a customHttpClient.- Parameters:
timeout- the timeout value in milliseconds- See Also:
RequestConfig.getConnectTimeout()
setConnectionRequestTimeout
public void setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfiginstance on a customHttpClient.- Parameters:
connectionRequestTimeout- the timeout value to request a connection in milliseconds- See Also:
RequestConfig.getConnectionRequestTimeout()
setReadTimeout
public void setReadTimeout(int timeout)
Set the socket read timeout for the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfiginstance on a customHttpClient.- Parameters:
timeout- the timeout value in milliseconds- See Also:
RequestConfig.getSocketTimeout()
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 tofalse, so as not to run out of memory.- Since:
- 4.0
createRequest
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException
Description copied from interface:ClientHttpRequestFactoryCreate a newClientHttpRequestfor the specified URI and HTTP method.The returned request can be written to, and then executed by calling
ClientHttpRequest.execute().- Specified by:
createRequestin interfaceClientHttpRequestFactory- Parameters:
uri- the URI to create a request forhttpMethod- the HTTP method to execute- Returns:
- the created request
- Throws:
IOException- in case of I/O errors
createRequestConfig
protected RequestConfig createRequestConfig(Object client)
Create a defaultRequestConfigto use with the given client. Can returnnullto indicate that no custom request config should be set and the defaults of theHttpClientshould be used.The default implementation tries to merge the defaults of the client with the local customizations of this factory instance, if any.
- Parameters:
client- theHttpClient(orHttpAsyncClient) to check- Returns:
- the actual RequestConfig to use (may be
null) - Since:
- 4.2
- See Also:
mergeRequestConfig(RequestConfig)
mergeRequestConfig
protected RequestConfig mergeRequestConfig(RequestConfig clientConfig)
- Parameters:
clientConfig- the config held by the current- Returns:
- the merged request config
- Since:
- 4.2
createHttpUriRequest
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri)
Create a Commons HttpMethodBase object for the given HTTP method and URI specification.- Parameters:
httpMethod- the HTTP methoduri- the URI- Returns:
- the Commons HttpMethodBase object
postProcessHttpRequest
protected void postProcessHttpRequest(HttpUriRequest request)
Template method that allows for manipulating theHttpUriRequestbefore it is returned as part of aHttpComponentsClientHttpRequest.The default implementation is empty.
- Parameters:
request- the request to process
createHttpContext
protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, URI uri)
Template methods that creates aHttpContextfor the given HTTP method and URI.The default implementation returns
null.- Parameters:
httpMethod- the HTTP methoduri- the URI- Returns:
- the http context
destroy
public void destroy() throws Exception
Shutdown hook that closes the underlyingClientConnectionManager's connection pool, if any.- Specified by:
destroyin interfaceDisposableBean- Throws:
Exception- in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.