case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
if(!methid.getFollowRedirects()) {
if(LOG.isInfoEnabled())
LOG.warn("Redirect requested but not supported");
throw new HttpException("Redirect requested");
}
Header locationHeader = methid.getResponseHeader("location");
if(locationHeader == null) {
if(LOG.isInfoEnabled())
LOG.warn("Redirect requested, no location header");
throw new HttpException("Redirected without a location");
}
String location = locationHeader.getValue();
if(LOG.isInfoEnabled())
LOG.info("Redirected requested to: " + location);
URI newLocation = new URI(location.toCharArray());
// Retrieve the RequestHeaders
Header[] requestHeaders = methid.getRequestHeaders();
// Recycle this method so we can use it again.
methid.recycle();
HostConfiguration hc = methid.getHostConfiguration();
hc.setHost(
newLocation.getHost(),
newLocation.getPort(),
newLocation.getScheme()
);
methid.setFollowRedirects(true);
for(int j = 0; j < requestHeaders.length; j++) {
if(!requestHeaders[j].getName().equals("Host"))
methid.addRequestHeader(requestHeaders[j]);
}
// Set up the new values for the method.
methid.setPath(newLocation.getEscapedPath());
methid.setQueryString(newLocation.getEscapedQuery());
methid.removeRequestHeader(HttpAuthenticator.WWW_AUTH_RESP);
// Loop around and try the method again.
break;
default:
return;
}
}
throw new HttpException("Maximum redirects encountered, bailing");
}