Search in sources :

Example 16 with LayeredConnectionSocketFactory

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

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 17 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project cloudbreak by hortonworks.

the class MockSetup method disableSSLCheck.

private void disableSSLCheck() {
    try {
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
        LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        Unirest.setHttpClient(httpclient);
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException ignored) {
        throw new RuntimeException("can't create ssl settings");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) KeyManagementException(java.security.KeyManagementException)

Example 18 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project java by wavefrontHQ.

the class HttpClientTest method httpClientTimeoutsWork.

@Test(expected = ProcessingException.class)
public void httpClientTimeoutsWork() throws Exception {
    ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
    factory.registerProvider(JsonNodeWriter.class);
    factory.registerProvider(ResteasyJackson2Provider.class);
    HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().setMaxConnTotal(200).setMaxConnPerRoute(100).setConnectionTimeToLive(1, TimeUnit.MINUTES).setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(100).build()).setDefaultRequestConfig(RequestConfig.custom().setContentCompressionEnabled(true).setRedirectsEnabled(true).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(60000).build()).setSSLSocketFactory(new LayeredConnectionSocketFactory() {

        @Override
        public Socket createLayeredSocket(Socket socket, String target, int port, HttpContext context) throws IOException, UnknownHostException {
            return SSLConnectionSocketFactory.getSystemSocketFactory().createLayeredSocket(socket, target, port, context);
        }

        @Override
        public Socket createSocket(HttpContext context) throws IOException {
            return SSLConnectionSocketFactory.getSystemSocketFactory().createSocket(context);
        }

        @Override
        public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException {
            assertTrue("Non-zero timeout passed to connect socket is expected", connectTimeout > 0);
            throw new ProcessingException("OK");
        }
    }).build();
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(new ApacheHttpClient4Engine(httpClient, true)).providerFactory(factory).build();
    SocketServerRunnable sr = new SocketServerRunnable();
    Thread serverThread = new Thread(sr);
    serverThread.start();
    ResteasyWebTarget target = client.target("https://localhost:" + sr.getPort());
    SimpleRESTEasyAPI proxy = target.proxy(SimpleRESTEasyAPI.class);
    proxy.search("resteasy");
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) InetSocketAddress(java.net.InetSocketAddress) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) HttpContext(org.apache.http.protocol.HttpContext) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 19 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project gateway-dubbox by zhuzhong.

the class OpenApiHttpliClientFluentServiceImpl method initHttpClient.

private void initHttpClient() {
    LayeredConnectionSocketFactory ssl = null;
    try {
        ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
    } catch (final SSLInitializationException ex) {
        final SSLContext sslcontext;
        try {
            sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
            sslcontext.init(null, null, null);
            ssl = new SSLConnectionSocketFactory(sslcontext);
        } catch (final SecurityException ignore) {
        } catch (final KeyManagementException ignore) {
        } catch (final NoSuchAlgorithmException ignore) {
        }
    }
    final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory()).build();
    manager = new PoolingHttpClientConnectionManager(sfr);
    manager.setDefaultMaxPerRoute(maxPerRoute);
    manager.setMaxTotal(maxTotal);
    client = HttpClientBuilder.create().setConnectionManager(manager).build();
    executor = Executor.newInstance(client);
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLInitializationException(org.apache.http.conn.ssl.SSLInitializationException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 20 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project google-api-java-client by google.

the class GoogleApacheHttpTransport method newTrustedTransport.

/**
 * Returns a new instance of {@link ApacheHttpTransport} that uses {@link
 * GoogleUtils#getCertificateTrustStore()} for the trusted certificates.
 *
 * @deprecated Use
 *     com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport.newTrustedTransport()
 */
public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException {
    // Set socket buffer sizes to 8192
    SocketConfig socketConfig = SocketConfig.custom().setRcvBufSize(8192).setSndBufSize(8192).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);
    // Disable the stale connection check (previously configured in the HttpConnectionParams
    connectionManager.setValidateAfterInactivity(-1);
    // Use the included trust store
    KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
    SSLContext sslContext = SslUtils.getTlsSslContext();
    SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory());
    LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
    HttpClient client = HttpClientBuilder.create().useSystemProperties().setSSLSocketFactory(socketFactory).setDefaultSocketConfig(socketConfig).setMaxConnTotal(200).setMaxConnPerRoute(20).setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())).setConnectionManager(connectionManager).disableRedirectHandling().disableAutomaticRetries().build();
    return new ApacheHttpTransport(client);
}
Also used : SocketConfig(org.apache.http.config.SocketConfig) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) HttpClient(org.apache.http.client.HttpClient) SSLContext(javax.net.ssl.SSLContext) ApacheHttpTransport(com.google.api.client.http.apache.ApacheHttpTransport) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

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