use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project athenz by yahoo.
the class ZTSClient method initClient.
private void initClient(final String serverUrl, Principal identity, final String domainName, final String serviceName, final ServiceIdentityProvider siaProvider) {
ztsUrl = (serverUrl == null) ? confZtsUrl : serverUrl;
if (!isEmpty(ztsUrl)) {
if (!ztsUrl.endsWith("/zts/v1")) {
if (ztsUrl.charAt(ztsUrl.length() - 1) != '/') {
ztsUrl += '/';
}
ztsUrl += "zts/v1";
}
}
// determine to see if we need a host verifier for our ssl connections
HostnameVerifier hostnameVerifier = null;
if (!isEmpty(x509CertDNSName)) {
hostnameVerifier = new AWSHostNameVerifier(x509CertDNSName);
}
if (sslContext == null) {
sslContext = createSSLContext();
}
// setup our client config object with timeouts
final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final ClientConfig config = new ClientConfig(jacksonJsonProvider);
PoolingHttpClientConnectionManager connManager = createConnectionManager(sslContext, hostnameVerifier);
if (connManager != null) {
config.property(ApacheClientProperties.CONNECTION_MANAGER, connManager);
}
config.connectorProvider(new ApacheConnectorProvider());
if (proxyUrl != null) {
config.property(ClientProperties.PROXY_URI, proxyUrl);
}
ClientBuilder builder = getClientBuilder();
if (sslContext != null) {
builder = builder.sslContext(sslContext);
enablePrefetch = true;
}
// JerseyClientBuilder::withConfig() replaces the existing config with the new client
// config. Hence the client config should be added to the builder before the timeouts.
// Otherwise the timeout settings would be overridden.
Client rsClient = builder.withConfig(config).hostnameVerifier(hostnameVerifier).readTimeout(reqReadTimeout, TimeUnit.MILLISECONDS).connectTimeout(reqConnectTimeout, TimeUnit.MILLISECONDS).build();
ztsClient = new ZTSRDLGeneratedClient(ztsUrl, rsClient);
principal = identity;
domain = domainName;
service = serviceName;
this.siaProvider = siaProvider;
if (principal != null) {
domain = principal.getDomain();
service = principal.getName();
ztsClient.addCredentials(identity.getAuthority().getHeader(), identity.getCredentials());
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project athenz by yahoo.
the class ZTSClient method createConnectionManager.
PoolingHttpClientConnectionManager createConnectionManager(SSLContext sslContext, HostnameVerifier hostnameVerifier) {
if (sslContext == null) {
return null;
}
SSLConnectionSocketFactory sslSocketFactory;
if (hostnameVerifier == null) {
sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
} else {
sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslSocketFactory).register("http", new PlainConnectionSocketFactory()).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
// we'll use the default values from apache http connector - max 20 and per route 2
int maxPerRoute = Integer.parseInt(System.getProperty(ZTS_CLIENT_PROP_POOL_MAX_PER_ROUTE, "2"));
int maxTotal = Integer.parseInt(System.getProperty(ZTS_CLIENT_PROP_POOL_MAX_TOTAL, "20"));
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
return poolingHttpClientConnectionManager;
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project openremote by openremote.
the class ExtensibleResteasyClientBuilder method initDefaultEngine43.
// The rest is copy/paste pretty much
public static ApacheHttpClient43Engine initDefaultEngine43(ExtensibleResteasyClientBuilder that) {
HttpClient httpClient = null;
HostnameVerifier verifier = null;
if (that.verifier != null) {
verifier = new ExtensibleResteasyClientBuilder.VerifierWrapper(that.verifier);
} else {
switch(that.policy) {
case ANY:
verifier = new NoopHostnameVerifier();
break;
case WILDCARD:
verifier = new DefaultHostnameVerifier();
break;
case STRICT:
verifier = new DefaultHostnameVerifier();
break;
}
}
try {
SSLConnectionSocketFactory sslsf = null;
SSLContext theContext = that.sslContext;
if (that.disableTrustManager) {
theContext = SSLContext.getInstance("SSL");
theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
verifier = new NoopHostnameVerifier();
sslsf = new SSLConnectionSocketFactory(theContext, verifier);
} else if (theContext != null) {
sslsf = new SSLConnectionSocketFactory(theContext, verifier) {
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
that.prepareSocketForSni(socket);
}
};
} else if (that.clientKeyStore != null || that.truststore != null) {
SSLContext ctx = SSLContexts.custom().setProtocol(SSLConnectionSocketFactory.TLS).setSecureRandom(null).loadKeyMaterial(that.clientKeyStore, that.clientPrivateKeyPassword != null ? that.clientPrivateKeyPassword.toCharArray() : null).loadTrustMaterial(that.truststore, TrustSelfSignedStrategy.INSTANCE).build();
sslsf = new SSLConnectionSocketFactory(ctx, verifier) {
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
that.prepareSocketForSni(socket);
}
};
} else {
final SSLContext tlsContext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
tlsContext.init(null, null, null);
sslsf = new SSLConnectionSocketFactory(tlsContext, verifier);
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
HttpClientConnectionManager cm = null;
if (that.connectionPoolSize > 0) {
PoolingHttpClientConnectionManager tcm = new PoolingHttpClientConnectionManager(registry, null, null, null, that.connectionTTL, that.connectionTTLUnit);
tcm.setMaxTotal(that.connectionPoolSize);
if (that.maxPooledPerRoute == 0) {
that.maxPooledPerRoute = that.connectionPoolSize;
}
tcm.setDefaultMaxPerRoute(that.maxPooledPerRoute);
cm = tcm;
} else {
cm = new BasicHttpClientConnectionManager(registry);
}
RequestConfig.Builder rcBuilder = RequestConfig.custom();
if (that.socketTimeout > -1) {
rcBuilder.setSocketTimeout((int) that.socketTimeoutUnits.toMillis(that.socketTimeout));
}
if (that.establishConnectionTimeout > -1) {
rcBuilder.setConnectTimeout((int) that.establishConnectionTimeoutUnits.toMillis(that.establishConnectionTimeout));
}
if (that.connectionCheckoutTimeoutMs > -1) {
rcBuilder.setConnectionRequestTimeout(that.connectionCheckoutTimeoutMs);
}
// The magic configure()
httpClient = that.configure(HttpClientBuilder.create().setConnectionManager(cm).setDefaultRequestConfig(rcBuilder.build()).setProxy(that.defaultProxy).disableContentCompression()).build();
ApacheHttpClient43Engine engine = (ApacheHttpClient43Engine) ApacheHttpClient4EngineFactory.create(httpClient, true);
engine.setResponseBufferSize(that.responseBufferSize);
engine.setHostnameVerifier(verifier);
// this may be null. We can't really support this with Apache Client.
engine.setSslContext(theContext);
return engine;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project warn-report by saaavsaaa.
the class WebRequestClient method opera.
private static String opera(String url, HttpEntity paras, boolean isGet) throws Exception {
// System.out.println("----------------------------------------");
// System.out.println("----------------------------------------");
String logPath = url;
// DefaultHttpClient httpclient = new DefaultHttpClient(); @deprecated
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
String cookieStr = "";
List<Cookie> list = cs.getCookies();
for (Cookie cookie : list) {
cookieStr += cookie.getName() + "=" + cookie.getValue() + ";";
}
CloseableHttpResponse response;
if (isGet) {
response = getResponse(logPath, cookieStr, httpclient);
} else {
response = postResponse(logPath, cookieStr, paras, httpclient);
}
// response.setEntity(new UrlEncodedFormEntity(params));
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
StringBuilder txt = new StringBuilder();
while ((line = reader.readLine()) != null) {
txt.append(line);
}
return txt.toString();
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project coprhd-controller by CoprHD.
the class SSLHelper method getHttpClientBuilder.
/**
* Generate a {@link HttpClientBuilder} instance to be used to create a httpclient instance.
* @param threadSafeClients boolean if true a thread-safe HTTP client instance is created.
* @param maxConnections for thread-safe clients, the maximum concurrent connections
* @param maxConnectionsPerHost for thread-safe clients, the maximum concurrent connections allows to a specific host
* @param useConnectionTimeout socket connection timeout in milliseconds
* @param useConnectionReadTimeout connection read timeout in milliseconds
* @param sslContext {@link SSLContext} initialized SSLContext instance
* @return {@link HttpClientBuilder} instance
*/
private static HttpClientBuilder getHttpClientBuilder(boolean threadSafeClients, int maxConnections, int maxConnectionsPerHost, int useConnectionTimeout, int useConnectionReadTimeout, SSLContext sslContext) {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(HTTPS, socketFactory).build();
HttpClientConnectionManager cm = null;
if (threadSafeClients) {
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
if (maxConnections > 0) {
((PoolingHttpClientConnectionManager) cm).setMaxTotal(maxConnections);
}
if (maxConnectionsPerHost > 0) {
((PoolingHttpClientConnectionManager) cm).setDefaultMaxPerRoute(maxConnectionsPerHost);
}
} else {
cm = new BasicHttpClientConnectionManager(socketFactoryRegistry);
}
// Build the request config identifying the socket connection parameters.
RequestConfig.Builder requestConfigBulder = RequestConfig.custom();
if (useConnectionReadTimeout > 0) {
requestConfigBulder.setSocketTimeout(useConnectionReadTimeout);
}
if (useConnectionTimeout > 0) {
requestConfigBulder.setConnectTimeout(useConnectionTimeout);
}
RequestConfig requestConfig = requestConfigBulder.setExpectContinueEnabled(true).build();
// construct a client instance with the connection manager embedded
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(cm);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
return httpClientBuilder;
}
Aggregations