Search in sources :

Example 6 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project Taier by DTStack.

the class PoolHttpClient method getHttpClient.

private static CloseableHttpClient getHttpClient() {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    cm.setMaxTotal(maxTotal);
    cm.setDefaultMaxPerRoute(maxPerRoute);
    // 设置请求和传输超时时间
    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(ConnectTimeout).setSocketTimeout(SocketTimeout).setConnectTimeout(ConnectTimeout).build();
    return HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm).setRetryHandler(new RdosHttpRequestRetryHandler()).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) 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) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 7 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project jeecg-boot by jeecgboot.

the class CASServiceUtil method createHttpClientWithNoSsl.

/**
 * 创建模拟客户端(针对 https 客户端禁用 SSL 验证)
 *
 * @param cookieStore 缓存的 Cookies 信息
 * @return
 * @throws Exception
 */
private static CloseableHttpClient createHttpClientWithNoSsl() throws Exception {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        // don't check
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        // don't check
        }
    } };
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, trustAllCerts, null);
    LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(ctx);
    return HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 8 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project jersey by eclipse-ee4j.

the class ApacheConnector method createConnectionManager.

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;
    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;
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) ManagedHttpClientConnectionFactory(org.apache.http.impl.conn.ManagedHttpClientConnectionFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 9 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project dcache by dCache.

the class GsiHttpClientSender method createSocketFactoryRegistry.

@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();
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)

Example 10 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory 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)

Aggregations

LayeredConnectionSocketFactory (org.apache.http.conn.socket.LayeredConnectionSocketFactory)27 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)19 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)16 SSLContext (javax.net.ssl.SSLContext)13 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)13 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)12 KeyManagementException (java.security.KeyManagementException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Socket (java.net.Socket)5 KeyStore (java.security.KeyStore)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 HttpHost (org.apache.http.HttpHost)5 HttpClient (org.apache.http.client.HttpClient)4 File (java.io.File)3 InetSocketAddress (java.net.InetSocketAddress)3 KeyStoreException (java.security.KeyStoreException)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 SocketConfig (org.apache.http.config.SocketConfig)3