org.apache.http.nio.conn.SchemeIOSessionStrategy Java Examples

The following examples show how to use org.apache.http.nio.conn.SchemeIOSessionStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JKSCertInfoTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void paramsArePassedToConfiguredObject() {

    // given
    JKSCertInfo certInfo = createTestCertInfoBuilder()
            .withKeystorePath(TEST_KEYSTORE_PATH)
            .withKeystorePassword(TEST_KEYSTORE_PASSWORD)
            .withTruststorePath(TEST_TRUSTSTORE_PATH)
            .withTruststorePassword(TEST_TRUSTSTORE_PASSWORD)
            .build();

    HttpClientConfig.Builder clientConfigBuilder = spy(createDefaultClientConfigBuilder());

    // when
    certInfo.applyTo(clientConfigBuilder);

    // then
    verify(clientConfigBuilder).httpsIOSessionStrategy((SchemeIOSessionStrategy) notNull());
    Assert.assertNotNull(clientConfigBuilder.build().getHttpsIOSessionStrategy());
}
 
Example #2
Source File: PEMCertInfoTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void paramsArePassedToConfiguredObject() {

    // given
    PEMCertInfo certInfo = createTestCertInfoBuilder()
            .withKeyPath(TEST_KEY_PATH)
            .withKeyPassphrase(TEST_KEY_PASSPHRASE)
            .withClientCertPath(TEST_CLIENT_CERT_PATH)
            .withCaPath(TEST_CA_PATH)
            .build();

    HttpClientConfig.Builder clientConfigBuilder = spy(createDefaultClientConfigBuilder());

    // when
    certInfo.applyTo(clientConfigBuilder);

    // then
    verify(clientConfigBuilder).httpsIOSessionStrategy((SchemeIOSessionStrategy) notNull());
    Assert.assertNotNull(clientConfigBuilder.build().getHttpsIOSessionStrategy());
}
 
Example #3
Source File: JKSCertInfoTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void paramsArePassedToConfiguredObject() {

    // given
    JKSCertInfo certInfo = createTestCertInfoBuilder()
            .withKeystorePath(TEST_KEYSTORE_PATH)
            .withKeystorePassword(TEST_KEYSTORE_PASSWORD)
            .withTruststorePath(TEST_TRUSTSTORE_PATH)
            .withTruststorePassword(TEST_TRUSTSTORE_PASSWORD)
            .build();

    HttpClientFactory.Builder httpClientFactoryBuilder = Mockito.spy(createDefaultHttpClientFactoryBuilder());

    // when
    certInfo.applyTo(httpClientFactoryBuilder);

    // then
    verify(httpClientFactoryBuilder).withHttpsIOSessionStrategy((SchemeIOSessionStrategy) notNull());
    Assert.assertNotNull(httpClientFactoryBuilder.build().httpsIOSessionStrategy);
}
 
Example #4
Source File: PEMCertInfoTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void paramsArePassedToConfiguredObject() {

    // given
    PEMCertInfo certInfo = createTestCertInfoBuilder()
            .withKeyPath(TEST_KEY_PATH)
            .withKeyPassphrase(TEST_KEY_PASSPHRASE)
            .withClientCertPath(TEST_CLIENT_CERT_PATH)
            .withCaPath(TEST_CA_PATH)
            .build();

    HttpClientFactory.Builder httpClientFactoryBuilder = spy(createDefaultTestObjectBuilder());

    // when
    certInfo.applyTo(httpClientFactoryBuilder);

    // then
    verify(httpClientFactoryBuilder).withHttpsIOSessionStrategy((SchemeIOSessionStrategy) notNull());
    Assert.assertNotNull(httpClientFactoryBuilder.build().httpIOSessionStrategy);
}
 
Example #5
Source File: AsyncHttpClientGenerator.java    From cetty with Apache License 2.0 5 votes vote down vote up
@Override
protected Registry<SchemeIOSessionStrategy> registry() {
    return RegistryBuilder
            .<SchemeIOSessionStrategy>create()
            .register("http", NoopIOSessionStrategy.INSTANCE)
            .register("https", buildSSLIOSessionStrategy())
            .build();
}
 
Example #6
Source File: ExtendedJestClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
Registry<SchemeIOSessionStrategy> createSchemeIOSessionStrategyRegistry() {
    HttpClientConfig httpClientConfig = wrappedHttpClientConfig.getHttpClientConfig();
    return RegistryBuilder.<SchemeIOSessionStrategy>create()
                .register("http", httpClientConfig.getHttpIOSessionStrategy())
                .register("https", httpClientConfig.getHttpsIOSessionStrategy())
                .build();
}
 
Example #7
Source File: RestClient.java    From light with Apache License 2.0 5 votes vote down vote up
private Registry<SchemeIOSessionStrategy> asyncRegistry() throws Exception {
    // Allow TLSv1 protocol only
    SSLIOSessionStrategy sslSessionStrategy = new SSLIOSessionStrategy(
            sslContext(),
            new String[] { "TLSv1" },
            null,
            hostnameVerifier());

    // Create a registry of custom connection session strategies for supported
    // protocol schemes.
    return RegistryBuilder.<SchemeIOSessionStrategy>create()
            .register("http", NoopIOSessionStrategy.INSTANCE)
            .register("https", sslSessionStrategy)
            .build();
}
 
Example #8
Source File: DefaultEsClientFactory.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the SSL connection to use certificates by setting the keystores
 * @param httpConfig the http client configuration
 * @param config the configuration
 */
@SuppressWarnings("nls")
private void updateSslConfig(Builder httpConfig, Map<String, String> config) {
    try {
        String clientKeystorePath = config.get("client.keystore");
        String clientKeystorePassword = config.get("client.keystore.password");
        String trustStorePath = config.get("client.truststore");
        String trustStorePassword = config.get("client.truststore.password");

        SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();

        String trustCertificate = config.get("client.trust.certificate");
        if (!StringUtils.isBlank(trustCertificate) && trustCertificate.equals("true")) {
            sslContextBuilder = sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());
        }

        SSLContext sslContext = sslContextBuilder.build();
        Info kPathInfo = new Info(clientKeystorePath, clientKeystorePassword);
        Info tPathInfo = new Info(trustStorePath, trustStorePassword);
        sslContext.init(KeyStoreUtil.getKeyManagers(kPathInfo), KeyStoreUtil.getTrustManagers(tPathInfo), new SecureRandom());

        String trustHost = config.get("client.trust.host");
        HostnameVerifier hostnameVerifier = !StringUtils.isBlank(trustHost) && trustHost.equals("true") ? NoopHostnameVerifier.INSTANCE : new DefaultHostnameVerifier();

        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);

        httpConfig.defaultSchemeForDiscoveredNodes("https");
        httpConfig.sslSocketFactory(sslSocketFactory); // for sync calls
        httpConfig.httpsIOSessionStrategy(httpsIOSessionStrategy); // for async calls

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: AutoCleanedPoolingNHttpClientConnectionManager.java    From caravan with Apache License 2.0 4 votes vote down vote up
protected static Registry<SchemeIOSessionStrategy> getDefaultRegistry() {
    return RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE)
            .register("https", SSLIOSessionStrategy.getDefaultStrategy()).build();
}
 
Example #10
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void registryBuilderUsesProvidedHttpIOSessionStrategy() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfigBuilder();

    SchemeIOSessionStrategy expectedSchemeIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    httpClientConfigBuilder.httpIOSessionStrategy(expectedSchemeIOSessionStrategy);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(httpClientConfigBuilder.build());
    ExtendedJestClientFactory factory = new ExtendedJestClientFactory(builder.build());

    // when
    Registry<SchemeIOSessionStrategy> registry = factory.createSchemeIOSessionStrategyRegistry();

    // then
    assertEquals(expectedSchemeIOSessionStrategy, registry.lookup("http"));

}
 
Example #11
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void registryBuilderUsesProvidedHttpsIOSessionStrategy() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfigBuilder();

    SchemeIOSessionStrategy expectedSchemeIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    httpClientConfigBuilder.httpsIOSessionStrategy(expectedSchemeIOSessionStrategy);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(httpClientConfigBuilder.build());
    ExtendedJestClientFactory factory = new ExtendedJestClientFactory(builder.build());

    // when
    Registry<SchemeIOSessionStrategy> registry = factory.createSchemeIOSessionStrategyRegistry();

    // then
    assertEquals(expectedSchemeIOSessionStrategy, registry.lookup("https"));

}
 
Example #12
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
Registry<SchemeIOSessionStrategy> createSchemeIOSessionStrategyRegistry() {
    return RegistryBuilder.<SchemeIOSessionStrategy>create()
            .register("http", httpIOSessionStrategy)
            .register("https", httpsIOSessionStrategy)
            .build();
}
 
Example #13
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public Builder withHttpIOSessionStrategy(SchemeIOSessionStrategy httpIOSessionStrategy) {
    this.httpIOSessionStrategy = httpIOSessionStrategy;
    return this;
}
 
Example #14
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public Builder withHttpsIOSessionStrategy(SchemeIOSessionStrategy httpsIOSessionStrategy) {
    this.httpsIOSessionStrategy = httpsIOSessionStrategy;
    return this;
}
 
Example #15
Source File: HttpClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void builderSetsAllFields() {

    HttpClientFactory.Builder builder = new HttpClientFactory.Builder();

    ConnectionSocketFactory plainSocketFactory = mock(ConnectionSocketFactory.class);
    LayeredConnectionSocketFactory sslSocketFactory = mock(LayeredConnectionSocketFactory.class);
    SchemeIOSessionStrategy httpIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    SchemeIOSessionStrategy httpsIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    CredentialsProvider credentialsProvider = mock(CredentialsProvider.class);

    builder.withServerList(TEST_SERVER_LIST)
            .withConnTimeout(TEST_CONNECTION_TIMEOUT)
            .withReadTimeout(TEST_READ_TIMEOUT)
            .withMaxTotalConnections(TEST_MAX_TOTAL_CONNECTIONS)
            .withIoThreadCount(TEST_IO_THREAD_COUNT)
            .withPooledResponseBuffers(TEST_POOLED_RESPONSE_BUFFERS_ENABLED)
            .withPooledResponseBuffersSizeInBytes(TEST_POOLED_RESPONSE_BUFFERS_SIZE_IN_BYTES)
            .withPlainSocketFactory(plainSocketFactory)
            .withSslSocketFactory(sslSocketFactory)
            .withHttpIOSessionStrategy(httpIOSessionStrategy)
            .withHttpsIOSessionStrategy(httpsIOSessionStrategy)
            .withDefaultCredentialsProvider(credentialsProvider);

    // when
    HttpClientFactory httpClientFactory = builder.build();

    // then
    assertEquals(TEST_SERVER_LIST, httpClientFactory.serverList);
    assertEquals(TEST_CONNECTION_TIMEOUT, httpClientFactory.connTimeout);
    assertEquals(TEST_READ_TIMEOUT, httpClientFactory.readTimeout);
    assertEquals(TEST_MAX_TOTAL_CONNECTIONS, httpClientFactory.maxTotalConnections);
    assertEquals(TEST_IO_THREAD_COUNT, httpClientFactory.ioThreadCount);
    assertEquals(TEST_POOLED_RESPONSE_BUFFERS_ENABLED, httpClientFactory.pooledResponseBuffersEnabled);
    assertEquals(TEST_POOLED_RESPONSE_BUFFERS_SIZE_IN_BYTES, httpClientFactory.pooledResponseBuffersSizeInBytes);
    assertEquals(plainSocketFactory, httpClientFactory.plainSocketFactory);
    assertEquals(sslSocketFactory, httpClientFactory.sslSocketFactory);
    assertEquals(httpIOSessionStrategy, httpClientFactory.httpIOSessionStrategy);
    assertEquals(httpsIOSessionStrategy, httpClientFactory.httpsIOSessionStrategy);
    assertEquals(credentialsProvider, httpClientFactory.defaultCredentialsProvider);

}
 
Example #16
Source File: HttpClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void builderSetsDefaultFields() {

    HttpClientFactory.Builder builder = new HttpClientFactory.Builder();

    ConnectionSocketFactory plainSocketFactory = mock(ConnectionSocketFactory.class);
    LayeredConnectionSocketFactory sslSocketFactory = mock(LayeredConnectionSocketFactory.class);
    SchemeIOSessionStrategy httpIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    SchemeIOSessionStrategy httpsIOSessionStrategy = mock(SchemeIOSessionStrategy.class);
    CredentialsProvider credentialsProvider = mock(CredentialsProvider.class);

    builder.withServerList(TEST_SERVER_LIST)
            .withConnTimeout(TEST_CONNECTION_TIMEOUT)
            .withReadTimeout(TEST_READ_TIMEOUT)
            .withMaxTotalConnections(TEST_MAX_TOTAL_CONNECTIONS)
            .withIoThreadCount(TEST_IO_THREAD_COUNT)
            .withPooledResponseBuffers(TEST_POOLED_RESPONSE_BUFFERS_ENABLED)
            .withPooledResponseBuffersSizeInBytes(TEST_POOLED_RESPONSE_BUFFERS_SIZE_IN_BYTES)
            .withPlainSocketFactory(plainSocketFactory)
            .withSslSocketFactory(sslSocketFactory)
            .withHttpIOSessionStrategy(httpIOSessionStrategy)
            .withHttpsIOSessionStrategy(httpsIOSessionStrategy)
            .withDefaultCredentialsProvider(credentialsProvider);

    // when
    HttpClientFactory httpClientFactory = builder.build();

    // then
    assertEquals(TEST_SERVER_LIST, httpClientFactory.serverList);
    assertEquals(TEST_CONNECTION_TIMEOUT, httpClientFactory.connTimeout);
    assertEquals(TEST_READ_TIMEOUT, httpClientFactory.readTimeout);
    assertEquals(TEST_MAX_TOTAL_CONNECTIONS, httpClientFactory.maxTotalConnections);
    assertEquals(TEST_IO_THREAD_COUNT, httpClientFactory.ioThreadCount);
    assertEquals(TEST_POOLED_RESPONSE_BUFFERS_ENABLED, httpClientFactory.pooledResponseBuffersEnabled);
    assertEquals(TEST_POOLED_RESPONSE_BUFFERS_SIZE_IN_BYTES, httpClientFactory.pooledResponseBuffersSizeInBytes);
    assertNotNull(httpClientFactory.plainSocketFactory);
    assertNotNull(httpClientFactory.sslSocketFactory);
    assertNotNull(httpClientFactory.httpIOSessionStrategy);
    assertNotNull(httpClientFactory.httpsIOSessionStrategy);
    assertNotNull(httpClientFactory.defaultCredentialsProvider);

}