com.fitbur.apache.http.impl.client.HttpClientBuilder Maven / Gradle / Ivy
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in com.fitburpliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
package com.fitbur.apache.http.impl.client;
import java.io.Closeable;
import java.net.ProxySelector;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import com.fitbur.apache.http.ConnectionReuseStrategy;
import com.fitbur.apache.http.Header;
import com.fitbur.apache.http.HttpHost;
import com.fitbur.apache.http.HttpRequestInterceptor;
import com.fitbur.apache.http.HttpResponseInterceptor;
import com.fitbur.apache.http.annotation.NotThreadSafe;
import com.fitbur.apache.http.auth.AuthSchemeProvider;
import com.fitbur.apache.http.client.AuthenticationStrategy;
import com.fitbur.apache.http.client.BackoffManager;
import com.fitbur.apache.http.client.ConnectionBackoffStrategy;
import com.fitbur.apache.http.client.CookieStore;
import com.fitbur.apache.http.client.CredentialsProvider;
import com.fitbur.apache.http.client.HttpRequestRetryHandler;
import com.fitbur.apache.http.client.RedirectStrategy;
import com.fitbur.apache.http.client.ServiceUnavailableRetryStrategy;
import com.fitbur.apache.http.client.UserTokenHandler;
import com.fitbur.apache.http.client.config.AuthSchemes;
import com.fitbur.apache.http.client.config.CookieSpecs;
import com.fitbur.apache.http.client.config.RequestConfig;
import com.fitbur.apache.http.client.protocol.RequestAcceptEncoding;
import com.fitbur.apache.http.client.protocol.RequestAddCookies;
import com.fitbur.apache.http.client.protocol.RequestAuthCache;
import com.fitbur.apache.http.client.protocol.RequestClientConnControl;
import com.fitbur.apache.http.client.protocol.RequestDefaultHeaders;
import com.fitbur.apache.http.client.protocol.RequestExpectContinue;
import com.fitbur.apache.http.client.protocol.ResponseContentEncoding;
import com.fitbur.apache.http.client.protocol.ResponseProcessCookies;
import com.fitbur.apache.http.config.ConnectionConfig;
import com.fitbur.apache.http.config.Lookup;
import com.fitbur.apache.http.config.RegistryBuilder;
import com.fitbur.apache.http.config.SocketConfig;
import com.fitbur.apache.http.conn.ConnectionKeepAliveStrategy;
import com.fitbur.apache.http.conn.HttpClientConnectionManager;
import com.fitbur.apache.http.conn.SchemePortResolver;
import com.fitbur.apache.http.conn.routing.HttpRoutePlanner;
import com.fitbur.apache.http.conn.socket.ConnectionSocketFactory;
import com.fitbur.apache.http.conn.socket.LayeredConnectionSocketFactory;
import com.fitbur.apache.http.conn.socket.PlainConnectionSocketFactory;
import com.fitbur.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fitbur.apache.http.conn.ssl.SSLContexts;
import com.fitbur.apache.http.conn.ssl.X509HostnameVerifier;
import com.fitbur.apache.http.cookie.CookieSpecProvider;
import com.fitbur.apache.http.impl.DefaultConnectionReuseStrategy;
import com.fitbur.apache.http.impl.NoConnectionReuseStrategy;
import com.fitbur.apache.http.impl.auth.BasicSchemeFactory;
import com.fitbur.apache.http.impl.auth.DigestSchemeFactory;
import com.fitbur.apache.http.impl.auth.KerberosSchemeFactory;
import com.fitbur.apache.http.impl.auth.NTLMSchemeFactory;
import com.fitbur.apache.http.impl.auth.SPNegoSchemeFactory;
import com.fitbur.apache.http.impl.conn.DefaultProxyRoutePlanner;
import com.fitbur.apache.http.impl.conn.DefaultRoutePlanner;
import com.fitbur.apache.http.impl.conn.DefaultSchemePortResolver;
import com.fitbur.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import com.fitbur.apache.http.impl.conn.SystemDefaultRoutePlanner;
import com.fitbur.apache.http.impl.cookie.BestMatchSpecFactory;
import com.fitbur.apache.http.impl.cookie.BrowserCompatSpecFactory;
import com.fitbur.apache.http.impl.cookie.IgnoreSpecFactory;
import com.fitbur.apache.http.impl.cookie.NetscapeDraftSpecFactory;
import com.fitbur.apache.http.impl.cookie.RFC2109SpecFactory;
import com.fitbur.apache.http.impl.cookie.RFC2965SpecFactory;
import com.fitbur.apache.http.impl.execchain.BackoffStrategyExec;
import com.fitbur.apache.http.impl.execchain.ClientExecChain;
import com.fitbur.apache.http.impl.execchain.MainClientExec;
import com.fitbur.apache.http.impl.execchain.ProtocolExec;
import com.fitbur.apache.http.impl.execchain.RedirectExec;
import com.fitbur.apache.http.impl.execchain.RetryExec;
import com.fitbur.apache.http.impl.execchain.ServiceUnavailableRetryExec;
import com.fitbur.apache.http.protocol.HttpProcessor;
import com.fitbur.apache.http.protocol.HttpProcessorBuilder;
import com.fitbur.apache.http.protocol.HttpRequestExecutor;
import com.fitbur.apache.http.protocol.RequestContent;
import com.fitbur.apache.http.protocol.RequestTargetHost;
import com.fitbur.apache.http.protocol.RequestUserAgent;
import com.fitbur.apache.http.util.TextUtils;
import com.fitbur.apache.http.util.VersionInfo;
/**
* Builder for {@link CloseableHttpClient} instances.
*
* When a particular com.fitburponent is not explicitly this class will
* use its com.fitburfault implementation. System properties will be taken
* into account when configuring the com.fitburfault implementations when
* {@link #useSystemProperties()} method is called prior to calling
* {@link #build()}.
*
* - ssl.TrustManagerFactory.algorithm
* - javax.net.ssl.trustStoreType
* - javax.net.ssl.trustStore
* - javax.net.ssl.trustStoreProvider
* - javax.net.ssl.trustStorePassword
* - ssl.KeyManagerFactory.algorithm
* - javax.net.ssl.keyStoreType
* - javax.net.ssl.keyStore
* - javax.net.ssl.keyStoreProvider
* - javax.net.ssl.keyStorePassword
* - https.protocols
* - https.cipherSuites
* - http.proxyHost
* - http.proxyPort
* - http.nonProxyHosts
* - http.keepAlive
* - http.maxConnections
* - http.agent
*
*
* Please note that some settings used by this class can be mutually
* exclusive and may not apply when building {@link CloseableHttpClient}
* instances.
*
* @since 4.3
*/
@NotThreadSafe
public class HttpClientBuilder {
private HttpRequestExecutor requestExec;
private X509HostnameVerifier hostnameVerifier;
private LayeredConnectionSocketFactory sslSocketFactory;
private SSLContext sslcontext;
private HttpClientConnectionManager connManager;
private SchemePortResolver schemePortResolver;
private ConnectionReuseStrategy reuseStrategy;
private ConnectionKeepAliveStrategy keepAliveStrategy;
private AuthenticationStrategy targetAuthStrategy;
private AuthenticationStrategy proxyAuthStrategy;
private UserTokenHandler userTokenHandler;
private HttpProcessor httpprocessor;
private LinkedList requestFirst;
private LinkedList requestLast;
private LinkedList responseFirst;
private LinkedList responseLast;
private HttpRequestRetryHandler retryHandler;
private HttpRoutePlanner routePlanner;
private RedirectStrategy redirectStrategy;
private ConnectionBackoffStrategy connectionBackoffStrategy;
private BackoffManager backoffManager;
private ServiceUnavailableRetryStrategy serviceUnavailStrategy;
private Lookup authSchemeRegistry;
private Lookup cookieSpecRegistry;
private CookieStore cookieStore;
private CredentialsProvider credentialsProvider;
private String userAgent;
private HttpHost proxy;
private Collection extends Header> com.fitburfaultHeaders;
private SocketConfig com.fitburfaultSocketConfig;
private ConnectionConfig com.fitburfaultConnectionConfig;
private RequestConfig com.fitburfaultRequestConfig;
private boolean systemProperties;
private boolean redirectHandlingDisabled;
private boolean automaticRetriesDisabled;
private boolean contentCompressionDisabled;
private boolean cookieManagementDisabled;
private boolean authCachingDisabled;
private boolean connectionStateDisabled;
private int maxConnTotal = 0;
private int maxConnPerRoute = 0;
private List closeables;
static final String DEFAULT_USER_AGENT;
static {
final VersionInfo vi = VersionInfo.loadVersionInfo
("com.fitbur.apache.http.client", HttpClientBuilder.class.getClassLoader());
final String release = (vi != null) ?
vi.getRelease() : VersionInfo.UNAVAILABLE;
DEFAULT_USER_AGENT = "Apache-HttpClient/" + release + " (java 1.5)";
}
public static HttpClientBuilder create() {
return new HttpClientBuilder();
}
protected HttpClientBuilder() {
super();
}
/**
* Assigns {@link HttpRequestExecutor} instance.
*/
public final HttpClientBuilder setRequestExecutor(final HttpRequestExecutor requestExec) {
this.requestExec = requestExec;
return this;
}
/**
* Assigns {@link X509HostnameVerifier} instance.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} and the {@link #setSSLSocketFactory(
* com.fitbur.apache.http.conn.socket.LayeredConnectionSocketFactory)} methods.
*/
public final HttpClientBuilder setHostnameVerifier(final X509HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}
/**
* Assigns {@link SSLContext} instance.
*
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} and the {@link #setSSLSocketFactory(
* com.fitbur.apache.http.conn.socket.LayeredConnectionSocketFactory)} methods.
*/
public final HttpClientBuilder setSslcontext(final SSLContext sslcontext) {
this.sslcontext = sslcontext;
return this;
}
/**
* Assigns {@link LayeredConnectionSocketFactory} instance.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} method.
*/
public final HttpClientBuilder setSSLSocketFactory(
final LayeredConnectionSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
return this;
}
/**
* Assigns maximum total connection value.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} method.
*/
public final HttpClientBuilder setMaxConnTotal(final int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}
/**
* Assigns maximum connection per route value.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} method.
*/
public final HttpClientBuilder setMaxConnPerRoute(final int maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
return this;
}
/**
* Assigns com.fitburfault {@link SocketConfig}.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} method.
*/
public final HttpClientBuilder setDefaultSocketConfig(final SocketConfig config) {
this.com.fitburfaultSocketConfig = config;
return this;
}
/**
* Assigns com.fitburfault {@link ConnectionConfig}.
*
* Please note this value can be overridden by the {@link #setConnectionManager(
* com.fitbur.apache.http.conn.HttpClientConnectionManager)} method.
*/
public final HttpClientBuilder setDefaultConnectionConfig(final ConnectionConfig config) {
this.com.fitburfaultConnectionConfig = config;
return this;
}
/**
* Assigns {@link HttpClientConnectionManager} instance.
*/
public final HttpClientBuilder setConnectionManager(
final HttpClientConnectionManager connManager) {
this.connManager = connManager;
return this;
}
/**
* Assigns {@link ConnectionReuseStrategy} instance.
*/
public final HttpClientBuilder setConnectionReuseStrategy(
final ConnectionReuseStrategy reuseStrategy) {
this.reuseStrategy = reuseStrategy;
return this;
}
/**
* Assigns {@link ConnectionKeepAliveStrategy} instance.
*/
public final HttpClientBuilder setKeepAliveStrategy(
final ConnectionKeepAliveStrategy keepAliveStrategy) {
this.keepAliveStrategy = keepAliveStrategy;
return this;
}
/**
* Assigns {@link AuthenticationStrategy} instance for proxy
* authentication.
*/
public final HttpClientBuilder setTargetAuthenticationStrategy(
final AuthenticationStrategy targetAuthStrategy) {
this.targetAuthStrategy = targetAuthStrategy;
return this;
}
/**
* Assigns {@link AuthenticationStrategy} instance for target
* host authentication.
*/
public final HttpClientBuilder setProxyAuthenticationStrategy(
final AuthenticationStrategy proxyAuthStrategy) {
this.proxyAuthStrategy = proxyAuthStrategy;
return this;
}
/**
* Assigns {@link UserTokenHandler} instance.
*
* Please note this value can be overridden by the {@link #disableConnectionState()}
* method.
*/
public final HttpClientBuilder setUserTokenHandler(final UserTokenHandler userTokenHandler) {
this.userTokenHandler = userTokenHandler;
return this;
}
/**
* Disables connection state tracking.
*/
public final HttpClientBuilder disableConnectionState() {
connectionStateDisabled = true;
return this;
}
/**
* Assigns {@link SchemePortResolver} instance.
*/
public final HttpClientBuilder setSchemePortResolver(
final SchemePortResolver schemePortResolver) {
this.schemePortResolver = schemePortResolver;
return this;
}
/**
* Assigns User-Agent value.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder setUserAgent(final String userAgent) {
this.userAgent = userAgent;
return this;
}
/**
* Assigns com.fitburfault request header values.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder setDefaultHeaders(final Collection extends Header> com.fitburfaultHeaders) {
this.com.fitburfaultHeaders = com.fitburfaultHeaders;
return this;
}
/**
* Adds this protocol interceptor to the head of the protocol processing list.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder addInterceptorFirst(final HttpResponseInterceptor itcp) {
if (itcp == null) {
return this;
}
if (responseFirst == null) {
responseFirst = new LinkedList();
}
responseFirst.addFirst(itcp);
return this;
}
/**
* Adds this protocol interceptor to the tail of the protocol processing list.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder addInterceptorLast(final HttpResponseInterceptor itcp) {
if (itcp == null) {
return this;
}
if (responseLast == null) {
responseLast = new LinkedList();
}
responseLast.addLast(itcp);
return this;
}
/**
* Adds this protocol interceptor to the head of the protocol processing list.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder addInterceptorFirst(final HttpRequestInterceptor itcp) {
if (itcp == null) {
return this;
}
if (requestFirst == null) {
requestFirst = new LinkedList();
}
requestFirst.addFirst(itcp);
return this;
}
/**
* Adds this protocol interceptor to the tail of the protocol processing list.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder addInterceptorLast(final HttpRequestInterceptor itcp) {
if (itcp == null) {
return this;
}
if (requestLast == null) {
requestLast = new LinkedList();
}
requestLast.addLast(itcp);
return this;
}
/**
* Disables state (cookie) management.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder disableCookieManagement() {
this.cookieManagementDisabled = true;
return this;
}
/**
* Disables automatic content com.fitburcompression.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder disableContentCompression() {
contentCompressionDisabled = true;
return this;
}
/**
* Disables authentication scheme caching.
*
* Please note this value can be overridden by the {@link #setHttpProcessor(
* com.fitbur.apache.http.protocol.HttpProcessor)} method.
*/
public final HttpClientBuilder disableAuthCaching() {
this.authCachingDisabled = true;
return this;
}
/**
* Assigns {@link HttpProcessor} instance.
*/
public final HttpClientBuilder setHttpProcessor(final HttpProcessor httpprocessor) {
this.httpprocessor = httpprocessor;
return this;
}
/**
* Assigns {@link HttpRequestRetryHandler} instance.
*
* Please note this value can be overridden by the {@link #disableAutomaticRetries()}
* method.
*/
public final HttpClientBuilder setRetryHandler(final HttpRequestRetryHandler retryHandler) {
this.retryHandler = retryHandler;
return this;
}
/**
* Disables automatic request recovery and re-execution.
*/
public final HttpClientBuilder disableAutomaticRetries() {
automaticRetriesDisabled = true;
return this;
}
/**
* Assigns com.fitburfault proxy value.
*
* Please note this value can be overridden by the {@link #setRoutePlanner(
* com.fitbur.apache.http.conn.routing.HttpRoutePlanner)} and
* {@link #disableRedirectHandling()} methods.
*/
public final HttpClientBuilder setProxy(final HttpHost proxy) {
this.proxy = proxy;
return this;
}
/**
* Assigns {@link HttpRoutePlanner} instance.
*
* Please note this value can be overridden by the {@link #disableRedirectHandling()}
* method.
*/
public final HttpClientBuilder setRoutePlanner(final HttpRoutePlanner routePlanner) {
this.routePlanner = routePlanner;
return this;
}
/**
* Assigns {@link RedirectStrategy} instance.
*/
public final HttpClientBuilder setRedirectStrategy(final RedirectStrategy redirectStrategy) {
this.redirectStrategy = redirectStrategy;
return this;
}
/**
* Disables automatic redirect handling.
*/
public final HttpClientBuilder disableRedirectHandling() {
redirectHandlingDisabled = true;
return this;
}
/**
* Assigns {@link ConnectionBackoffStrategy} instance.
*/
public final HttpClientBuilder setConnectionBackoffStrategy(
final ConnectionBackoffStrategy connectionBackoffStrategy) {
this.connectionBackoffStrategy = connectionBackoffStrategy;
return this;
}
/**
* Assigns {@link BackoffManager} instance.
*/
public final HttpClientBuilder setBackoffManager(final BackoffManager backoffManager) {
this.backoffManager = backoffManager;
return this;
}
/**
* Assigns {@link ServiceUnavailableRetryStrategy} instance.
*/
public final HttpClientBuilder setServiceUnavailableRetryStrategy(
final ServiceUnavailableRetryStrategy serviceUnavailStrategy) {
this.serviceUnavailStrategy = serviceUnavailStrategy;
return this;
}
/**
* Assigns com.fitburfault {@link CookieStore} instance which will be used for
* request execution if not explicitly set in the client execution context.
*/
public final HttpClientBuilder setDefaultCookieStore(final CookieStore cookieStore) {
this.cookieStore = cookieStore;
return this;
}
/**
* Assigns com.fitburfault {@link CredentialsProvider} instance which will be used
* for request execution if not explicitly set in the client execution
* context.
*/
public final HttpClientBuilder setDefaultCredentialsProvider(
final CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}
/**
* Assigns com.fitburfault {@link com.fitbur.apache.http.auth.AuthScheme} registry which will
* be used for request execution if not explicitly set in the client execution
* context.
*/
public final HttpClientBuilder setDefaultAuthSchemeRegistry(
final Lookup authSchemeRegistry) {
this.authSchemeRegistry = authSchemeRegistry;
return this;
}
/**
* Assigns com.fitburfault {@link com.fitbur.apache.http.cookie.CookieSpec} registry which will
* be used for request execution if not explicitly set in the client execution
* context.
*/
public final HttpClientBuilder setDefaultCookieSpecRegistry(
final Lookup cookieSpecRegistry) {
this.cookieSpecRegistry = cookieSpecRegistry;
return this;
}
/**
* Assigns com.fitburfault {@link RequestConfig} instance which will be used
* for request execution if not explicitly set in the client execution
* context.
*/
public final HttpClientBuilder setDefaultRequestConfig(final RequestConfig config) {
this.com.fitburfaultRequestConfig = config;
return this;
}
/**
* Use system properties when creating and configuring com.fitburfault
* implementations.
*/
public final HttpClientBuilder useSystemProperties() {
systemProperties = true;
return this;
}
/**
* For internal use.
*/
protected ClientExecChain com.fitburcorateMainExec(final ClientExecChain mainExec) {
return mainExec;
}
/**
* For internal use.
*/
protected ClientExecChain com.fitburcorateProtocolExec(final ClientExecChain protocolExec) {
return protocolExec;
}
/**
* For internal use.
*/
protected void addCloseable(final Closeable closeable) {
if (closeable == null) {
return;
}
if (closeables == null) {
closeables = new ArrayList();
}
closeables.add(closeable);
}
private static String[] split(final String s) {
if (TextUtils.isBlank(s)) {
return null;
}
return s.split(" *, *");
}
public CloseableHttpClient build() {
// Create main request executor
HttpRequestExecutor requestExec = this.requestExec;
if (requestExec == null) {
requestExec = new HttpRequestExecutor();
}
HttpClientConnectionManager connManager = this.connManager;
if (connManager == null) {
LayeredConnectionSocketFactory sslSocketFactory = this.sslSocketFactory;
if (sslSocketFactory == null) {
final String[] supportedProtocols = systemProperties ? split(
System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = systemProperties ? split(
System.getProperty("https.cipherSuites")) : null;
X509HostnameVerifier hostnameVerifier = this.hostnameVerifier;
if (hostnameVerifier == null) {
hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
}
if (sslcontext != null) {
sslSocketFactory = new SSLConnectionSocketFactory(
sslcontext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
if (systemProperties) {
sslSocketFactory = new SSLConnectionSocketFactory(
(SSLSocketFactory) SSLSocketFactory.getDefault(),
supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
sslSocketFactory = new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
hostnameVerifier);
}
}
}
@SuppressWarnings("resource")
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build());
if (com.fitburfaultSocketConfig != null) {
poolingmgr.setDefaultSocketConfig(com.fitburfaultSocketConfig);
}
if (com.fitburfaultConnectionConfig != null) {
poolingmgr.setDefaultConnectionConfig(com.fitburfaultConnectionConfig);
}
if (systemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);
}
}
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
connManager = poolingmgr;
}
ConnectionReuseStrategy reuseStrategy = this.reuseStrategy;
if (reuseStrategy == null) {
if (systemProperties) {
final String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
reuseStrategy = DefaultConnectionReuseStrategy.INSTANCE;
} else {
reuseStrategy = NoConnectionReuseStrategy.INSTANCE;
}
} else {
reuseStrategy = DefaultConnectionReuseStrategy.INSTANCE;
}
}
ConnectionKeepAliveStrategy keepAliveStrategy = this.keepAliveStrategy;
if (keepAliveStrategy == null) {
keepAliveStrategy = DefaultConnectionKeepAliveStrategy.INSTANCE;
}
AuthenticationStrategy targetAuthStrategy = this.targetAuthStrategy;
if (targetAuthStrategy == null) {
targetAuthStrategy = TargetAuthenticationStrategy.INSTANCE;
}
AuthenticationStrategy proxyAuthStrategy = this.proxyAuthStrategy;
if (proxyAuthStrategy == null) {
proxyAuthStrategy = ProxyAuthenticationStrategy.INSTANCE;
}
UserTokenHandler userTokenHandler = this.userTokenHandler;
if (userTokenHandler == null) {
if (!connectionStateDisabled) {
userTokenHandler = DefaultUserTokenHandler.INSTANCE;
} else {
userTokenHandler = NoopUserTokenHandler.INSTANCE;
}
}
ClientExecChain execChain = new MainClientExec(
requestExec,
connManager,
reuseStrategy,
keepAliveStrategy,
targetAuthStrategy,
proxyAuthStrategy,
userTokenHandler);
execChain = com.fitburcorateMainExec(execChain);
HttpProcessor httpprocessor = this.httpprocessor;
if (httpprocessor == null) {
String userAgent = this.userAgent;
if (userAgent == null) {
if (systemProperties) {
userAgent = System.getProperty("http.agent");
}
if (userAgent == null) {
userAgent = DEFAULT_USER_AGENT;
}
}
final HttpProcessorBuilder b = HttpProcessorBuilder.create();
if (requestFirst != null) {
for (final HttpRequestInterceptor i: requestFirst) {
b.addFirst(i);
}
}
if (responseFirst != null) {
for (final HttpResponseInterceptor i: responseFirst) {
b.addFirst(i);
}
}
b.addAll(
new RequestDefaultHeaders(com.fitburfaultHeaders),
new RequestContent(),
new RequestTargetHost(),
new RequestClientConnControl(),
new RequestUserAgent(userAgent),
new RequestExpectContinue());
if (!cookieManagementDisabled) {
b.add(new RequestAddCookies());
}
if (!contentCompressionDisabled) {
b.add(new RequestAcceptEncoding());
}
if (!authCachingDisabled) {
b.add(new RequestAuthCache());
}
if (!cookieManagementDisabled) {
b.add(new ResponseProcessCookies());
}
if (!contentCompressionDisabled) {
b.add(new ResponseContentEncoding());
}
if (requestLast != null) {
for (final HttpRequestInterceptor i: requestLast) {
b.addLast(i);
}
}
if (responseLast != null) {
for (final HttpResponseInterceptor i: responseLast) {
b.addLast(i);
}
}
httpprocessor = b.build();
}
execChain = new ProtocolExec(execChain, httpprocessor);
execChain = com.fitburcorateProtocolExec(execChain);
// Add request retry executor, if not disabled
if (!automaticRetriesDisabled) {
HttpRequestRetryHandler retryHandler = this.retryHandler;
if (retryHandler == null) {
retryHandler = DefaultHttpRequestRetryHandler.INSTANCE;
}
execChain = new RetryExec(execChain, retryHandler);
}
HttpRoutePlanner routePlanner = this.routePlanner;
if (routePlanner == null) {
SchemePortResolver schemePortResolver = this.schemePortResolver;
if (schemePortResolver == null) {
schemePortResolver = DefaultSchemePortResolver.INSTANCE;
}
if (proxy != null) {
routePlanner = new DefaultProxyRoutePlanner(proxy, schemePortResolver);
} else if (systemProperties) {
routePlanner = new SystemDefaultRoutePlanner(
schemePortResolver, ProxySelector.getDefault());
} else {
routePlanner = new DefaultRoutePlanner(schemePortResolver);
}
}
// Add redirect executor, if not disabled
if (!redirectHandlingDisabled) {
RedirectStrategy redirectStrategy = this.redirectStrategy;
if (redirectStrategy == null) {
redirectStrategy = DefaultRedirectStrategy.INSTANCE;
}
execChain = new RedirectExec(execChain, routePlanner, redirectStrategy);
}
// Optionally, add service unavailable retry executor
final ServiceUnavailableRetryStrategy serviceUnavailStrategy = this.serviceUnavailStrategy;
if (serviceUnavailStrategy != null) {
execChain = new ServiceUnavailableRetryExec(execChain, serviceUnavailStrategy);
}
// Optionally, add connection back-off executor
final BackoffManager backoffManager = this.backoffManager;
final ConnectionBackoffStrategy connectionBackoffStrategy = this.connectionBackoffStrategy;
if (backoffManager != null && connectionBackoffStrategy != null) {
execChain = new BackoffStrategyExec(execChain, connectionBackoffStrategy, backoffManager);
}
Lookup authSchemeRegistry = this.authSchemeRegistry;
if (authSchemeRegistry == null) {
authSchemeRegistry = RegistryBuilder.create()
.register(AuthSchemes.BASIC, new BasicSchemeFactory())
.register(AuthSchemes.DIGEST, new DigestSchemeFactory())
.register(AuthSchemes.NTLM, new NTLMSchemeFactory())
.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
.build();
}
Lookup cookieSpecRegistry = this.cookieSpecRegistry;
if (cookieSpecRegistry == null) {
cookieSpecRegistry = RegistryBuilder.create()
.register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
.register(CookieSpecs.STANDARD, new RFC2965SpecFactory())
.register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory())
.register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecFactory())
.register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecFactory())
.register("rfc2109", new RFC2109SpecFactory())
.register("rfc2965", new RFC2965SpecFactory())
.build();
}
CookieStore com.fitburfaultCookieStore = this.cookieStore;
if (com.fitburfaultCookieStore == null) {
com.fitburfaultCookieStore = new BasicCookieStore();
}
CredentialsProvider com.fitburfaultCredentialsProvider = this.credentialsProvider;
if (com.fitburfaultCredentialsProvider == null) {
if (systemProperties) {
com.fitburfaultCredentialsProvider = new SystemDefaultCredentialsProvider();
} else {
com.fitburfaultCredentialsProvider = new BasicCredentialsProvider();
}
}
return new InternalHttpClient(
execChain,
connManager,
routePlanner,
cookieSpecRegistry,
authSchemeRegistry,
com.fitburfaultCookieStore,
com.fitburfaultCredentialsProvider,
com.fitburfaultRequestConfig != null ? com.fitburfaultRequestConfig : RequestConfig.DEFAULT,
closeables != null ? new ArrayList(closeables) : null);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy