public class HttpComponentsClientHttpRequestFactory extends 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.
Constructor and Description |
---|
HttpComponentsClientHttpRequestFactory()
Create a new instance of the
HttpComponentsClientHttpRequestFactory
with a default HttpClient based on system properties. |
HttpComponentsClientHttpRequestFactory(org.apache.http.client.HttpClient httpClient)
Create a new instance of the
HttpComponentsClientHttpRequestFactory
with the given HttpClient instance. |
Modifier and Type | Method and Description |
---|---|
protected org.apache.http.protocol.HttpContext |
createHttpContext(HttpMethod httpMethod,
URI uri)
Template methods that creates a
HttpContext for the given HTTP method and URI. |
protected org.apache.http.client.methods.HttpUriRequest |
createHttpUriRequest(HttpMethod httpMethod,
URI uri)
Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
|
ClientHttpRequest |
createRequest(URI uri,
HttpMethod httpMethod)
Create a new
ClientHttpRequest for the specified URI and HTTP method. |
protected org.apache.http.client.config.RequestConfig |
createRequestConfig(Object client)
Create a default
RequestConfig to use with the given client. |
void |
destroy()
Shutdown hook that closes the underlying
ClientConnectionManager 's
connection pool, if any. |
org.apache.http.client.HttpClient |
getHttpClient()
Return the
HttpClient used for
synchronous execution. |
protected org.apache.http.client.config.RequestConfig |
mergeRequestConfig(org.apache.http.client.config.RequestConfig clientConfig)
Merge the given
HttpClient -level RequestConfig with
the factory-level RequestConfig , if necessary. |
protected void |
postProcessHttpRequest(org.apache.http.client.methods.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 |
setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection
from the connection manager using the underlying
RequestConfig . |
void |
setConnectTimeout(int timeout)
Set the connection timeout for the underlying
RequestConfig . |
void |
setHttpClient(org.apache.http.client.HttpClient httpClient)
Set the
HttpClient used for
synchronous execution. |
void |
setHttpContextFactory(BiFunction<HttpMethod,URI,org.apache.http.protocol.HttpContext> httpContextFactory)
Configure a factory to pre-create the
HttpContext for each request. |
void |
setReadTimeout(int timeout)
Set the socket read timeout for the underlying
RequestConfig . |
public HttpComponentsClientHttpRequestFactory()
HttpComponentsClientHttpRequestFactory
with a default HttpClient
based on system properties.public HttpComponentsClientHttpRequestFactory(org.apache.http.client.HttpClient httpClient)
HttpComponentsClientHttpRequestFactory
with the given HttpClient
instance.httpClient
- the HttpClient instance to use for this request factorypublic void setHttpClient(org.apache.http.client.HttpClient httpClient)
HttpClient
used for
synchronous execution.public org.apache.http.client.HttpClient getHttpClient()
HttpClient
used for
synchronous execution.public void setConnectTimeout(int timeout)
RequestConfig
.
A timeout value of 0 specifies an infinite timeout.
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
This options does not affect connection timeouts for SSL
handshakes or CONNECT requests; for that, it is required to
use the SocketConfig
on the
HttpClient
itself.
timeout
- the timeout value in millisecondsRequestConfig.getConnectTimeout()
,
SocketConfig.getSoTimeout()
public void setConnectionRequestTimeout(int connectionRequestTimeout)
RequestConfig
.
A timeout value of 0 specifies an infinite timeout.
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
connectionRequestTimeout
- the timeout value to request a connection in millisecondsRequestConfig.getConnectionRequestTimeout()
public void setReadTimeout(int timeout)
RequestConfig
.
A timeout value of 0 specifies an infinite timeout.
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
timeout
- the timeout value in millisecondsRequestConfig.getSocketTimeout()
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 void setHttpContextFactory(BiFunction<HttpMethod,URI,org.apache.http.protocol.HttpContext> httpContextFactory)
HttpContext
for each request.
This may be useful for example in mutual TLS authentication where a
different RestTemplate
for each client certificate such that
all calls made through a given RestTemplate
instance as associated
for the same client identity. HttpClientContext.setUserToken(Object)
can be used to specify a fixed user token for all requests.
httpContextFactory
- the context factory to usepublic ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws 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 executeIOException
- in case of I/O errors@Nullable protected org.apache.http.client.config.RequestConfig createRequestConfig(Object client)
RequestConfig
to use with the given client.
Can return null
to indicate that no custom request config should
be set and the defaults of the HttpClient
should be used.
The default implementation tries to merge the defaults of the client with the local customizations of this factory instance, if any.
client
- the HttpClient
(or HttpAsyncClient
) to checknull
)mergeRequestConfig(RequestConfig)
protected org.apache.http.client.config.RequestConfig mergeRequestConfig(org.apache.http.client.config.RequestConfig clientConfig)
HttpClient
-level RequestConfig
with
the factory-level RequestConfig
, if necessary.clientConfig
- the config held by the currentprotected org.apache.http.client.methods.HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri)
httpMethod
- the HTTP methoduri
- the URIprotected void postProcessHttpRequest(org.apache.http.client.methods.HttpUriRequest request)
HttpUriRequest
before it is
returned as part of a HttpComponentsClientHttpRequest
.
The default implementation is empty.
request
- the request to process@Nullable protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, URI uri)
HttpContext
for the given HTTP method and URI.
The default implementation returns null
.
httpMethod
- the HTTP methoduri
- the URIpublic void destroy() throws Exception
ClientConnectionManager
's
connection pool, if any.destroy
in interface DisposableBean
Exception
- in case of shutdown errors. Exceptions will get logged
but not rethrown to allow other beans to release their resources as well.