use of org.apache.http.conn.socket.ConnectionSocketFactory in project voltdb by VoltDB.
the class TestJSONInterface method httpUrlOverJSONExecute.
private static String httpUrlOverJSONExecute(String method, String url, String user, String password, String scheme, int expectedCode, String expectedCt, String varString) throws Exception {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (X509Certificate[] arg0, String arg1) -> 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 hb = HttpClientBuilder.create();
hb.setSslcontext(sslContext);
hb.setConnectionManager(connMgr);
try (CloseableHttpClient httpclient = hb.build()) {
HttpRequestBase request;
switch(method) {
case "POST":
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(varString, utf8ApplicationFormUrlEncoded));
request = post;
break;
case "PUT":
HttpPut put = new HttpPut(url);
put.setEntity(new StringEntity(varString, utf8ApplicationFormUrlEncoded));
request = put;
break;
case "DELETE":
HttpDelete delete = new HttpDelete(url);
request = delete;
break;
case "GET":
request = new HttpGet(url + ((varString != null && varString.trim().length() > 0) ? ("?" + varString.trim()) : ""));
break;
default:
request = new HttpGet(url + ((varString != null && varString.trim().length() > 0) ? ("?" + varString.trim()) : ""));
break;
}
// 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();
request.setProtocolVersion(HttpVersion.HTTP_1_1);
request.setConfig(rc);
if (user != null && password != null) {
if (scheme.equalsIgnoreCase("hashed")) {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hashedPasswordBytes = md.digest(password.getBytes("UTF-8"));
String h = user + ":" + Encoder.hexEncode(hashedPasswordBytes);
request.setHeader("Authorization", "Hashed " + h);
} else if (scheme.equalsIgnoreCase("hashed256")) {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashedPasswordBytes = md.digest(password.getBytes("UTF-8"));
String h = user + ":" + Encoder.hexEncode(hashedPasswordBytes);
request.setHeader("Authorization", "Hashed " + h);
} else if (scheme.equalsIgnoreCase("basic")) {
request.setHeader("Authorization", "Basic " + new String(Base64.encodeToString(new String(user + ":" + password).getBytes(), false)));
}
}
ResponseHandler<String> rh = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
String ct = response.getHeaders("Content-Type")[0].getValue();
if (expectedCt != null) {
assertTrue(ct.contains(expectedCt));
}
assertEquals(expectedCode, status);
if ((status >= 200 && status < 300) || HANDLED_CLIENT_ERRORS.contains(status)) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
return null;
}
};
return httpclient.execute(request, rh);
}
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project cloudstack by apache.
the class HttpClientHelper method createSocketFactoryConfigration.
private static Registry<ConnectionSocketFactory> createSocketFactoryConfigration() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
Registry<ConnectionSocketFactory> socketFactoryRegistry;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
final SSLConnectionSocketFactory cnnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTPS, cnnectionSocketFactory).build();
return socketFactoryRegistry;
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project cdap-ingest by caskdata.
the class StreamReader method createConnectionManager.
private PoolingHttpClientConnectionManager createConnectionManager() throws NoSuchAlgorithmException, KeyManagementException {
Registry<ConnectionSocketFactory> connectionRegistry = null;
if (!verifySSLCert) {
connectionRegistry = RestUtil.getRegistryWithDisabledCertCheck();
}
PoolingHttpClientConnectionManager clientConnectionManager;
if (connectionRegistry != null) {
clientConnectionManager = new PoolingHttpClientConnectionManager(connectionRegistry);
} else {
clientConnectionManager = new PoolingHttpClientConnectionManager();
}
return clientConnectionManager;
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project dal by ctripcorp.
the class WebUtil method initWeakSSLClient.
private static HttpClient initWeakSSLClient() {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) {
return true;
}
}).build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
// do nothing, has been handled outside
}
b.setSslcontext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
X509HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
/**
* Set timeout option
*/
RequestConfig.Builder configBuilder = RequestConfig.custom();
configBuilder.setConnectTimeout(TIMEOUT);
configBuilder.setSocketTimeout(TIMEOUT);
b.setDefaultRequestConfig(configBuilder.build());
// finally, build the HttpClient;
// -- done!
HttpClient sslClient = b.build();
return sslClient;
}
use of org.apache.http.conn.socket.ConnectionSocketFactory in project iTest by e-government-ua.
the class DeleteTask method createHttpClient_AcceptsUntrustedCerts.
public HttpClient createHttpClient_AcceptsUntrustedCerts() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSslcontext(sslContext);
HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
HttpClient client = b.build();
return client;
}
Aggregations