Class HttpComponentsClientHttpRequestFactory
- All Implemented Interfaces:
Closeable
,AutoCloseable
,DisposableBean
,ClientHttpRequestFactory
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.
- Since:
- 3.1
- Author:
- Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll, Juergen Hoeller
-
Constructor Summary
ConstructorDescriptionCreate a new instance of theHttpComponentsClientHttpRequestFactory
with a defaultHttpClient
based on system properties.HttpComponentsClientHttpRequestFactory
(org.apache.http.client.HttpClient httpClient) Create a new instance of theHttpComponentsClientHttpRequestFactory
with the givenHttpClient
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected org.apache.http.protocol.HttpContext
createHttpContext
(HttpMethod httpMethod, URI uri) Template methods that creates aHttpContext
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.createRequest
(URI uri, HttpMethod httpMethod) Create a newClientHttpRequest
for the specified URI and HTTP method.protected org.apache.http.client.config.RequestConfig
createRequestConfig
(Object client) Create a defaultRequestConfig
to use with the given client.void
destroy()
Shutdown hook that closes the underlyingClientConnectionManager
's connection pool, if any.org.apache.http.client.HttpClient
Return theHttpClient
used for synchronous execution.protected org.apache.http.client.config.RequestConfig
mergeRequestConfig
(org.apache.http.client.config.RequestConfig clientConfig) Merge the givenHttpClient
-levelRequestConfig
with the factory-levelRequestConfig
, if necessary.protected void
postProcessHttpRequest
(org.apache.http.client.methods.HttpUriRequest request) Template method that allows for manipulating theHttpUriRequest
before it is returned as part of aHttpComponentsClientHttpRequest
.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 underlyingRequestConfig
.void
setConnectTimeout
(int timeout) Set the connection timeout for the underlyingRequestConfig
.void
setHttpClient
(org.apache.http.client.HttpClient httpClient) Set theHttpClient
used for synchronous execution.void
setHttpContextFactory
(BiFunction<HttpMethod, URI, org.apache.http.protocol.HttpContext> httpContextFactory) Configure a factory to pre-create theHttpContext
for each request.void
setReadTimeout
(int timeout) Set the socket read timeout for the underlyingRequestConfig
.
-
Constructor Details
-
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory()Create a new instance of theHttpComponentsClientHttpRequestFactory
with a defaultHttpClient
based on system properties. -
HttpComponentsClientHttpRequestFactory
public HttpComponentsClientHttpRequestFactory(org.apache.http.client.HttpClient httpClient) Create a new instance of theHttpComponentsClientHttpRequestFactory
with the givenHttpClient
instance.- Parameters:
httpClient
- the HttpClient instance to use for this request factory
-
-
Method Details
-
setHttpClient
public void setHttpClient(org.apache.http.client.HttpClient httpClient) Set theHttpClient
used for synchronous execution. -
getHttpClient
public org.apache.http.client.HttpClient getHttpClient()Return theHttpClient
used for synchronous execution. -
setConnectTimeout
public void setConnectTimeout(int timeout) Set the connection timeout for the underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.This options does not affect connection timeouts for SSL handshakes or CONNECT requests; for that, it is required to use the
SocketConfig
on theHttpClient
itself.- Parameters:
timeout
- the timeout value in milliseconds- See Also:
-
RequestConfig.getConnectTimeout()
SocketConfig.getSoTimeout()
-
setConnectionRequestTimeout
public void setConnectionRequestTimeout(int connectionRequestTimeout) Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance 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 underlyingRequestConfig
. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance 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
-
setHttpContextFactory
public void setHttpContextFactory(BiFunction<HttpMethod, URI, org.apache.http.protocol.HttpContext> httpContextFactory) Configure a factory to pre-create theHttpContext
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 givenRestTemplate
instance as associated for the same client identity.HttpClientContext.setUserToken(Object)
can be used to specify a fixed user token for all requests.- Parameters:
httpContextFactory
- the context factory to use- Since:
- 5.2.7
-
createRequest
Description copied from interface:ClientHttpRequestFactory
Create a newClientHttpRequest
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 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
Create a defaultRequestConfig
to use with the given client. Can returnnull
to indicate that no custom request config should be set and the defaults of theHttpClient
should 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
protected org.apache.http.client.config.RequestConfig mergeRequestConfig(org.apache.http.client.config.RequestConfig clientConfig) Merge the givenHttpClient
-levelRequestConfig
with the factory-levelRequestConfig
, if necessary.- Parameters:
clientConfig
- the config held by the current- Returns:
- the merged request config
- Since:
- 4.2
-
createHttpUriRequest
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.- Parameters:
httpMethod
- the HTTP methoduri
- the URI- Returns:
- the Commons HttpMethodBase object
-
postProcessHttpRequest
protected void postProcessHttpRequest(org.apache.http.client.methods.HttpUriRequest request) Template method that allows for manipulating theHttpUriRequest
before it is returned as part of aHttpComponentsClientHttpRequest
.The default implementation is empty.
- Parameters:
request
- the request to process
-
createHttpContext
@Nullable protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, URI uri) Template methods that creates aHttpContext
for the given HTTP method and URI.The default implementation returns
null
.- Parameters:
httpMethod
- the HTTP methoduri
- the URI- Returns:
- the http context
-
destroy
Shutdown hook that closes the underlyingClientConnectionManager
's connection pool, if any.- Specified by:
destroy
in 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.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceClientHttpRequestFactory
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-