Example usage for org.apache.http.conn.socket PlainConnectionSocketFactory getSocketFactory

List of usage examples for org.apache.http.conn.socket PlainConnectionSocketFactory getSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.socket PlainConnectionSocketFactory getSocketFactory.

Prototype

public static PlainConnectionSocketFactory getSocketFactory() 

Source Link

Usage

From source file:com.bosch.iot.things.example.historian.Controller.java

private synchronized CloseableHttpClient getHttpClient() {
    if (theHttpClient == null) {

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // #### ONLY FOR TEST: Trust ANY certificate (self certified, any chain, ...)
        try {/*w w w  .j av  a2s.  c  o  m*/
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true)
                    .build();
            httpClientBuilder.setSSLContext(sslContext);

            // #### ONLY FOR TEST: Do NOT verify hostname
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                    NoopHostnameVerifier.INSTANCE);

            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                    .<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslConnectionSocketFactory).build();
            PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
                    socketFactoryRegistry);
            httpClientBuilder.setConnectionManager(httpClientConnectionManager);
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException ex) {
            java.util.logging.Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

        Properties config = getConfig();
        if (config.getProperty("http.proxyHost") != null) {
            httpClientBuilder.setProxy(new HttpHost(config.getProperty("http.proxyHost"),
                    Integer.parseInt(config.getProperty("http.proxyPort"))));
        }
        if (config.getProperty("http.proxyUser") != null) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                    new AuthScope(HttpHost.create(getConfig().getProperty("thingsServiceEndpointUrl"))),
                    new UsernamePasswordCredentials(config.getProperty("http.proxyUser"),
                            config.getProperty("http.proxyPwd")));
            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
        }

        theHttpClient = httpClientBuilder.build();
    }
    return theHttpClient;
}

From source file:com.spotify.docker.client.DefaultDockerClient.java

private Registry<ConnectionSocketFactory> getSchemeRegistry(final Builder builder) {
    final SSLConnectionSocketFactory https;
    if (builder.dockerCertificates == null) {
        https = SSLConnectionSocketFactory.getSocketFactory();
    } else {/*www .j a  va  2s.c o  m*/
        https = new SSLConnectionSocketFactory(builder.dockerCertificates.sslContext(),
                builder.dockerCertificates.hostnameVerifier());
    }

    final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
            .<ConnectionSocketFactory>create().register("https", https)
            .register("http", PlainConnectionSocketFactory.getSocketFactory());

    if (builder.uri.getScheme().equals(UNIX_SCHEME)) {
        registryBuilder.register(UNIX_SCHEME, new UnixConnectionSocketFactory(builder.uri));
    }

    return registryBuilder.build();
}

From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java

private HttpClientConnectionManager createConnectionManager() throws GeneralSecurityException, IOException {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);/* w ww .  j  a  va2  s . co m*/

    if (registry == null) {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        X509TrustManager x509TrustManager = null;
        for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
            if (trustManager instanceof X509TrustManager) {
                x509TrustManager = (X509TrustManager) trustManager;
                break;
            }
        }

        X509Certificate[] acceptedIssuers = x509TrustManager.getAcceptedIssuers();
        if (acceptedIssuers != null) {
            // If this is null, something is really wrong...
            int issuerNum = 1;
            for (X509Certificate cert : acceptedIssuers) {
                trustStore.setCertificateEntry("issuer" + issuerNum, cert);
                issuerNum++;
            }
        } else {
            LOG.log(Level.INFO, "Didn't find any new certificates to trust.");
        }

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        sslContextBuilder.loadTrustMaterial(trustStore,
                new KnownArcGISCertificatesTrustStrategy(new ArrayList<>(trustedCerts)));
        SSLContext sslContext = sslContextBuilder.build();
        SSLContext.setDefault(sslContext);
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                new DataStoreProxyHostnameVerifier(new ArrayList<>(trustedCerts)));

        this.registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();
    }
    return new PoolingHttpClientConnectionManager(registry);
}

From source file:com.baidubce.http.BceHttpClient.java

/**
 * Create connection manager for http client.
 *
 * @return The connection manager for http client.
 *///from ww  w  .j  a v a2 s.c  o m
private HttpClientConnectionManager createHttpClientConnectionManager() {
    ConnectionSocketFactory socketFactory = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslSocketFactory;
    try {
        sslSocketFactory = new SSLConnectionSocketFactory(SSLContext.getDefault(),
                SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
    } catch (NoSuchAlgorithmException e) {
        throw new BceClientException("Fail to create SSLConnectionSocketFactory", e);
    }
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register(Protocol.HTTP.toString(), socketFactory)
            .register(Protocol.HTTPS.toString(), sslSocketFactory).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setDefaultMaxPerRoute(this.config.getMaxConnections());
    connectionManager.setDefaultSocketConfig(SocketConfig.custom()
            .setSoTimeout(this.config.getSocketTimeoutInMillis()).setTcpNoDelay(true).build());
    connectionManager.setMaxTotal(this.config.getMaxConnections());
    return connectionManager;
}

From source file:com.ibm.og.client.ApacheClient.java

private ConnectionSocketFactory createPlainConnectionSocketFactory() {
    return PlainConnectionSocketFactory.getSocketFactory();
}

From source file:com.helger.httpclient.HttpClientFactory.java

@Nonnull
public HttpClientConnectionManager createConnectionManager(
        @Nonnull final LayeredConnectionSocketFactory aSSLFactory) {
    final Registry<ConnectionSocketFactory> aConSocketRegistry = RegistryBuilder
            .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", aSSLFactory).build();
    final DnsResolver aDNSResolver = createDNSResolver();
    final PoolingHttpClientConnectionManager aConnMgr = new PoolingHttpClientConnectionManager(
            aConSocketRegistry, aDNSResolver);
    aConnMgr.setDefaultMaxPerRoute(100);
    aConnMgr.setMaxTotal(200);/*  ww  w.j  av  a 2 s.  c om*/
    aConnMgr.setValidateAfterInactivity(1000);

    final ConnectionConfig aConnectionConfig = createConnectionConfig();
    aConnMgr.setDefaultConnectionConfig(aConnectionConfig);

    return aConnMgr;
}

From source file:org.glassfish.jersey.apache.connector.ApacheConnector.java

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;//from  w  w w.  ja va  2 s  .c  o m
    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 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;
}

From source file:com.github.dockerjava.jaxrs.connector.ApacheConnector.java

private HttpClientConnectionManager createConnectionManager(final Configuration config,
        final SSLContext sslContext, X509HostnameVerifier hostnameVerifier, final boolean useSystemProperties) {

    final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols"))
            : null;//from www  .  j a  va  2s.  c  o m
    final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites"))
            : null;

    if (hostnameVerifier == null) {
        hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
    }

    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, 4096, Integer.class);

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            registry, new 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;
}

From source file:run.var.teamcity.cloud.docker.client.apcon.ApacheConnector.java

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;//from   ww  w.  j  av  a 2  s  .  co  m
    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(
                    org.apache.http.conn.ssl.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 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;
}