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());
// Unset auth scope
this.targetAuthState.setAuthScope(null);
this.proxyAuthState.setAuthScope(null);
// Invalidate auth states if redirecting to another host
if (!route.getTargetHost().equals(newTarget)) {
this.targetAuthState.invalidate();
AuthScheme authScheme = this.proxyAuthState.getAuthScheme();
if (authScheme != null && authScheme.isConnectionBased()) {
this.proxyAuthState.invalidate();
}
}
RequestWrapper newRequest = wrapRequest(redirect);
newRequest.setParams(this.params);
HttpRoute newRoute = determineRoute(newTarget, newRequest, this.localContext);
if (this.log.isDebugEnabled()) {
this.log.debug("Redirecting to '" + uri + "' via " + newRoute);