Search in sources :

Example 31 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project validator by validator.

the class PrudentHttpEntityResolver method setParams.

/**
 * Sets the timeouts of the HTTP client.
 *
 * @param connectionTimeout
 *            timeout until connection established in milliseconds. Zero
 *            means no timeout.
 * @param socketTimeout
 *            timeout for waiting for data in milliseconds. Zero means no
 *            timeout.
 * @param maxRequests
 *            maximum number of connections to a particular host
 */
public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
    PrudentHttpEntityResolver.maxRequests = maxRequests;
    PoolingHttpClientConnectionManager phcConnMgr;
    // 
    Registry<ConnectionSocketFactory> registry = // 
    RegistryBuilder.<ConnectionSocketFactory>create().register("http", // 
    PlainConnectionSocketFactory.getSocketFactory()).register("https", // 
    SSLConnectionSocketFactory.getSocketFactory()).build();
    HttpClientBuilder builder = HttpClients.custom().useSystemProperties();
    builder.setRedirectStrategy(new LaxRedirectStrategy());
    builder.setMaxConnPerRoute(maxRequests);
    builder.setMaxConnTotal(Integer.parseInt(System.getProperty("nu.validator.servlet.max-total-connections", "200")));
    if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "true"))) {
        // 
        try {
            SSLContext promiscuousSSLContext = // 
            new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
            builder.setSslcontext(promiscuousSSLContext);
            // 
            HostnameVerifier verifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            // 
            SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
            registry = // 
            RegistryBuilder.<ConnectionSocketFactory>create().register("https", // 
            promiscuousSSLConnSocketFactory).register("http", // 
            PlainConnectionSocketFactory.getSocketFactory()).build();
        } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | NumberFormatException e) {
            e.printStackTrace();
        }
    }
    phcConnMgr = new PoolingHttpClientConnectionManager(registry);
    phcConnMgr.setDefaultMaxPerRoute(maxRequests);
    phcConnMgr.setMaxTotal(200);
    builder.setConnectionManager(phcConnMgr);
    RequestConfig.Builder config = RequestConfig.custom();
    config.setCircularRedirectsAllowed(true);
    config.setMaxRedirects(Integer.parseInt(System.getProperty("nu.validator.servlet.max-redirects", "20")));
    config.setConnectTimeout(connectionTimeout);
    config.setCookieSpec(CookieSpecs.BEST_MATCH);
    config.setSocketTimeout(socketTimeout);
    config.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
    client = builder.setDefaultRequestConfig(config.build()).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder)

Example 32 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project docker-client by spotify.

the class DefaultDockerClient method getSchemeRegistry.

private Registry<ConnectionSocketFactory> getSchemeRegistry(final Builder builder) {
    final SSLConnectionSocketFactory https;
    if (builder.dockerCertificatesStore == null) {
        https = SSLConnectionSocketFactory.getSocketFactory();
    } else {
        https = new SSLConnectionSocketFactory(builder.dockerCertificatesStore.sslContext(), builder.dockerCertificatesStore.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();
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory)

Example 33 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project ovirt-engine-sdk-java by oVirt.

the class ConnectionBuilder45 method createConnectionSocketFactoryRegistry.

private Registry createConnectionSocketFactoryRegistry() {
    String protocol = getProtocol();
    Registry registry = null;
    // Create SSL/TLS or plain connection:
    if (HTTP_PROTOCOL.equals(protocol)) {
        ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
        registry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP_PROTOCOL, plainsf).build();
    } else if (HTTPS_PROTOCOL.equals(protocol)) {
        try {
            LayeredConnectionSocketFactory sslsf = null;
            if (this.insecure) {
                SSLContext sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, new TrustManager[] { noCaTrustManager }, null);
                sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
            } else {
                SSLContextBuilder sslContextBuilder = SSLContexts.custom();
                if (trustStoreFile != null) {
                    sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), this.trustStorePassword != null ? this.trustStorePassword.toCharArray() : null);
                }
                SSLContext sslContext = sslContextBuilder.build();
                sslsf = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
            }
            registry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTPS_PROTOCOL, sslsf).build();
        } catch (NoSuchAlgorithmException e) {
            throw new Error(NO_TLS_ERROR, e);
        } catch (KeyManagementException e) {
            throw new Error(BAD_KEY_ERROR, e);
        } catch (KeyStoreException e) {
            throw new Error(KEY_STORE_ERROR, e);
        } catch (FileNotFoundException e) {
            throw new Error(KEY_STORE_FILE_NOT_FOUND_ERROR, e);
        } catch (CertificateException e) {
            throw new Error(CERTIFICATE_ERROR, e);
        } catch (IOException e) {
            throw new Error(IO_ERROR, e);
        }
    } else {
        throw new Error(BAD_PROTOCOL_ERROR + protocol);
    }
    return registry;
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) FileNotFoundException(java.io.FileNotFoundException) Error(org.ovirt.engine.sdk4.Error) CertificateException(java.security.cert.CertificateException) Registry(org.apache.http.config.Registry) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) File(java.io.File)

Example 34 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project opacclient by opacapp.

the class AdditionalKeyStoresSSLSocketFactory method create.

/**
 * Creates a customized keystore
 *
 * @param socketFactory The class that should be used to instantiate a new socket factory, must
 *                      be a subclass of {@link SSLConnectionSocketFactory}.
 * @return a new {@link SSLConnectionSocketFactory}
 */
public static SSLConnectionSocketFactory create(Class<?> socketFactory, X509TrustManager trustManager) throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { trustManager }, null);
    if (socketFactory != null) {
        try {
            return (SSLConnectionSocketFactory) socketFactory.getDeclaredConstructor(SSLContext.class).newInstance(sslContext);
        } catch (Exception e) {
            // Fall back to default
            e.printStackTrace();
        }
    }
    return new SSLConnectionSocketFactory(sslContext);
}
Also used : SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 35 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project opacclient by opacapp.

the class HttpClientFactory method getNewApacheHttpClient.

/**
 * Create a new HttpClient.
 *
 * @param tls_only If this is true, only TLS v1 and newer will be used, SSLv3 will be disabled.
 *                 We highly recommend to set this to true, if possible. This is currently a
 *                 no-op on the default implementation and only used in the Android
 *                 implementation!
 */
public HttpClient getNewApacheHttpClient(boolean customssl, boolean tls_only, boolean allCipherSuites, boolean disguise_app) {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setRedirectStrategy(new CustomRedirectStrategy());
    if (disguise_app) {
        builder.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, " + "like Gecko) Chrome/43.0.2357.130 Safari/537.36\t");
    } else {
        builder.setUserAgent(user_agent);
    }
    if (customssl && ssl_store_path != null) {
        try {
            if (trust_store == null) {
                trust_store = getKeyStore();
            }
            SSLConnectionSocketFactory sf = AdditionalKeyStoresSSLSocketFactory.create(getSocketFactoryClass(tls_only, allCipherSuites), new AdditionalKeyStoresSSLSocketFactory.AdditionalKeyStoresTrustManager(trust_store));
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sf).build();
            HttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
            builder.setConnectionManager(ccm);
            return builder.build();
        } catch (Exception e) {
            e.printStackTrace();
            return builder.build();
        }
    } else {
        return builder.build();
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ProtocolException(org.apache.http.ProtocolException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CircularRedirectException(org.apache.http.client.CircularRedirectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)179 SSLContext (javax.net.ssl.SSLContext)109 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)72 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)61 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)58 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)58 IOException (java.io.IOException)49 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)45 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)41 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 KeyManagementException (java.security.KeyManagementException)34 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)33 HttpClient (org.apache.http.client.HttpClient)28 RequestConfig (org.apache.http.client.config.RequestConfig)28 KeyStoreException (java.security.KeyStoreException)27 KeyStore (java.security.KeyStore)26 HostnameVerifier (javax.net.ssl.HostnameVerifier)26 CertificateException (java.security.cert.CertificateException)24 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)24