use of org.apache.http.impl.client.HttpClientBuilder 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.impl.client.HttpClientBuilder in project lucene-solr by apache.
the class HttpClientUtil method setupBuilder.
private static HttpClientBuilder setupBuilder(HttpClientBuilder builder, SolrParams config) {
Builder requestConfigBuilder = RequestConfig.custom().setRedirectsEnabled(config.getBool(HttpClientUtil.PROP_FOLLOW_REDIRECTS, false)).setDecompressionEnabled(false).setConnectTimeout(config.getInt(HttpClientUtil.PROP_CONNECTION_TIMEOUT, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(config.getInt(HttpClientUtil.PROP_SO_TIMEOUT, DEFAULT_SO_TIMEOUT));
String cpolicy = cookiePolicy;
if (cpolicy != null) {
requestConfigBuilder.setCookieSpec(cpolicy);
}
RequestConfig requestConfig = requestConfigBuilder.build();
HttpClientBuilder retBuilder = builder.setDefaultRequestConfig(requestConfig);
if (config.getBool(HttpClientUtil.PROP_USE_RETRY, true)) {
retBuilder = retBuilder.setRetryHandler(new SolrHttpRequestRetryHandler(3));
} else {
retBuilder = retBuilder.setRetryHandler(NO_RETRY);
}
final String basicAuthUser = config.get(HttpClientUtil.PROP_BASIC_AUTH_USER);
final String basicAuthPass = config.get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
if (basicAuthUser != null && basicAuthPass != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUser, basicAuthPass));
retBuilder.setDefaultCredentialsProvider(credsProvider);
}
if (config.getBool(HttpClientUtil.PROP_ALLOW_COMPRESSION, false)) {
retBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
retBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
} else {
retBuilder.disableContentCompression();
}
return retBuilder;
}
use of org.apache.http.impl.client.HttpClientBuilder in project sling by apache.
the class TopologyConnectorClient method createHttpClient.
private CloseableHttpClient createHttpClient() {
final HttpClientBuilder builder = HttpClientBuilder.create();
// setting the SoTimeout (which is configured in seconds)
builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(1000 * config.getSoTimeout()).build());
builder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
return builder.build();
}
use of org.apache.http.impl.client.HttpClientBuilder in project ddf by codice.
the class HttpSolrClientFactory method getSecureHttpClient.
private static CloseableHttpClient getSecureHttpClient(boolean retryRequestsOnError) {
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(getSslContext(), getProtocols(), getCipherSuites(), SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
HttpRequestRetryHandler solrRetryHandler = new SolrHttpRequestRetryHandler();
HttpClientBuilder builder = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).setDefaultCookieStore(new BasicCookieStore()).setMaxConnTotal(128).setMaxConnPerRoute(32);
if (retryRequestsOnError) {
builder.setRetryHandler(solrRetryHandler);
}
return builder.build();
}
use of org.apache.http.impl.client.HttpClientBuilder 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