useDynamicProxy ? proxyHost : PROXY_HOST,
useDynamicProxy ? proxyPort : PROXY_PORT,
useDynamicProxy ? getProxyUser() : PROXY_USER,
useDynamicProxy ? getProxyPass() : PROXY_PASS);
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);
}