Class HttpComponentsClientHttpRequestFactory

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

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 5.1 or higher, as of Spring 6.0.

Since:
3.1
Author:
Oleg Kalnichevski, Arjen Poutsma, Stephane Nicoll, Juergen Hoeller
  • Constructor Details

    • HttpComponentsClientHttpRequestFactory

      public HttpComponentsClientHttpRequestFactory()
      Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient based on system properties.
    • HttpComponentsClientHttpRequestFactory

      public HttpComponentsClientHttpRequestFactory(HttpClient httpClient)
      Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.
      Parameters:
      httpClient - the HttpClient instance to use for this request factory
  • Method Details

    • setHttpClient

      public void setHttpClient(HttpClient httpClient)
      Set the HttpClient used for synchronous execution.
    • getHttpClient

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

      public void setConnectTimeout(int connectTimeout)
      Set the connection timeout for the underlying 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.

      Parameters:
      connectTimeout - the timeout value in milliseconds
      See Also:
    • setConnectTimeout

      public void setConnectTimeout(Duration connectTimeout)
      Set the connection timeout for the underlying 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.

      Parameters:
      connectTimeout - the timeout as a Duration.
      Since:
      6.1
      See Also:
    • setConnectionRequestTimeout

      public void setConnectionRequestTimeout(int connectionRequestTimeout)
      Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

      Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

      Parameters:
      connectionRequestTimeout - the timeout value to request a connection in milliseconds
      See Also:
    • setConnectionRequestTimeout

      public void setConnectionRequestTimeout(Duration connectionRequestTimeout)
      Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

      Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

      Parameters:
      connectionRequestTimeout - the timeout value to request a connection as a Duration.
      Since:
      6.1
      See Also:
    • setReadTimeout

      public void setReadTimeout(int readTimeout)
      Set the response timeout for the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

      Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

      Parameters:
      readTimeout - the timeout value in milliseconds
      Since:
      6.2
      See Also:
    • setReadTimeout

      public void setReadTimeout(Duration readTimeout)
      Set the response timeout for the underlying RequestConfig. A timeout value of 0 specifies an infinite timeout.

      Additional properties can be configured by specifying a RequestConfig instance on a custom HttpClient.

      Parameters:
      readTimeout - the timeout as a Duration.
      Since:
      6.2
      See Also:
    • setHttpContextFactory

      public void setHttpContextFactory(BiFunction<HttpMethod,URI,org.apache.hc.core5.http.protocol.HttpContext> httpContextFactory)
      Configure a factory to pre-create the 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.

      Parameters:
      httpContextFactory - the context factory to use
      Since:
      5.2.7
    • createRequest

      public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws 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:
      IOException - in case of I/O errors
    • createRequestConfig

      protected @Nullable RequestConfig createRequestConfig(Object client)
      Create a default 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.

      Parameters:
      client - the HttpClient (or HttpAsyncClient) to check
      Returns:
      the actual RequestConfig to use (may be null)
      Since:
      4.2
      See Also:
    • mergeRequestConfig

      protected RequestConfig mergeRequestConfig(RequestConfig clientConfig)
      Merge the given HttpClient-level RequestConfig with the factory-level configuration, if necessary.
      Parameters:
      clientConfig - the config held by the current
      Returns:
      the merged request config
      Since:
      4.2
    • createHttpUriRequest

      protected org.apache.hc.core5.http.ClassicHttpRequest createHttpUriRequest(HttpMethod httpMethod, 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(org.apache.hc.core5.http.ClassicHttpRequest request)
      Template method that allows for manipulating the ClassicHttpRequest before it is returned as part of a HttpComponentsClientHttpRequest.

      The default implementation is empty.

      Parameters:
      request - the request to process
    • createHttpContext

      protected @Nullable org.apache.hc.core5.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, 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 Exception
      Shutdown hook that closes the underlying HttpClientConnectionManager's connection pool, if any.
      Specified by:
      destroy in interface DisposableBean
      Throws:
      Exception - in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.