Example usage for javax.net.ssl SSLSocketFactory getDefault

List of usage examples for javax.net.ssl SSLSocketFactory getDefault

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocketFactory getDefault.

Prototype

public static SocketFactory getDefault() 

Source Link

Document

Returns the default SSL socket factory.

Usage

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java

/**
 * Has the exact logic in HttpClientBuilder, but with the ability to configure
 * <code>socketFactory</code>.
 *//*from  ww w. j av  a2 s . co  m*/
private PoolingHttpClientConnectionManager createConnectionManager(final HttpClientBuilder builder) {
    final ConnectionSocketFactory socketFactory = new SocksConnectionSocketFactory();

    LayeredConnectionSocketFactory sslSocketFactory;
    try {
        sslSocketFactory = (LayeredConnectionSocketFactory) FieldUtils.readDeclaredField(builder,
                "sslSocketFactory", true);
        final SocketConfig defaultSocketConfig = (SocketConfig) FieldUtils.readDeclaredField(builder,
                "defaultSocketConfig", true);
        final ConnectionConfig defaultConnectionConfig = (ConnectionConfig) FieldUtils
                .readDeclaredField(builder, "defaultConnectionConfig", true);
        final boolean systemProperties = (Boolean) FieldUtils.readDeclaredField(builder, "systemProperties",
                true);
        final int maxConnTotal = (Integer) FieldUtils.readDeclaredField(builder, "maxConnTotal", true);
        final int maxConnPerRoute = (Integer) FieldUtils.readDeclaredField(builder, "maxConnPerRoute", true);
        HostnameVerifier hostnameVerifier = (HostnameVerifier) FieldUtils.readDeclaredField(builder,
                "hostnameVerifier", true);
        final SSLContext sslcontext = (SSLContext) FieldUtils.readDeclaredField(builder, "sslContext", true);

        if (sslSocketFactory == null) {
            final String[] supportedProtocols = systemProperties
                    ? StringUtils.split(System.getProperty("https.protocols"), ',')
                    : null;
            final String[] supportedCipherSuites = systemProperties
                    ? StringUtils.split(System.getProperty("https.cipherSuites"), ',')
                    : null;
            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);
                }
            }
        }

        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create().register("http", socketFactory)
                        .register("https", sslSocketFactory).build());
        if (defaultSocketConfig != null) {
            connectionManager.setDefaultSocketConfig(defaultSocketConfig);
        }
        if (defaultConnectionConfig != null) {
            connectionManager.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);
                connectionManager.setDefaultMaxPerRoute(max);
                connectionManager.setMaxTotal(2 * max);
            }
        }
        if (maxConnTotal > 0) {
            connectionManager.setMaxTotal(maxConnTotal);
        }
        if (maxConnPerRoute > 0) {
            connectionManager.setDefaultMaxPerRoute(maxConnPerRoute);
        }
        return connectionManager;
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}