Package io.dropwizard.client
Class HttpClientBuilder
- java.lang.Object
-
- io.dropwizard.client.HttpClientBuilder
-
public class HttpClientBuilder extends Object
A convenience class for buildingHttpClient
instances.Among other things,
- Disables stale connection checks by default
- Disables Nagle's algorithm
- Disables cookie management by default
-
-
Constructor Summary
Constructors Constructor Description HttpClientBuilder(com.codahale.metrics.MetricRegistry metricRegistry)
HttpClientBuilder(Environment environment)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.apache.hc.client5.http.impl.classic.CloseableHttpClient
build(String name)
Builds theHttpClient
.protected com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager
configureConnectionManager(com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager connectionManager)
protected org.apache.hc.client5.http.auth.Credentials
configureCredentials(AuthConfiguration auth)
determine the Credentials implementation to useprotected org.apache.hc.client5.http.impl.classic.HttpClientBuilder
createBuilder()
Creates an ApacheHttpClientBuilder
.protected ConfiguredCloseableHttpClient
createClient(org.apache.hc.client5.http.impl.classic.HttpClientBuilder builder, com.codahale.metrics.httpclient5.InstrumentedHttpClientConnectionManager manager, String name)
Map the parameters inHttpClientConfiguration
to configuration on aHttpClientBuilder
instanceprotected 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.protected org.apache.hc.core5.http.impl.io.HttpRequestExecutor
createRequestExecutor(String name)
Creates aHttpRequestExecutor
.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 nameprotected org.apache.hc.client5.http.impl.classic.HttpClientBuilder
customizeBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder builder)
Configures an ApacheHttpClientBuilder
.HttpClientBuilder
disableContentCompression(boolean disableContentCompression)
Disable support of decompression of responsesHttpClientBuilder
name(String environmentName)
Use the given environment name.HttpClientBuilder
using(com.codahale.metrics.httpclient5.HttpClientMetricNameStrategy metricNameStrategy)
Use the givenHttpClientMetricNameStrategy
instance.HttpClientBuilder
using(HttpClientConfiguration configuration)
Use the givenHttpClientConfiguration
instance.HttpClientBuilder
using(List<? extends org.apache.hc.core5.http.Header> defaultHeaders)
Use the given default headers for each HTTP requestHttpClientBuilder
using(HostnameVerifier verifier)
Use the givenHostnameVerifier
instance.HttpClientBuilder
using(org.apache.hc.client5.http.auth.CredentialsStore credentialsStore)
Use the givenCredentialsStore
instance.HttpClientBuilder
using(org.apache.hc.client5.http.DnsResolver resolver)
Use the givenDnsResolver
instance.HttpClientBuilder
using(org.apache.hc.client5.http.HttpRequestRetryStrategy httpRequestRetryStrategy)
Uses theHttpRequestRetryStrategy
for handling request retries.HttpClientBuilder
using(org.apache.hc.client5.http.protocol.RedirectStrategy redirectStrategy)
Use the givenRedirectStrategy
instance.HttpClientBuilder
using(org.apache.hc.client5.http.routing.HttpRoutePlanner routePlanner)
Use the givenHttpRoutePlanner
instance.HttpClientBuilder
using(org.apache.hc.core5.http.config.Registry<org.apache.hc.client5.http.socket.ConnectionSocketFactory> registry)
Use the givenRegistry
instance.HttpClientBuilder
using(org.apache.hc.core5.http.protocol.HttpProcessor httpProcessor)
Use the givenHttpProcessor
instance
-
-
-
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(HttpClientConfiguration configuration)
Use the givenHttpClientConfiguration
instance.- Parameters:
configuration
- aHttpClientConfiguration
instance- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.client5.http.DnsResolver resolver)
Use the givenDnsResolver
instance.- Parameters:
resolver
- aDnsResolver
instance- Returns:
this
-
using
public HttpClientBuilder using(HostnameVerifier verifier)
Use the givenHostnameVerifier
instance.- Parameters:
verifier
- aHostnameVerifier
instance- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.client5.http.HttpRequestRetryStrategy httpRequestRetryStrategy)
Uses theHttpRequestRetryStrategy
for handling request retries.- Parameters:
httpRequestRetryStrategy
- anHttpRequestRetryStrategy
- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.core5.http.config.Registry<org.apache.hc.client5.http.socket.ConnectionSocketFactory> registry)
Use the givenRegistry
instance.- Parameters:
registry
-- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.client5.http.routing.HttpRoutePlanner routePlanner)
Use the givenHttpRoutePlanner
instance.- Parameters:
routePlanner
- aHttpRoutePlanner
instance- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.client5.http.auth.CredentialsStore credentialsStore)
Use the givenCredentialsStore
instance.- Parameters:
credentialsStore
- aCredentialsStore
instance- Returns:
this
-
using
public HttpClientBuilder using(com.codahale.metrics.httpclient5.HttpClientMetricNameStrategy metricNameStrategy)
Use the givenHttpClientMetricNameStrategy
instance.- Parameters:
metricNameStrategy
- aHttpClientMetricNameStrategy
instance- Returns:
this
-
using
public HttpClientBuilder using(org.apache.hc.client5.http.protocol.RedirectStrategy redirectStrategy)
Use the givenRedirectStrategy
instance.- Parameters:
redirectStrategy
- aRedirectStrategy
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 givenHttpProcessor
instance- Parameters:
httpProcessor
- aHttpProcessor
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 theHttpClient
.- Parameters:
name
-- Returns:
- an
CloseableHttpClient
-
createRequestExecutor
protected org.apache.hc.core5.http.impl.io.HttpRequestExecutor createRequestExecutor(String name)
Creates aHttpRequestExecutor
. Intended for use by subclasses to provide a customized request executor. The default implementation is anInstrumentedHttpRequestExecutor
- Parameters:
name
-- Returns:
- a
HttpRequestExecutor
- Since:
- 2.0
-
createBuilder
protected org.apache.hc.client5.http.impl.classic.HttpClientBuilder createBuilder()
Creates an ApacheHttpClientBuilder
. Intended for use by subclasses to create builder instance from subclass ofHttpClientBuilder
- 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 ApacheHttpClientBuilder
. 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 inHttpClientConfiguration
to configuration on aHttpClientBuilder
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
orNTCredentials
}
-
-