* Instantiates a new apache http client.
*
*/
private ApacheHttpClient() {
final HttpClientBuilder builder = HttpClientBuilder.create();
// Allow self-signed SSL certificates:
try {
final SSLContextBuilder sslbuilder = new SSLContextBuilder();
sslbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslbuilder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(sslsf);
} catch (final Exception e) {
LOG.log(Level.WARNING, "Couldn't init SSL strategy", e);
}
// Work with PoolingClientConnectionManager
final HttpClientConnectionManager connection = new PoolingHttpClientConnectionManager();
builder.setConnectionManager(connection);
// Provide eviction thread to clear out stale threads.
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (this) {
wait(5000);
connection.closeExpiredConnections();
connection.closeIdleConnections(30,
TimeUnit.SECONDS);
}
}
} catch (final InterruptedException ex) {
}
}
}).start();
try {
builder.setDefaultCookieStore(new MyCookieStore());
} catch (final IOException e) {
LOG.log(Level.WARNING, "Couldn't init cookie store", e);
}
final RequestConfig globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
.setConnectTimeout(20000).setStaleConnectionCheckEnabled(false)
.build();
builder.setDefaultRequestConfig(globalConfig);
final SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(60000).setTcpNoDelay(true).build();
builder.setDefaultSocketConfig(socketConfig);
// generate httpclient
httpClient = builder.build();
}