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:org.andstatus.app.net.http.MyHttpClientFactory.java

public static HttpClient getHttpClient(SslModeEnum sslMode) {
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", TlsSniSocketFactory.getInstance(sslMode)).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    // max.  3 connections in total
    connectionManager.setMaxTotal(3);/* w  w w . j  a va2  s.  c om*/
    // max.  2 connections per host
    connectionManager.setDefaultMaxPerRoute(2);

    // use request defaults from AndroidHttpClient
    RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
            .setConnectTimeout(MyPreferences.getConnectionTimeoutMs())
            .setSocketTimeout(2 * MyPreferences.getConnectionTimeoutMs()).setStaleConnectionCheckEnabled(false)
            .build();

    HttpClientBuilder builder = HttpClients.custom().useSystemProperties()
            .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
            /* TODO maybe:  
            .setRetryHandler(DavHttpRequestRetryHandler.INSTANCE)
            .setRedirectStrategy(DavRedirectStrategy.INSTANCE)  
            */
            .disableRedirectHandling().setUserAgent(HttpConnection.USER_AGENT).disableCookieManagement();

    return builder.build();
}

From source file:com.ninjas.movietime.conf.vendor.metrics.InstrumentedHttpClientConnectionManager.java

protected static Registry<ConnectionSocketFactory> getDefaultRegistry() {
    return RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
}

From source file:zz.pseas.ghost.client.ClientFactory.java

public static CloseableHttpClient getNewClient() {
    try {//from   w w w.  j  a v a 2 s .co m
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf)
                .build();
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(reg);

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 60 * 1000)
                .setConnectionRequestTimeout(10 * 60 * 1000).setConnectionRequestTimeout(10 * 60 * 1000)
                .build();

        CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connMgr).build();
        return client;
    } catch (Exception e) {
        return null;
    }
}

From source file:co.cask.cdap.client.rest.RestUtil.java

public static Registry<ConnectionSocketFactory> getRegistryWithDisabledCertCheck()
        throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override/*from  w  w  w.j ava 2  s  . c om*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }
    } }, new SecureRandom());
    SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    return RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf)
            .register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
}

From source file:org.dcache.srm.client.GsiHttpClientSender.java

@Override
protected Registry<ConnectionSocketFactory> createSocketFactoryRegistry() {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = new FlexibleCredentialSSLConnectionSocketFactory(sslContextFactory,
            supportedProtocols, supportedCipherSuites, hostnameVerifier);
    GsiConnectionSocketFactory gsisf = new GsiConnectionSocketFactory(sslsf);
    return RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", gsisf)
            .build();/*  ww w . j  a  va2s.  c om*/
}

From source file:org.elasticsearch.client.SSLSocketFactoryHttpConfigCallback.java

@Override
public void customizeHttpClient(HttpClientBuilder httpClientBuilder) {
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);// w w  w  . j  a  v a 2s . c  o m
    //default settings may be too constraining
    connectionManager.setDefaultMaxPerRoute(10);
    connectionManager.setMaxTotal(30);
    httpClientBuilder.setConnectionManager(connectionManager);
}

From source file:org.geosamples.utilities.HTTPClient.java

/**
 * This method relaxes SSL constraints because geosamples does not yet
 * provide certificate./*from   www  . ja  v  a  2  s  . c o  m*/
 *
 * @see <a href="http://literatejava.com/networks/ignore-ssl-certificate-errors-apache-httpclient-4-4/">Tom's Blog</a>
 * @return CloseableHttpClient
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyStoreException
 * @throws java.security.KeyManagementException
 */
public static CloseableHttpClient clientWithNoSecurityValidation()
        throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();

    // setup a Trust Strategy that allows all certificates.
    SSLContext sslContext = null;

    sslContext = new SSLContextBuilder().loadTrustMaterial(null, (X509Certificate[] arg0, String arg1) -> true)
            .build();

    clientBuilder.setSSLContext(sslContext);

    // don't check Hostnames, either.
    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

    // here's the special part:
    //      -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
    //      -- and create a Registry, to register it.
    //
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    // now, we create connection-manager using our Registry.
    //      -- allows multi-threaded use
    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    clientBuilder.setConnectionManager(connMgr);

    CloseableHttpClient httpClient = clientBuilder.build();

    return httpClient;
}

From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java

public void createHttpClient() {
    PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
    SSLContext sslContext = SSLContexts.createSystemDefault();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);

    Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("http", sf)
            .register("https", sslsf).build();

    this.cm = new PoolingHttpClientConnectionManager(r);
}

From source file:utils.HttpClientGenerator.java

public static CloseableHttpClient getHttpClient(boolean checkCert) {

    if (checkCert == false) {
        HttpClientBuilder b = HttpClientBuilder.create();

        // setup a Trust Strategy that allows all certificates.
        SSLContext sslContext = null;
        try {//from   w ww  . ja v  a2 s.  c  o  m
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            String err = "error occurred while creating SSL disables hhtp client";
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        b.setSslcontext(sslContext);

        // not to check Hostnames
        HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        //       create an SSL Socket Factory, to use weakened "trust strategy";
        //       and create a Registry, to register it.
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                (X509HostnameVerifier) hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();

        // creating connection-manager using our Registry.
        //      -- allows multi-threaded use
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
                socketFactoryRegistry);
        connMgr.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        connMgr.setMaxPerRoute(new HttpRoute(localhost), 10);
        b.setConnectionManager(connMgr);

        // finally, build the HttpClient;
        CloseableHttpClient client = b.build();
        return client;
    } else {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // Increase default max connection per route to 20
        cm.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        cm.setMaxPerRoute(new HttpRoute(localhost), 10);
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build();
        return client;
    }
}

From source file:slash.navigation.rest.ssl.SSLConnectionManagerFactory.java

public HttpClientConnectionManager createConnectionManager() throws CertificateException,
        NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
    SSLContext sslContext = createSSLContext();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    return new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build());
}