HttpResponse response = null;
boolean done = false;
try {
while (!done) {
HttpRoute route = roureq.getRoute();
// Allocate connection if needed
if (managedConn == null) {
managedConn = allocateConnection(route, timeout);
}
// Reopen connection if needed
if (!managedConn.isOpen()) {
managedConn.open(route, context, params);
}
try {
establishRoute(route, context);
} catch (TunnelRefusedException ex) {
if (LOG.isDebugEnabled()) {
LOG.debug(ex.getMessage());
}
response = ex.getResponse();
break;
}
if (HttpConnectionParams.isStaleCheckingEnabled(params)) {
// validate connection
LOG.debug("Stale connection check");
if (managedConn.isStale()) {
LOG.debug("Stale connection detected");
managedConn.close();
continue;
}
}
// Wrap the original request
RequestWrapper request = wrapRequest(roureq.getRequest());
request.setParams(this.params);
// Re-write request URI if needed
rewriteRequestURI(request, route);
// Use virtual host if set
HttpHost target = (HttpHost) request.getParams().getParameter(
ClientPNames.VIRTUAL_HOST);
if (target == null) {
target = route.getTargetHost();
}
HttpHost proxy = route.getProxyHost();
// Populate the execution context
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST,
target);
context.setAttribute(ExecutionContext.HTTP_PROXY_HOST,