Class HttpClientBuilder


  • public class HttpClientBuilder
    extends Object
    A convenience class for building HttpClient instances.

    Among other things,

    • Disables stale connection checks by default
    • Disables Nagle's algorithm
    • Disables cookie management by default

    • Constructor Detail

      • HttpClientBuilder

        public HttpClientBuilder​(com.codahale.metrics.MetricRegistry metricRegistry)
      • HttpClientBuilder

        public HttpClientBuilder​(Environment environment)
    • Method Detail

      • name

        public HttpClientBuilder name​(String environmentName)
        Use the given environment name. This is used in the user agent.
        Parameters:
        environmentName - an environment name to use in the user agent.
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.client5.http.DnsResolver resolver)
        Use the given DnsResolver instance.
        Parameters:
        resolver - a DnsResolver instance
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.client5.http.HttpRequestRetryStrategy httpRequestRetryStrategy)
        Uses the HttpRequestRetryStrategy for handling request retries.
        Parameters:
        httpRequestRetryStrategy - an HttpRequestRetryStrategy
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.core5.http.config.Registry<org.apache.hc.client5.http.socket.ConnectionSocketFactory> registry)
        Use the given Registry instance.
        Parameters:
        registry -
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.client5.http.routing.HttpRoutePlanner routePlanner)
        Use the given HttpRoutePlanner instance.
        Parameters:
        routePlanner - a HttpRoutePlanner instance
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.client5.http.auth.CredentialsStore credentialsStore)
        Use the given CredentialsStore instance.
        Parameters:
        credentialsStore - a CredentialsStore instance
        Returns:
        this
      • using

        public HttpClientBuilder using​(com.codahale.metrics.httpclient5.HttpClientMetricNameStrategy metricNameStrategy)
        Use the given HttpClientMetricNameStrategy instance.
        Parameters:
        metricNameStrategy - a HttpClientMetricNameStrategy instance
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.client5.http.protocol.RedirectStrategy redirectStrategy)
        Use the given RedirectStrategy instance.
        Parameters:
        redirectStrategy - a RedirectStrategy instance
        Returns:
        this
      • using

        public HttpClientBuilder using​(List<? extends org.apache.hc.core5.http.Header> defaultHeaders)
        Use the given default headers for each HTTP request
        Parameters:
        defaultHeaders - HTTP headers
        Returns:
        this
      • using

        public HttpClientBuilder using​(org.apache.hc.core5.http.protocol.HttpProcessor httpProcessor)
        Use the given HttpProcessor instance
        Parameters:
        httpProcessor - a HttpProcessor instance
        Returns:
        this
      • disableContentCompression

        public HttpClientBuilder disableContentCompression​(boolean disableContentCompression)
        Disable support of decompression of responses
        Parameters:
        disableContentCompression - true, if disabled
        Returns:
        this
      • build

        public org.apache.hc.client5.http.impl.classic.CloseableHttpClient build​(String name)
        Builds the HttpClient.
        Parameters:
        name -
        Returns:
        an CloseableHttpClient
      • createRequestExecutor

        protected org.apache.hc.core5.http.impl.io.HttpRequestExecutor createRequestExecutor​(String name)
        Creates a HttpRequestExecutor. Intended for use by subclasses to provide a customized request executor. The default implementation is an InstrumentedHttpRequestExecutor
        Parameters:
        name -
        Returns:
        a HttpRequestExecutor
        Since:
        2.0
      • createBuilder

        protected org.apache.hc.client5.http.impl.classic.HttpClientBuilder createBuilder()
        Creates an Apache HttpClientBuilder. Intended for use by subclasses to create builder instance from subclass of HttpClientBuilder
        Returns:
        an HttpClientBuilder
        Since:
        2.0
      • customizeBuilder

        protected org.apache.hc.client5.http.impl.classic.HttpClientBuilder customizeBuilder​(org.apache.hc.client5.http.impl.classic.HttpClientBuilder builder)
        Configures an Apache HttpClientBuilder. Intended for use by subclasses to inject HttpClientBuilder configuration. The default implementation is an identity function.
      • createClient

        protected ConfiguredCloseableHttpClient createClient​(org.apache.hc.client5.http.impl.classic.HttpClientBuilder builder,
                                                             com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager manager,
                                                             String name)
        Map the parameters in HttpClientConfiguration to configuration on a HttpClientBuilder instance
        Parameters:
        builder -
        manager -
        name -
        Returns:
        the configured CloseableHttpClient
      • createUserAgent

        protected String createUserAgent​(String name)
        Create a user agent string using the configured user agent if defined, otherwise using a combination of the environment name and this client name
        Parameters:
        name - the name of this client
        Returns:
        the user agent string to be used by this client
      • createConnectionManager

        protected com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager createConnectionManager​(org.apache.hc.core5.http.config.Registry<org.apache.hc.client5.http.socket.ConnectionSocketFactory> registry,
                                                                                                                   String name)
        Create a InstrumentedHttpClientConnectionManager based on the HttpClientConfiguration. It sets the maximum connections per route and the maximum total connections that the connection manager can create
        Parameters:
        registry -
        name -
        Returns:
        a InstrumentedHttpClientConnectionManger instance
      • configureConnectionManager

        protected com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager configureConnectionManager​(com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager connectionManager)
      • configureCredentials

        protected org.apache.hc.client5.http.auth.Credentials configureCredentials​(AuthConfiguration auth)
        determine the Credentials implementation to use
        Parameters:
        auth -
        Returns:
        a Credentials instance, either {UsernamePasswordCredentials or NTCredentials}