use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project carbon-apimgt by wso2.
the class APIUtil method getHttpClient.
/**
* Return a http client instance
*
* @param port - server port
* @param protocol- service endpoint protocol http/https
* @return
*/
public static HttpClient getHttpClient(int port, String protocol) {
APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String maxTotal = configuration.getFirstProperty(APIConstants.HTTP_CLIENT_MAX_TOTAL);
String defaultMaxPerRoute = configuration.getFirstProperty(APIConstants.HTTP_CLIENT_DEFAULT_MAX_PER_ROUTE);
String proxyEnabled = configuration.getFirstProperty(APIConstants.PROXY_ENABLE);
String proxyHost = configuration.getFirstProperty(APIConstants.PROXY_HOST);
String proxyPort = configuration.getFirstProperty(APIConstants.PROXY_PORT);
String proxyUsername = configuration.getFirstProperty(APIConstants.PROXY_USERNAME);
String proxyPassword = configuration.getFirstProperty(APIConstants.PROXY_PASSWORD);
String nonProxyHosts = configuration.getFirstProperty(APIConstants.NON_PROXY_HOSTS);
String proxyProtocol = configuration.getFirstProperty(APIConstants.PROXY_PROTOCOL);
if (proxyProtocol != null) {
protocol = proxyProtocol;
}
PoolingHttpClientConnectionManager pool = null;
try {
pool = getPoolingHttpClientConnectionManager(protocol);
} catch (APIManagementException e) {
log.error("Error while getting http client connection manager", e);
}
pool.setMaxTotal(Integer.parseInt(maxTotal));
pool.setDefaultMaxPerRoute(Integer.parseInt(defaultMaxPerRoute));
RequestConfig params = RequestConfig.custom().build();
HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(pool).setDefaultRequestConfig(params);
if (Boolean.parseBoolean(proxyEnabled)) {
HttpHost host = new HttpHost(proxyHost, Integer.parseInt(proxyPort), protocol);
DefaultProxyRoutePlanner routePlanner;
if (!StringUtils.isBlank(nonProxyHosts)) {
routePlanner = new ExtendedProxyRoutePlanner(host, configuration);
} else {
routePlanner = new DefaultProxyRoutePlanner(host);
}
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
if (!StringUtils.isBlank(proxyUsername) && !StringUtils.isBlank(proxyPassword)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
clientBuilder = clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}
return clientBuilder.build();
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project wikidata-query-rdf by wikimedia.
the class HttpClientUtils method createPooledConnectionManager.
public static HttpClientConnectionManager createPooledConnectionManager(int soTimeout) {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(-1L, SECONDS);
configureConnectionManager(soTimeout, connectionManager);
return connectionManager;
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project quickutil by quickutil.
the class HttpUtil method buildHttpsClientMananger.
/**
* 生成https连接管理器
*/
public static HttpClientConnectionManager buildHttpsClientMananger(InputStream clientCer, String clientPW, InputStream serverCer, String serverPW) {
try {
KeyManager[] keysManagers = null;
TrustManager[] trustManagers = null;
// 验证客户端证书
if (clientCer != null) {
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(clientCer, clientPW.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(ks, clientPW.toCharArray());
keysManagers = keyManagerFactory.getKeyManagers();
}
// 验证服务端证书
if (serverCer != null) {
KeyStore ks2 = KeyStore.getInstance("pkcs12");
ks2.load(serverCer, serverPW.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks2);
trustManagers = trustManagerFactory.getTrustManagers();
} else {
trustManagers = new TrustManager[] { tm };
}
// 生成ssl参数
SSLContext context = SSLContext.getInstance("TLS");
context.init(keysManagers, trustManagers, null);
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
return new PoolingHttpClientConnectionManager(socketFactoryRegistry);
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
return null;
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project kylo by Teradata.
the class CustomApacheConnector method createConnectionManager.
private HttpClientConnectionManager createConnectionManager(final Client client, final Configuration config, final SSLContext sslContext, final boolean useSystemProperties) {
final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites")) : null;
HostnameVerifier hostnameVerifier = client.getHostnameVerifier();
final LayeredConnectionSocketFactory sslSocketFactory;
if (sslContext != null) {
sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
if (useSystemProperties) {
sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
}
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
final Integer chunkSize = ClientProperties.getValue(config.getProperties(), ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.class);
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry, new CustomApacheConnector.ConnectionFactory(chunkSize));
if (useSystemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
connectionManager.setDefaultMaxPerRoute(max);
connectionManager.setMaxTotal(2 * max);
}
}
return connectionManager;
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project athenz by yahoo.
the class HttpCertSigner method createConnectionPooling.
/**
* Create a http client connection manager based on given ssl context
* @param sslContext ssl context containing keystore with client key/cert
* @return connection manager object
*/
PoolingHttpClientConnectionManager createConnectionPooling(SSLContext sslContext) {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
// route is host + port. Since we have only one, set the max and the route the same
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_POOL_PER_ROUTE);
poolingHttpClientConnectionManager.setMaxTotal(DEFAULT_MAX_POOL_TOTAL);
return poolingHttpClientConnectionManager;
}
Aggregations