HttpClient httpClient = map.get(key);
if (httpClient == null){ // One-time init for this client
HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);
httpClient = new DefaultHttpClient(clientParams){
@Override
protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
return new DefaultHttpRequestRetryHandler(RETRY_COUNT, false) {
// TODO HACK to fix https://issues.apache.org/jira/browse/HTTPCLIENT-1120
// can hopefully be removed when 4.1.3 or 4.2 are released
@Override
public boolean retryRequest(IOException ex, int count, HttpContext ctx) {
Object request = ctx.getAttribute(ExecutionContext.HTTP_REQUEST);
if(request instanceof HttpUriRequest){
if (request instanceof RequestWrapper) {
request = ((RequestWrapper) request).getOriginal();
}
if(((HttpUriRequest)request).isAborted()){
log.warn("Workround for HTTPCLIENT-1120 request retry: "+ex);
return false;
}
}
/*
* When connect fails due to abort, the request is not in the context.
* Tried adding the request - with a new key - to the local context in the sample() method,
* but the request was not flagged as aborted, so that did not help.
* So we check for any specific exception that is triggered.
*/
if (
(ex instanceof java.net.BindException &&
ex.getMessage().contains("Address already in use: connect"))
||
ex.getMessage().contains("Request aborted") // plain IOException
) {
/*
* The above messages may be generated by aborted connects.
* If either occurs in other situations, retrying is unlikely to help,
* so preventing retry should not cause a problem.
*/
log.warn("Workround for HTTPCLIENT-1120 connect retry: "+ex);
return false;
}
return super.retryRequest(ex, count, ctx);
} // end of hack
}; // set retry count
}
};
((AbstractHttpClient) httpClient).addResponseInterceptor(new ResponseContentEncoding());
((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER); // HACK
// Override the defualt schemes as necessary
SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
if (SLOW_HTTP != null){
schemeRegistry.register(SLOW_HTTP);
}
if (HTTPS_SCHEME != null){
schemeRegistry.register(HTTPS_SCHEME);
}
// Set up proxy details
if (useDynamicProxy){
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
String proxyUser = getProxyUser();
if (proxyUser.length() > 0) {
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(proxyUser, getProxyPass()));
}
} else if (useStaticProxy) {
HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (PROXY_USER.length() > 0)
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(
new AuthScope(PROXY_HOST, PROXY_PORT),
new UsernamePasswordCredentials(PROXY_USER, PROXY_PASS));
}