Search in sources :

Example 26 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project htmlunit by HtmlUnit.

the class HttpWebConnection method createConnectionManager.

/**
 * Has the exact logic in {@link HttpClientBuilder#build()} which sets the {@code connManager} part,
 * but with the ability to configure {@code socketFactory}.
 */
private static PoolingHttpClientConnectionManager createConnectionManager(final HttpClientBuilder builder) {
    try {
        PublicSuffixMatcher publicSuffixMatcher = getField(builder, "publicSuffixMatcher");
        if (publicSuffixMatcher == null) {
            publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
        }
        LayeredConnectionSocketFactory sslSocketFactory = getField(builder, "sslSocketFactory");
        final SocketConfig defaultSocketConfig = getField(builder, "defaultSocketConfig");
        final ConnectionConfig defaultConnectionConfig = getField(builder, "defaultConnectionConfig");
        final boolean systemProperties = getField(builder, "systemProperties");
        final int maxConnTotal = getField(builder, "maxConnTotal");
        final int maxConnPerRoute = getField(builder, "maxConnPerRoute");
        HostnameVerifier hostnameVerifier = getField(builder, "hostnameVerifier");
        final SSLContext sslcontext = getField(builder, "sslContext");
        final DnsResolver dnsResolver = getField(builder, "dnsResolver");
        final long connTimeToLive = getField(builder, "connTimeToLive");
        final TimeUnit connTimeToLiveTimeUnit = getField(builder, "connTimeToLiveTimeUnit");
        if (sslSocketFactory == null) {
            final String[] supportedProtocols = systemProperties ? split(System.getProperty("https.protocols")) : null;
            final String[] supportedCipherSuites = systemProperties ? split(System.getProperty("https.cipherSuites")) : null;
            if (hostnameVerifier == null) {
                hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
            }
            if (sslcontext == null) {
                if (systemProperties) {
                    sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
                } else {
                    sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
                }
            } else {
                sslSocketFactory = new SSLConnectionSocketFactory(sslcontext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
            }
        }
        final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", new SocksConnectionSocketFactory()).register("https", sslSocketFactory).build(), null, null, dnsResolver, connTimeToLive, connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
        if (defaultSocketConfig != null) {
            poolingmgr.setDefaultSocketConfig(defaultSocketConfig);
        }
        if (defaultConnectionConfig != null) {
            poolingmgr.setDefaultConnectionConfig(defaultConnectionConfig);
        }
        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);
        }
        return poolingmgr;
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : DnsResolver(org.apache.http.conn.DnsResolver) SocketConfig(org.apache.http.config.SocketConfig) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HtmlUnitSSLConnectionSocketFactory(com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory) HttpHint(com.gargoylesoftware.htmlunit.WebRequest.HttpHint) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SocksConnectionSocketFactory(com.gargoylesoftware.htmlunit.httpclient.SocksConnectionSocketFactory) SocksConnectionSocketFactory(com.gargoylesoftware.htmlunit.httpclient.SocksConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) HtmlUnitSSLConnectionSocketFactory(com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) TimeUnit(java.util.concurrent.TimeUnit) PublicSuffixMatcher(org.apache.http.conn.util.PublicSuffixMatcher) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 27 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project lavaplayer by WearifulCupid0.

the class ExtendedConnectionOperator method upgrade.

@Override
public void upgrade(ManagedHttpClientConnection connection, HttpHost host, HttpContext context) throws IOException {
    ConnectionSocketFactory socketFactory = getSocketFactory(host, HttpClientContext.adapt(context));
    if (!(socketFactory instanceof LayeredConnectionSocketFactory)) {
        throw new UnsupportedSchemeException(host.getSchemeName() + " protocol does not support connection upgrade");
    }
    LayeredConnectionSocketFactory layeredFactory = (LayeredConnectionSocketFactory) socketFactory;
    Socket socket = connection.getSocket();
    int port = this.schemePortResolver.resolve(host);
    socket = layeredFactory.createLayeredSocket(socket, host.getHostName(), port, context);
    connection.bind(socket);
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) UnsupportedSchemeException(org.apache.http.conn.UnsupportedSchemeException) Socket(java.net.Socket)

Aggregations

LayeredConnectionSocketFactory (org.apache.http.conn.socket.LayeredConnectionSocketFactory)27 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)19 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)16 SSLContext (javax.net.ssl.SSLContext)13 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)13 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)12 KeyManagementException (java.security.KeyManagementException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Socket (java.net.Socket)5 KeyStore (java.security.KeyStore)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 HttpHost (org.apache.http.HttpHost)5 HttpClient (org.apache.http.client.HttpClient)4 File (java.io.File)3 InetSocketAddress (java.net.InetSocketAddress)3 KeyStoreException (java.security.KeyStoreException)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 SocketConfig (org.apache.http.config.SocketConfig)3