public class HttpClientBuilder extends Object
HttpClient
objects configured with particular settings.
When using the single-arg constructor variant to wrap an existing instance of
HttpClientBuilder
, there are several caveats of which to be aware:
buildClient()
is called:
RequestConfig
ConnectionConfig
This is due to the unfortunate fact that the Apache builder does not currently provide accessor methods to obtain the default instances currently set on the builder. Therefore, if you need to set any default request or connection config parameters which are not exposed by this builder, then you must use the Apache builder directly and may not use this builder.
connectionDisregardTLSCertificate
is set to true
, then any value
previously set via the Apache builder's
HttpClientBuilder.setSSLSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
will be
unconditionally overwritten.connectionProxyHost
, connectionProxyUsername
and
connectionProxyPassword
, then any value previously set via the Apache builder's
HttpClientBuilder.setDefaultCredentialsProvider(CredentialsProvider)
will be
unconditionally overwritten.HttpClientConnectionManager
is set on the Apache builder via
HttpClientBuilder.setConnectionManager(org.apache.http.conn.HttpClientConnectionManager)
,
this supersedes various other properties set on the Apache builder. This includes the following
instances/properties on the Apache builder:
SSLSocketFactory
(LayeredConnectionSocketFactory
)SSLContext
X509HostnameVerifier
SocketConfig
ConnectionConfig
maxConnTotal
maxConnPerRoute
Similarly, the following setters on this builder will become ineffective when a non-null connection manger is set on the Apache builder:
setTLSSocketFactory(LayeredConnectionSocketFactory)
setConnectionDisregardTLSCertificate(boolean)
setSocketBufferSize(int)
setHttpContentCharSet(String)
setMaxConnectionsTotal(int)
setMaxConnectionsPerRoute(int)
Therefore, if you need to explicitly supply a connection manager instance to the Apache builder (for example in order
to be able to use IdleConnectionSweeper
), then you must supply these properties or instances directly to the
connection manager rather than to this builder or the Apache builder.
SSLSocketFactory
on the Apache builder will supersede
the following Apache builder properties:
Modifier and Type | Field and Description |
---|---|
private HttpClientBuilder |
apacheBuilder
The Apache HttpClientBuilder 4.3+ instance over which to layer this builder.
|
private boolean |
connectionCloseAfterResponse
Whether to instruct the server to close the connection after it has sent its response.
|
private boolean |
connectionDisregardTLSCertificate
Whether the SSL/TLS certificates used by the responder should be ignored.
|
private String |
connectionProxyHost
Host name of the HTTP proxy server through which connections will be made.
|
private String |
connectionProxyPassword
Password used to connect to the HTTP proxy server.
|
private int |
connectionProxyPort
Port number of the HTTP proxy server through which connections will be made.
|
private String |
connectionProxyUsername
Username used to connect to the HTTP proxy server.
|
private int |
connectionRequestTimeout
Maximum length of time in milliseconds to wait for a connection to be returned from the connection
manager.
|
private boolean |
connectionStaleCheck
Whether to check a connection for staleness before using.
|
private int |
connectionTimeout
Maximum length of time in milliseconds to wait for the connection to be established.
|
private boolean |
disableAuthCaching
Flag for disabling auth caching.
|
private boolean |
disableAutomaticRetries
Flag for disabling automatic retries.
|
private boolean |
disableConnectionState
Flag for disabling connection state.
|
private boolean |
disableContentCompression
Flag for disabling content compression.
|
private boolean |
disableCookieManagement
Flag for disabling cookie management.
|
private boolean |
disableRedirectHandling
Flag for disabling redirect handling.
|
private String |
httpContentCharSet
Character set used for HTTP entity content.
|
private boolean |
httpFollowRedirects
Whether to follow HTTP redirects.
|
private int |
maxConnectionsPerRoute
Max simultaneous connections per route allowed by the pooling connection manager.
|
private int |
maxConnectionsTotal
Max total simultaneous connections allowed by the pooling connection manager.
|
private List<org.apache.http.HttpRequestInterceptor> |
requestInterceptorsFirst
List of request interceptors to add first.
|
private List<org.apache.http.HttpRequestInterceptor> |
requestInterceptorsLast
List of request interceptors to add last.
|
private List<org.apache.http.HttpResponseInterceptor> |
responseInterceptorsFirst
List of response interceptors to add first.
|
private List<org.apache.http.HttpResponseInterceptor> |
responseInterceptorsLast
List of response interceptors to add last.
|
private HttpRequestRetryHandler |
retryHandler
Handler which determines if a request should be retried after a recoverable exception during execution.
|
private ServiceUnavailableRetryStrategy |
serviceUnavailStrategy
Strategy which determines if a request should be retried given the response from the target server.
|
private int |
socketBufferSize
Socket buffer size in bytes.
|
private InetAddress |
socketLocalAddress
Local IP address used when establishing connections.
|
private int |
socketTimeout
Maximum period inactivity between two consecutive data packets in milliseconds.
|
private List<HttpClientContextHandler> |
staticContextHandlers
List of static context handlers.
|
private LayeredConnectionSocketFactory |
tlsSocketFactory
The TLS socket factory to use.
|
private String |
userAgent
Apache UserAgent.
|
private boolean |
useSystemProperties
Flag for enabling use of system properties.
|
Constructor and Description |
---|
HttpClientBuilder()
Constructor.
|
HttpClientBuilder(HttpClientBuilder builder)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
HttpClient |
buildClient()
Constructs an
HttpClient using the settings of this builder. |
protected void |
decorateApacheBuilder()
Decorate the Apache builder as determined by this builder's parameters.
|
protected HttpClientBuilder |
getApacheBuilder()
Get the Apache
HttpClientBuilder instance over which this builder will be
layered. |
String |
getConnectionProxyHost()
Gets the hostname of the default proxy used when making connection.
|
String |
getConnectionProxyPassword()
Gets the password used when authenticating to the proxy.
|
int |
getConnectionProxyPort()
Gets the port of the default proxy used when making connection.
|
String |
getConnectionProxyUsername()
Gets the username to use when authenticating to the proxy.
|
int |
getConnectionRequestTimeout()
Gets the maximum length of time in milliseconds to wait for a connection to be returned from the connection
manager.
|
int |
getConnectionTimeout()
Gets the maximum length of time in milliseconds to wait for the connection to be established.
|
List<org.apache.http.HttpRequestInterceptor> |
getFirstRequestInterceptors()
Get the list of request interceptors to add first.
|
List<org.apache.http.HttpResponseInterceptor> |
getFirstResponseInterceptors()
Get the list of response interceptors to add first.
|
String |
getHttpContentCharSet()
Gets the character set used with the HTTP entity (body).
|
HttpRequestRetryHandler |
getHttpRequestRetryHandler()
Get the handler which determines if a request should be retried after a recoverable exception during execution.
|
List<org.apache.http.HttpRequestInterceptor> |
getLastRequestInterceptors()
Get the list of request interceptors to add last.
|
List<org.apache.http.HttpResponseInterceptor> |
getLastResponseInterceptors()
Get the list of response interceptors to add last.
|
int |
getMaxConnectionsPerRoute()
Gets the max simultaneous connections per route allowed by the pooling connection manager.
|
int |
getMaxConnectionsTotal()
Gets the max total simultaneous connections allowed by the pooling connection manager.
|
ServiceUnavailableRetryStrategy |
getServiceUnavailableRetryHandler()
Get the handler which determines if a request should be retried given the response from the target server.
|
int |
getSocketBufferSize()
Gets the size of the socket buffer, in bytes, used for request/response buffering.
|
InetAddress |
getSocketLocalAddress()
Gets the local IP address used when making requests.
|
int |
getSocketTimeout()
Gets the maximum period inactivity between two consecutive data packets in milliseconds.
|
List<HttpClientContextHandler> |
getStaticContextHandlers()
Get the list of static
HttpClientContextHandler . |
LayeredConnectionSocketFactory |
getTLSSocketFactory()
Get the TLS socket factory to use.
|
String |
getUserAgent()
Gets user agent.
|
boolean |
isConnectionCloseAfterResponse()
Gets whether to instruct the server to close the connection after it has sent its response.
|
boolean |
isConnectionDisregardTLSCertificate()
Gets whether the responder's SSL/TLS certificate should be ignored.
|
boolean |
isConnectionStalecheck()
Deprecated.
|
boolean |
isConnectionStaleCheck()
Gets whether reused connections are checked if they are closed before being used by the client.
|
boolean |
isDisableAuthCaching()
Get the flag for disabling auth caching.
|
boolean |
isDisableAutomaticRetries()
Get the flag for disabling automatic retries.
|
boolean |
isDisableConnectionState()
Get the flag for disabling connection state.
|
boolean |
isDisableContentCompression()
Get the flag for disabling content compression.
|
boolean |
isDisableCookieManagement()
Get the flag for disabling cookie management.
|
boolean |
isDisableRedirectHandling()
Get the flag for disabling redirect handling.
|
boolean |
isHttpFollowRedirects()
Gets whether HTTP redirects will be followed.
|
boolean |
isUseSystemProperties()
Get the flag enabling use of system properties.
|
private <T> List<T> |
normalizeList(List<T> items)
Normalize and copy the supplied list to remove nulls.
|
void |
resetDefaults()
Resets all builder parameters to their defaults.
|
void |
setConnectionCloseAfterResponse(boolean close)
Sets whether to instruct the server to close the connection after it has sent its response.
|
void |
setConnectionDisregardTLSCertificate(boolean disregard)
Sets whether the responder's SSL/TLS certificate should be ignored.
|
void |
setConnectionProxyHost(String host)
Sets the hostname of the default proxy used when making connection.
|
void |
setConnectionProxyPassword(String password)
Sets the password used when authenticating to the proxy.
|
void |
setConnectionProxyPort(int port)
Sets the port of the default proxy used when making connection.
|
void |
setConnectionProxyUsername(String usename)
Sets the username to use when authenticating to the proxy.
|
void |
setConnectionRequestTimeout(long timeout)
Sets the maximum length of time in milliseconds to wait for a connection to be returned from the connection
manager.
|
void |
setConnectionStalecheck(boolean check)
Deprecated.
|
void |
setConnectionStaleCheck(boolean check)
Sets whether reused connections are checked if they are closed before being used by the client.
|
void |
setConnectionTimeout(long timeout)
Sets the maximum length of time in milliseconds to wait for the connection to be established.
|
void |
setDisableAuthCaching(boolean flag)
Set the flag for disabling auth caching.
|
void |
setDisableAutomaticRetries(boolean flag)
Set the flag for disabling automatic retries.
|
void |
setDisableConnectionState(boolean flag)
Set the flag for disabling connection state.
|
void |
setDisableContentCompression(boolean flag)
Set the flag for disabling content compression.
|
void |
setDisableCookieManagement(boolean flag)
Set the flag for disabling cookie management.
|
void |
setDisableRedirectHandling(boolean flag)
Set the flag for disabling redirect handling.
|
void |
setFirstRequestInterceptors(List<org.apache.http.HttpRequestInterceptor> interceptors)
Set the list of request interceptors to add first.
|
void |
setFirstResponseInterceptors(List<org.apache.http.HttpResponseInterceptor> interceptors)
Set the list of response interceptors to add first.
|
void |
setHttpContentCharSet(String charSet)
Sets the character set used with the HTTP entity (body).
|
void |
setHttpFollowRedirects(boolean followRedirects)
Gets whether HTTP redirects will be followed.
|
void |
setHttpRequestRetryHandler(HttpRequestRetryHandler handler)
Set the handler which determines if a request should be retried after a recoverable exception during execution.
|
void |
setLastRequestInterceptors(List<org.apache.http.HttpRequestInterceptor> interceptors)
Set the list of request interceptors to add last.
|
void |
setLastResponseInterceptors(List<org.apache.http.HttpResponseInterceptor> interceptors)
Set the list of response interceptors to add last.
|
void |
setMaxConnectionsPerRoute(int max)
Sets the max simultaneous connections per route allowed by the pooling connection manager.
|
void |
setMaxConnectionsTotal(int max)
Sets the max total simultaneous connections allowed by the pooling connection manager.
|
void |
setServiceUnavailableRetryHandler(ServiceUnavailableRetryStrategy strategy)
Set the strategy which determines if a request should be retried given the response from the target server.
|
void |
setSocketBufferSize(int size)
Sets size of the socket buffer, in bytes, used for request/response buffering.
|
void |
setSocketLocalAddress(InetAddress address)
Sets the local IP address used when making requests.
|
void |
setSocketLocalAddress(String ipOrHost)
Sets the local IP address used when making requests.
|
void |
setSocketTimeout(long timeout)
Sets the maximum period inactivity between two consecutive data packets in milliseconds.
|
void |
setStaticContextHandlers(List<HttpClientContextHandler> handlers)
Set the list of static
HttpClientContextHandler . |
void |
setTLSSocketFactory(LayeredConnectionSocketFactory factory)
Set the TLS socket factory to use.
|
void |
setUserAgent(String what)
Sets user agent.
|
void |
setUseSystemProperties(boolean flag)
Set the flag enabling use of system properties.
|
private InetAddress socketLocalAddress
@Duration private int socketTimeout
private int socketBufferSize
@Duration private int connectionTimeout
@Duration private int connectionRequestTimeout
private int maxConnectionsTotal
private int maxConnectionsPerRoute
private boolean connectionDisregardTLSCertificate
@Nullable private LayeredConnectionSocketFactory tlsSocketFactory
private boolean connectionCloseAfterResponse
private boolean connectionStaleCheck
@Nullable private String connectionProxyHost
private int connectionProxyPort
@Nullable private String connectionProxyUsername
@Nullable private String connectionProxyPassword
private boolean httpFollowRedirects
@Nullable private String httpContentCharSet
@Nullable private HttpRequestRetryHandler retryHandler
@Nullable private ServiceUnavailableRetryStrategy serviceUnavailStrategy
private boolean disableAuthCaching
private boolean disableAutomaticRetries
private boolean disableConnectionState
private boolean disableContentCompression
private boolean disableCookieManagement
private boolean disableRedirectHandling
private boolean useSystemProperties
private List<org.apache.http.HttpRequestInterceptor> requestInterceptorsFirst
private List<org.apache.http.HttpRequestInterceptor> requestInterceptorsLast
private List<org.apache.http.HttpResponseInterceptor> responseInterceptorsFirst
private List<org.apache.http.HttpResponseInterceptor> responseInterceptorsLast
@Nonnull @NonnullElements private List<HttpClientContextHandler> staticContextHandlers
private HttpClientBuilder apacheBuilder
public HttpClientBuilder()
public HttpClientBuilder(@Nonnull HttpClientBuilder builder)
builder
- the Apache HttpClientBuilder 4.3+ instance over which to layer this builderpublic void resetDefaults()
public int getMaxConnectionsTotal()
public void setMaxConnectionsTotal(int max)
max
- the max total connectionpublic int getMaxConnectionsPerRoute()
public void setMaxConnectionsPerRoute(int max)
max
- the max connections per routepublic InetAddress getSocketLocalAddress()
public void setSocketLocalAddress(InetAddress address)
address
- local IP address used when making requestspublic void setSocketLocalAddress(String ipOrHost) throws UnknownHostException
ipOrHost
- IP address or hostname, never nullUnknownHostException
- thrown if the given IP or hostname can not be resolved@Duration public int getSocketTimeout()
public void setSocketTimeout(@Duration long timeout)
timeout
- maximum period inactivity between two consecutive data packets in millisecondspublic int getSocketBufferSize()
public void setSocketBufferSize(int size)
size
- size of the socket buffer, in bytes, used for request/response buffering; must be greater than 0@Duration public int getConnectionTimeout()
public void setConnectionTimeout(@Duration long timeout)
timeout
- maximum length of time in milliseconds to wait for the connection to be established@Duration public int getConnectionRequestTimeout()
public void setConnectionRequestTimeout(@Duration long timeout)
timeout
- maximum length of time in milliseconds to wait for the connection to be establishedpublic boolean isConnectionDisregardTLSCertificate()
This flag is overridden and ignored if a custom TLS socket factory is specified via
setTLSSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
.
public void setConnectionDisregardTLSCertificate(boolean disregard)
This flag is overridden and ignored if a custom TLS socket factory is specified via
setTLSSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
.
disregard
- whether the responder's SSL/TLS certificate should be ignored@Nullable public LayeredConnectionSocketFactory getTLSSocketFactory()
public void setTLSSocketFactory(@Nullable LayeredConnectionSocketFactory factory)
factory
- the new socket factory, may be nullpublic boolean isConnectionCloseAfterResponse()
public void setConnectionCloseAfterResponse(boolean close)
close
- whether to instruct the server to close the connection after it has sent its responsepublic boolean isConnectionStalecheck()
isConnectionStaleCheck()
public void setConnectionStalecheck(boolean check)
setConnectionStaleCheck(boolean)
check
- whether reused connections are checked if they are closed before being used by the clientpublic boolean isConnectionStaleCheck()
public void setConnectionStaleCheck(boolean check)
check
- whether reused connections are checked if they are closed before being used by the client@Nullable public String getConnectionProxyHost()
public void setConnectionProxyHost(@Nullable String host)
host
- hostname of the default proxy used when making connectionpublic int getConnectionProxyPort()
public void setConnectionProxyPort(int port)
port
- port of the default proxy used when making connection; must be greater than 0 and less than 65536@Nullable public String getConnectionProxyUsername()
public void setConnectionProxyUsername(@Nullable String usename)
usename
- username to use when authenticating to the proxy; may be null@Nullable public String getConnectionProxyPassword()
public void setConnectionProxyPassword(@Nullable String password)
password
- password used when authenticating to the proxy; may be nullpublic boolean isHttpFollowRedirects()
public void setHttpFollowRedirects(boolean followRedirects)
followRedirects
- true if redirects are followed, false otherwise@Nullable public String getHttpContentCharSet()
public void setHttpContentCharSet(@Nullable String charSet)
charSet
- character set used with the HTTP entity (body)public void setUserAgent(@Nullable String what)
what
- what to set. If this is null Apache will use the default.@Nullable public HttpRequestRetryHandler getHttpRequestRetryHandler()
public void setHttpRequestRetryHandler(@Nullable HttpRequestRetryHandler handler)
handler
- handler which determines if a request should be retried@Nullable public ServiceUnavailableRetryStrategy getServiceUnavailableRetryHandler()
public void setServiceUnavailableRetryHandler(@Nullable ServiceUnavailableRetryStrategy strategy)
strategy
- handler which determines if a request should be retriedpublic boolean isDisableAuthCaching()
public void setDisableAuthCaching(boolean flag)
flag
- true if disabled, false if notpublic boolean isDisableAutomaticRetries()
public void setDisableAutomaticRetries(boolean flag)
flag
- true if disabled, false if notpublic boolean isDisableConnectionState()
public void setDisableConnectionState(boolean flag)
flag
- true if disabled, false if notpublic boolean isDisableContentCompression()
public void setDisableContentCompression(boolean flag)
flag
- true if disabled, false if notpublic boolean isDisableCookieManagement()
public void setDisableCookieManagement(boolean flag)
flag
- true if disabled, false if notpublic boolean isDisableRedirectHandling()
public void setDisableRedirectHandling(boolean flag)
flag
- true if disabled, false if notpublic boolean isUseSystemProperties()
public void setUseSystemProperties(boolean flag)
flag
- true if enabled, false if not@Nullable public List<org.apache.http.HttpRequestInterceptor> getFirstRequestInterceptors()
public void setFirstRequestInterceptors(@Nullable List<org.apache.http.HttpRequestInterceptor> interceptors)
interceptors
- the list of interceptors, may be null@Nullable public List<org.apache.http.HttpRequestInterceptor> getLastRequestInterceptors()
public void setLastRequestInterceptors(@Nullable List<org.apache.http.HttpRequestInterceptor> interceptors)
interceptors
- the list of interceptors, may be null@Nullable public List<org.apache.http.HttpResponseInterceptor> getFirstResponseInterceptors()
public void setFirstResponseInterceptors(@Nullable List<org.apache.http.HttpResponseInterceptor> interceptors)
interceptors
- the list of interceptors, may be null@Nullable public List<org.apache.http.HttpResponseInterceptor> getLastResponseInterceptors()
public void setLastResponseInterceptors(@Nullable List<org.apache.http.HttpResponseInterceptor> interceptors)
interceptors
- the list of interceptors, may be null@Nonnull @NonnullElements @NotLive @Unmodifiable public List<HttpClientContextHandler> getStaticContextHandlers()
HttpClientContextHandler
.public void setStaticContextHandlers(@Nullable List<HttpClientContextHandler> handlers)
HttpClientContextHandler
.handlers
- the list of handlers , may be null@Nonnull @NonnullElements private <T> List<T> normalizeList(@Nullable List<T> items)
T
- type of collection to normalizeitems
- the list of items to normalizepublic HttpClient buildClient() throws Exception
HttpClient
using the settings of this builder.Exception
- if there is any problem building the new client instanceprotected void decorateApacheBuilder() throws Exception
Exception
- if there is a problem decorating the Apache builderprotected HttpClientBuilder getApacheBuilder()
HttpClientBuilder
instance over which this builder will be
layered. Subclasses may override to return a specialized subclass.Copyright © 1999–2019 Shibboleth Consortium. All rights reserved.