Example usage for org.apache.http.ssl SSLContexts createDefault

List of usage examples for org.apache.http.ssl SSLContexts createDefault

Introduction

In this page you can find the example usage for org.apache.http.ssl SSLContexts createDefault.

Prototype

public static SSLContext createDefault() throws SSLInitializationException 

Source Link

Document

Creates default factory based on the standard JSSE trust material ( cacerts file in the security properties directory).

Usage

From source file:com.xebialabs.overthere.winrm.WinRmClient.java

private HttpClientConnectionManager getHttpClientConnectionManager() {
    final Lookup<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
            .<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory() {
                @Override//from   w w w.  j  a v  a2  s  .co  m
                public Socket createSocket(HttpContext context) throws IOException {
                    return socketFactory.createSocket();
                }
            }).register("https", new SSLConnectionSocketFactory(SSLContexts.createDefault(),
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier()) {
                @Override
                public Socket createSocket(HttpContext context) throws IOException {
                    return socketFactory.createSocket();
                }
            }).build();
    return new BasicHttpClientConnectionManager(socketFactoryRegistry);
}

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

/**
 * Has the exact logic in HttpClientBuilder, but with the ability to configure
 * <code>socketFactory</code>.
 *//*from   w w  w . ja  va  2s . c  om*/
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);
    }
}

From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java

/**
 * Obtains default SSL socket factory with an SSL context based on the standard JSSE
 * trust material ({@code cacerts} file in the security properties directory).
 * System properties are not taken into consideration.
 *
 * @return default SSL socket factory/*from  w  w w. jav a2s.c o  m*/
 */
public static SSLConnectionSocketFactory getSocketFactory() throws SSLInitializationException {
    return new SSLConnectionSocketFactory(SSLContexts.createDefault(), getDefaultHostnameVerifier());
}