Search in sources :

Example 81 with PoolingHttpClientConnectionManager

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();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ExtendedProxyRoutePlanner(org.wso2.carbon.apimgt.impl.proxy.ExtendedProxyRoutePlanner) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope)

Example 82 with PoolingHttpClientConnectionManager

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;
}
Also used : PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 83 with PoolingHttpClientConnectionManager

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;
    }
}
Also used : KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) CertificateException(java.security.cert.CertificateException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory)

Example 84 with PoolingHttpClientConnectionManager

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;
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 85 with PoolingHttpClientConnectionManager

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;
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12