private RoutedRequest handleRedirect() throws HttpException {
if (this.redirectStrategy.isRedirected(
this.currentRequest, this.currentResponse, this.localContext)) {
HttpRoute route = this.mainRequest.getRoute();
RequestWrapper request = this.mainRequest.getRequest();
int maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
if (this.redirectCount >= maxRedirects) {
throw new RedirectException("Maximum redirects ("
+ maxRedirects + ") exceeded");
}
this.redirectCount++;
HttpUriRequest redirect = this.redirectStrategy.getRedirect(
this.currentRequest, this.currentResponse, this.localContext);
HttpRequest orig = request.getOriginal();
redirect.setHeaders(orig.getAllHeaders());
URI uri = redirect.getURI();
if (uri.getHost() == null) {
throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
}
HttpHost newTarget = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
// Reset auth states if redirecting to another host
if (!route.getTargetHost().equals(newTarget)) {
if (this.log.isDebugEnabled()) {
this.log.debug("[exchange: " + this.id + "] Resetting target auth state");
}
this.targetAuthState.reset();
AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
if (authScheme != null && authScheme.isConnectionBased()) {
if (this.log.isDebugEnabled()) {
this.log.debug("[exchange: " + this.id + "] Resetting proxy auth state");
}
this.proxyAuthState.reset();
}
}
RequestWrapper newRequest = wrapRequest(redirect);
newRequest.setParams(this.params);
HttpRoute newRoute = determineRoute(newTarget, newRequest, this.localContext);
if (this.log.isDebugEnabled()) {
this.log.debug("[exchange: " + this.id + "] Redirecting to '" + uri + "' via " + newRoute);