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:leap.lang.http.client.apache.ApacheHttpClient.java

protected Registry<ConnectionSocketFactory> getDefaultRegistry() {
    RegistryBuilder<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create();

    reg.register("http", PlainConnectionSocketFactory.getSocketFactory());

    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(SSL_CONTEXT,
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    reg.register("https", sslSocketFactory);

    return reg.build();
}

From source file:org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl.java

private void initializeClient() {
    SSLConnectionSocketFactory sslFactory = null;
    if (null != truststore && null != truststorePassword) {
        try {/*from   w w w. j a v a2  s . c  om*/
            SSLContext sslcontext = SSLContexts.custom()
                    .loadTrustMaterial(truststore, truststorePassword.toCharArray()).build();
            sslFactory = new SSLConnectionSocketFactory(sslcontext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        LOG.debug("Not configuring HTTPS because of missing truststore/password");
    }

    RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
    registryBuilder.register("http", PlainConnectionSocketFactory.getSocketFactory());
    // Only register the SSL factory when provided
    if (null != sslFactory) {
        registryBuilder.register("https", sslFactory);
    }
    pool = new PoolingHttpClientConnectionManager(registryBuilder.build());
    // Increase max total connection to 100
    final String maxCnxns = System.getProperty(MAX_POOLED_CONNECTIONS_KEY, MAX_POOLED_CONNECTIONS_DEFAULT);
    pool.setMaxTotal(Integer.parseInt(maxCnxns));
    // Increase default max connection per route to 25
    final String maxCnxnsPerRoute = System.getProperty(MAX_POOLED_CONNECTION_PER_ROUTE_KEY,
            MAX_POOLED_CONNECTION_PER_ROUTE_DEFAULT);
    pool.setDefaultMaxPerRoute(Integer.parseInt(maxCnxnsPerRoute));

    this.authCache = new BasicAuthCache();

    // A single thread-safe HttpClient, pooling connections via the ConnectionManager
    this.client = HttpClients.custom().setConnectionManager(pool).build();
}

From source file:com.fourspaces.couchdb.Session.java

/**
 * Constructor for obtaining a Session with an HTTP-AUTH username/password
 * and (optionally) a secure connection This isn't supported by CouchDB -
 * you need a proxy in front to use this
 *
 * @param host - hostname/*from ww  w .  ja v  a 2 s .  co  m*/
 * @param port - port to use
 * @param user - username
 * @param pass - password
 * @param usesAuth
 * @param secure - use an SSL connection?
 */
public Session(String host, int port, String user, String pass, boolean usesAuth, boolean secure) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.pass = pass;
    this.usesAuth = usesAuth;
    this.secure = secure;

    PlainConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
    Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", plainsf).register("https", sslsf).build();
    this.connManager = new PoolingHttpClientConnectionManager(r);

    this.requestConfig = RequestConfig.custom().setConnectTimeout(15 * 1000).setSocketTimeout((30 * 1000))
            .setAuthenticationEnabled(usesAuth).build();

    this.httpContext = HttpClientContext.create();
    if (user != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
        this.httpContext.setCredentialsProvider(credsProvider);
    }

    this.httpClient = createHttpClient();
}

From source file:org.callimachusproject.client.HttpClientFactory.java

private HttpClientFactory(File cacheDir) throws IOException {
    cacheDir.mkdirs();/*from www  . j  a  v  a  2 s .  c  o m*/
    entryFactory = new FileResourceFactory(cacheDir);
    decorator = new ProxyClientExecDecorator();
    LayeredConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSystemSocketFactory();
    connManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build());
    connManager.setDefaultSocketConfig(getDefaultSocketConfig());
    connManager.setDefaultConnectionConfig(getDefaultConnectionConfig());
    int max = Integer.parseInt(System.getProperty("http.maxConnections", "20"));
    connManager.setDefaultMaxPerRoute(max);
    connManager.setMaxTotal(2 * max);
    reuseStrategy = DefaultConnectionReuseStrategy.INSTANCE;
    keepAliveStrategy = new ConnectionKeepAliveStrategy() {
        private final long KEEPALIVE = SystemProperties.getClientKeepAliveTimeout();
        private ConnectionKeepAliveStrategy delegate = DefaultConnectionKeepAliveStrategy.INSTANCE;

        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            long ret = delegate.getKeepAliveDuration(response, context);
            if (ret > 0)
                return ret;
            return KEEPALIVE;
        }
    };
}

From source file:org.ops4j.pax.web.itest.base.client.HttpComponentsWrapper.java

private CloseableHttpClient createHttpClient() throws KeyStoreException, IOException, NoSuchAlgorithmException,
        CertificateException, KeyManagementException {
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    SSLConnectionSocketFactory sslsf = null;
    try {/*w  w  w  .j a  va2  s .  c  o  m*/
        FileInputStream instream = new FileInputStream(new File(keyStore));
        try {
            trustStore.load(instream, "password".toCharArray());
        } finally {
            // CHECKSTYLE:OFF
            try {
                instream.close();
            } catch (Exception ignore) {
            }
            // CHECKSTYLE:ON
        }

        SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore).build();
        sslsf = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    } catch (FileNotFoundException e) {
        LOG.error("Error preparing SSL for testing. Https will not be available.", e);
    }

    PlainConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();

    RegistryBuilder<ConnectionSocketFactory> rb = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", plainsf);
    if (sslsf != null) {
        rb.register("https", sslsf);
    }

    Registry<ConnectionSocketFactory> registry = rb.build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

    return HttpClients.custom().setConnectionManager(cm).build();

}

From source file:com.ksc.http.apache.client.impl.ApacheConnectionManagerFactory.java

private Registry<ConnectionSocketFactory> createSocketFactoryRegistry(
        ConnectionSocketFactory sslSocketFactory) {

    /*/*from   w  w w.  j  a  v  a2s .c o  m*/
     * If SSL cert checking for endpoints has been explicitly disabled,
     * register a new scheme for HTTPS that won't cause self-signed certs to
     * error out.
     */
    if (SDKGlobalConfiguration.isCertCheckingDisabled()) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("SSL Certificate checking for endpoints has been " + "explicitly disabled.");
        }
        sslSocketFactory = new TrustingSocketFactory();
    }

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

From source file:com.mirth.connect.client.core.ServerConnection.java

public ServerConnection(int timeout, String[] httpsProtocols, String[] httpsCipherSuites, boolean allowHTTP) {
    SSLContext sslContext = null;
    try {//from   ww  w.  j  a va2  s  .com
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (Exception e) {
        logger.error("Unable to build SSL context.", e);
    }

    String[] enabledProtocols = MirthSSLUtil.getEnabledHttpsProtocols(httpsProtocols);
    String[] enabledCipherSuites = MirthSSLUtil.getEnabledHttpsCipherSuites(httpsCipherSuites);
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            enabledProtocols, enabledCipherSuites, NoopHostnameVerifier.INSTANCE);
    RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory);
    if (allowHTTP) {
        builder.register("http", PlainConnectionSocketFactory.getSocketFactory());
    }
    Registry<ConnectionSocketFactory> socketFactoryRegistry = builder.build();

    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    httpClientConnectionManager.setDefaultMaxPerRoute(5);
    httpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
    // MIRTH-3962: The stale connection settings has been deprecated, and this is recommended instead
    httpClientConnectionManager.setValidateAfterInactivity(5000);

    HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(httpClientConnectionManager);
    HttpUtil.configureClientBuilder(clientBuilder);

    client = clientBuilder.build();
    requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setConnectionRequestTimeout(CONNECT_TIMEOUT).setSocketTimeout(timeout).build();
}

From source file:com.qwazr.utils.http.HttpUtils.java

/**
 * Create a new HttpClient which accept untrusted SSL certificates
 *
 * @return a new HttpClient//from w  ww. j  a v a  2  s  .c om
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public static CloseableHttpClient createHttpClient_AcceptsUntrustedCerts()
        throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

    final HttpClientBuilder unsecureHttpClientBuilder = HttpClientBuilder.create();

    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            return true;
        }
    }).build();

    unsecureHttpClientBuilder.setSSLContext(sslContext);

    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();

    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    unsecureHttpClientBuilder.setConnectionManager(connMgr);
    return unsecureHttpClientBuilder.build();
}

From source file:com.axibase.tsd.client.HttpClient.java

static PoolingHttpClientConnectionManager createConnectionManager(ClientConfiguration clientConfiguration,
        SslConfigurator sslConfig) {/*from   ww  w  .ja  va2  s  .c  om*/
    SSLContext sslContext = sslConfig.createSSLContext();
    X509HostnameVerifier hostnameVerifier;
    if (clientConfiguration.isIgnoreSSLErrors()) {
        ignoreSslCertificateErrorInit(sslContext);
        hostnameVerifier = new AllowAllHostnameVerifier();
    } else {
        hostnameVerifier = new StrictHostnameVerifier();
    }

    LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
            hostnameVerifier);

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

From source file:com.hp.octane.integrations.services.rest.OctaneRestClientImpl.java

OctaneRestClientImpl(OctaneSDK.SDKServicesConfigurer configurer) {
    if (configurer == null) {
        throw new IllegalArgumentException("invalid configurer");
    }/*from   ww w.  jav a2s .c o  m*/

    this.configurer = configurer;

    SSLContext sslContext = SSLContexts.createSystemDefault();
    HostnameVerifier hostnameVerifier = new CustomHostnameVerifier();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_TOTAL_CONNECTIONS);

    HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connectionManager);

    httpClient = clientBuilder.build();
}