use of org.apache.http.conn.socket.ConnectionSocketFactory in project dropwizard by dropwizard.
the class HttpClientBuilderTest method canUseACustomHostnameVerifierWhenTlsConfigurationSpecified.
@Test
public void canUseACustomHostnameVerifierWhenTlsConfigurationSpecified() throws Exception {
final TlsConfiguration tlsConfiguration = new TlsConfiguration();
tlsConfiguration.setVerifyHostname(true);
configuration.setTlsConfiguration(tlsConfiguration);
final HostnameVerifier customVerifier = (s, sslSession) -> false;
final Registry<ConnectionSocketFactory> configuredRegistry;
configuredRegistry = builder.using(configuration).using(customVerifier).createConfiguredRegistry();
assertThat(configuredRegistry).isNotNull();
final SSLConnectionSocketFactory socketFactory = (SSLConnectionSocketFactory) configuredRegistry.lookup("https");
assertThat(socketFactory).isNotNull();
final Field hostnameVerifierField = FieldUtils.getField(SSLConnectionSocketFactory.class, "hostnameVerifier", true);
assertThat(hostnameVerifierField.get(socketFactory)).isSameAs(customVerifier);
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project opennms by OpenNMS.
the class HttpClientWrapper method configureSSLContext.
protected void configureSSLContext(final HttpClientBuilder builder) {
final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
for (final Map.Entry<String, SSLContext> entry : m_sslContext.entrySet()) {
final SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(entry.getValue(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registryBuilder.register(entry.getKey(), sslConnectionFactory);
}
if (!m_sslContext.containsKey("http")) {
registryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
}
if (!m_sslContext.containsKey("https")) {
registryBuilder.register("https", SSLConnectionSocketFactory.getSystemSocketFactory());
}
final HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registryBuilder.build());
builder.setConnectionManager(ccm);
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project voltdb by VoltDB.
the class TestJSONOverHttps method callProcOverJSON.
private String callProcOverJSON(String varString, final int expectedCode) throws Exception {
URI uri = URI.create("https://localhost:" + m_port + "/api/1.0/");
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sf).build();
// allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClientBuilder b = HttpClientBuilder.create();
b.setSslcontext(sslContext);
b.setConnectionManager(connMgr);
try (CloseableHttpClient httpclient = b.build()) {
HttpPost post = new HttpPost(uri);
// play nice by using HTTP 1.1 continue requests where the client sends the request headers first
// to the server to see if the server is willing to accept it. This allows us to test large requests
// without incurring server socket connection terminations
RequestConfig rc = RequestConfig.copy(RequestConfig.DEFAULT).setExpectContinueEnabled(true).build();
post.setProtocolVersion(HttpVersion.HTTP_1_1);
post.setConfig(rc);
post.setEntity(new StringEntity(varString, utf8ApplicationFormUrlEncoded));
ResponseHandler<String> rh = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
assertEquals(expectedCode, status);
if ((status >= 200 && status < 300) || status == 400) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
return null;
}
};
return httpclient.execute(post, rh);
}
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project calcite-avatica by apache.
the class AvaticaCommonsHttpClientImpl method initializeClient.
private void initializeClient() {
SSLConnectionSocketFactory sslFactory = null;
if (null != truststore && null != truststorePassword) {
try {
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(truststore, truststorePassword.toCharArray()).build();
sslFactory = new SSLConnectionSocketFactory(sslcontext);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
LOG.debug("Not configuring HTTPS because of missing truststore/password");
}
RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
registryBuilder.register("http", PlainConnectionSocketFactory.getSocketFactory());
// Only register the SSL factory when provided
if (null != sslFactory) {
registryBuilder.register("https", sslFactory);
}
pool = new PoolingHttpClientConnectionManager(registryBuilder.build());
// Increase max total connection to 100
final String maxCnxns = System.getProperty(MAX_POOLED_CONNECTIONS_KEY, MAX_POOLED_CONNECTIONS_DEFAULT);
pool.setMaxTotal(Integer.parseInt(maxCnxns));
// Increase default max connection per route to 25
final String maxCnxnsPerRoute = System.getProperty(MAX_POOLED_CONNECTION_PER_ROUTE_KEY, MAX_POOLED_CONNECTION_PER_ROUTE_DEFAULT);
pool.setDefaultMaxPerRoute(Integer.parseInt(maxCnxnsPerRoute));
this.authCache = new BasicAuthCache();
// A single thread-safe HttpClient, pooling connections via the ConnectionManager
this.client = HttpClients.custom().setConnectionManager(pool).build();
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project cloudstack by apache.
the class HttpClientHelper method createHttpClient.
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
final BasicCookieStore cookieStore = new BasicCookieStore();
return HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)).setRedirectStrategy(new LaxRedirectStrategy()).setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build()).setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}
Aggregations