if (conman == null) {
conman = new PoolingHttpClientConnectionManager();
conman.setDefaultMaxPerRoute(maxConnections);
conman.setMaxTotal(Math.max(conman.getMaxTotal(), maxConnections));
}
HttpClientBuilder builder = HttpClientBuilder.create()
.disableCookieManagement()
.setConnectionManager(conman);
builder.useSystemProperties();
// TODO: use setDefaultHeaders for adding requestHeaders? It's a bit painful
// because we need to convert it to a Collection of Header objects.
// config code for older version of httpclient.
// builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(socketTimeout).build());
// HttpParams params = client.getParams();
// params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
// params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
// setup request/response intercepter for handling gzip-compressed response.
// Disabled because httpclient 4.3.3 sends "Accept-Encoding: gzip,deflate" by
// default. Response parsing will fail If gzip-decompression ResponseInterceptor
// is installed.
builder.addInterceptorLast(new HttpRequestInterceptor() {
@Override
public void process(final HttpRequest request, final HttpContext context) {
// System.err.println("RequestInterceptor");
// if (request.containsHeader("Accept-Encoding")) {
// System.err.println("already has Accept-Encoding: " + request.getHeaders("Accept-Encoding")[0]);
// }
// if (gzipAccepted) {
// if (!request.containsHeader("Accept-Encoding")) {
// request.addHeader("Accept-Encoding", "gzip");
// }
// }
// add extra headers configured.
if (requestHeaders != null) {
for (Entry<String, String> ent : requestHeaders.entrySet()) {
request.addHeader(ent.getKey(), ent.getValue());
}
}
}
});
// builder.addInterceptorFirst(new HttpResponseInterceptor() {
// @Override
// public void process(final HttpResponse response, final HttpContext context)
// throws HttpException, IOException {
// HttpEntity entity = response.getEntity();
// Header ceheader = entity.getContentEncoding();
// if (ceheader != null && contains(ceheader.getElements(), "gzip")) {
// response.setEntity(new GzipInflatingHttpEntityWrapper(response.getEntity()));
// }
// }
// });
this.client = builder.build();
}
return client;
}