Search in sources :

Example 21 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project fess-crawler by codelibs.

the class HcHttpClient method buildConnectionManager.

protected HttpClientConnectionManager buildConnectionManager(final HttpClientBuilder httpClientBuilder) {
    // SSL
    final LayeredConnectionSocketFactory sslSocketFactory = buildSSLSocketFactory(httpClientBuilder);
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = // 
    RegistryBuilder.<ConnectionSocketFactory>create().register("http", // 
    PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
    final long timeToLive = getInitParameter(TIME_TO_LIVE_PROPERTY, 5L, Long.class);
    final TimeUnit timeUnit = TimeUnit.valueOf(getInitParameter(TIME_TO_LIVE_TIME_UNIT_PROPERTY, "MINUTES", String.class));
    final int maxTotal = getInitParameter(MAX_TOTAL_CONNECTION_PROPERTY, 200, Integer.class);
    final int defaultMaxPerRoute = getInitParameter(DEFAULT_MAX_CONNECTION_PER_ROUTE_PROPERTY, 20, Integer.class);
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, null, null, dnsResolver, timeToLive, timeUnit);
    connectionManager.setMaxTotal(maxTotal);
    connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
    return connectionManager;
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) TimeUnit(java.util.concurrent.TimeUnit) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 22 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project testobject-java-api by saucelabs.

the class TestObjectClientImpl method buildClient.

private Client buildClient(ProxySettings proxySettings) {
    X509HostnameVerifier defaultHostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SslConfigurator sslConfig = SslConfigurator.newInstance();
    LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslConfig.createSSLContext(), new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null, defaultHostnameVerifier);
    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
    ClientConfig config = new ClientConfig();
    config.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingHttpClientConnectionManager(registry));
    ApacheConnectorProvider connector = new ApacheConnectorProvider();
    config.connectorProvider(connector);
    config.register(MultiPartFeature.class);
    config.register(JacksonFeature.class);
    if (proxySettings != null) {
        config.property(ClientProperties.PROXY_URI, "http://" + proxySettings.getHost() + ":" + proxySettings.getPort());
        if (proxySettings.getUsername() != null) {
            config.property(ClientProperties.PROXY_USERNAME, proxySettings.getUsername());
            config.property(ClientProperties.PROXY_PASSWORD, proxySettings.getPassword());
        }
    }
    SSLContext sslContext = sslConfig.createSSLContext();
    return ClientBuilder.newBuilder().sslContext(sslContext).newClient(config);
}
Also used : 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) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SslConfigurator(org.glassfish.jersey.SslConfigurator) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 23 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project wavefront-proxy 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 24 with LayeredConnectionSocketFactory

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

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)

Example 25 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project sslcontext-kickstart by Hakky54.

the class Apache4SslUtilsShould method createLayeredConnectionSocketFactoryWithIdentityMaterialAndTrustMaterial.

@Test
void createLayeredConnectionSocketFactoryWithIdentityMaterialAndTrustMaterial() {
    KeyStore identity = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + IDENTITY_FILE_NAME, IDENTITY_PASSWORD);
    KeyStore trustStore = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + TRUSTSTORE_FILE_NAME, TRUSTSTORE_PASSWORD);
    SSLFactory sslFactory = SSLFactory.builder().withIdentityMaterial(identity, IDENTITY_PASSWORD).withTrustMaterial(trustStore).build();
    assertThat(sslFactory.getSslContext()).isNotNull();
    assertThat(sslFactory.getKeyManager()).isPresent();
    assertThat(sslFactory.getTrustManager()).isNotNull();
    assertThat(sslFactory.getTrustedCertificates()).isNotEmpty();
    assertThat(sslFactory.getTrustManager()).isNotNull();
    assertThat(sslFactory.getHostnameVerifier()).isNotNull();
    LayeredConnectionSocketFactory socketFactory = Apache4SslUtils.toSocketFactory(sslFactory);
    assertThat(socketFactory).isNotNull();
}
Also used : SSLFactory(nl.altindag.ssl.SSLFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) KeyStore(java.security.KeyStore) Test(org.junit.jupiter.api.Test)

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