public class HttpComponentsClientHttpRequestFactory extends java.lang.Object implements ClientHttpRequestFactory, DisposableBean
ClientHttpRequestFactory
implementation that
uses Apache HttpComponents
HttpClient to create requests.
Allows to use a pre-configured HttpClient
instance -
potentially with authentication, HTTP connection pooling, etc.
NOTE: Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
Modifier and Type | Class and Description |
---|---|
private static class |
HttpComponentsClientHttpRequestFactory.HttpDelete
An alternative to
org.apache.http.client.methods.HttpDelete that
extends org.apache.http.client.methods.HttpEntityEnclosingRequestBase
rather than org.apache.http.client.methods.HttpRequestBase and
hence allows HTTP delete with a request body. |
Modifier and Type | Field and Description |
---|---|
private boolean |
bufferRequestBody |
private int |
connectTimeout |
private CloseableHttpClient |
httpClient |
private int |
socketTimeout |
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
protected 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 |
private void |
setLegacyConnectionTimeout(HttpClient client,
int timeout)
Apply the specified connection timeout to deprecated
HttpClient
implementations. |
private void |
setLegacySocketTimeout(HttpClient client,
int timeout)
Apply the specified socket timeout to deprecated
HttpClient
implementations. |
void |
setReadTimeout(int timeout)
Set the socket read timeout for the underlying HttpClient.
|
private CloseableHttpClient httpClient
private int connectTimeout
private int socketTimeout
private boolean bufferRequestBody
public HttpComponentsClientHttpRequestFactory()
HttpComponentsClientHttpRequestFactory
with a default HttpClient
.public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
HttpComponentsClientHttpRequestFactory
with the given HttpClient
instance.
As of Spring Framework 4.0, the given client is expected to be of type
CloseableHttpClient
(requiring HttpClient 4.3+).
httpClient
- the HttpClient instance to use for this request factorypublic void setHttpClient(HttpClient httpClient)
HttpClient
used for
As of Spring Framework 4.0, the given client is expected to be of type
CloseableHttpClient
(requiring HttpClient 4.3+).
public HttpClient getHttpClient()
HttpClient
used for
synchronous execution.public void setConnectTimeout(int timeout)
timeout
- the timeout value in millisecondsprivate void setLegacyConnectionTimeout(HttpClient client, int timeout)
HttpClient
implementations.
As of HttpClient 4.3, default parameters have to be exposed through a
RequestConfig
instance instead of setting the parameters on the
client. Unfortunately, this behavior is not backward-compatible and older
HttpClient
implementations will ignore the RequestConfig
object set in the context.
If the specified client is an older implementation, we set the custom
connection timeout through the deprecated API. Otherwise, we just return
as it is set through RequestConfig
with newer clients.
client
- the client to configuretimeout
- the custom connection timeoutpublic void setReadTimeout(int timeout)
timeout
- the timeout value in millisecondsprivate void setLegacySocketTimeout(HttpClient client, int timeout)
HttpClient
implementations. See setLegacyConnectionTimeout(HttpClient, int)
.client
- the client to configuretimeout
- the custom socket timeoutsetLegacyConnectionTimeout(HttpClient, int)
public void setBufferRequestBody(boolean bufferRequestBody)
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.
public ClientHttpRequest createRequest(java.net.URI uri, HttpMethod httpMethod) throws java.io.IOException
ClientHttpRequestFactory
ClientHttpRequest
for the specified URI and HTTP method.
The returned request can be written to, and then executed by calling
ClientHttpRequest.execute()
.
createRequest
in interface ClientHttpRequestFactory
uri
- the URI to create a request forhttpMethod
- the HTTP method to executejava.io.IOException
- in case of I/O errorsprotected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, java.net.URI uri)
httpMethod
- the HTTP methoduri
- the URIprotected void postProcessHttpRequest(HttpUriRequest request)
HttpUriRequest
before it is
returned as part of a HttpComponentsClientHttpRequest
.
The default implementation is empty.
request
- the request to processprotected HttpContext createHttpContext(HttpMethod httpMethod, java.net.URI uri)
HttpContext
for the given HTTP method and URI.
The default implementation returns null
.
httpMethod
- the HTTP methoduri
- the URIpublic void destroy() throws java.lang.Exception
ClientConnectionManager
's
connection pool, if any.destroy
in interface DisposableBean
java.lang.Exception
- in case of shutdown errors.
Exceptions will get logged but not rethrown to allow
other beans to release their resources too.