request.getContextURL());
// If the remap has failed then the URL will be null,
// so only modify Location header if the remap
// succeeded.
if (remappedURL != null) {
Header location =
retrieveHeader(LOCATION_HEADER,
responseAccessor);
location.setValue(remappedURL);
}
}
// We have now remapped the URL (either successfully or
// unsuccessfully). However, if we are not to follow
// redirects then ensure there is no URL to follow.
// (If the remap were successful then the Location header
// contains the remapped URL.)
if (request.isFollowRedirects()) {
if (configuration.remapRedirects()) {
urlToFollow = remappedURL;
} else {
urlToFollow = redirectURL;
}
} else {
urlToFollow = null;
}
if (urlToFollow != null) {
// release the existing executor
requestExecutor.release();
if (redirectCycles.contains(urlToFollow)) {
throw new HTTPException(
"Cyclical redirect has been detected.");
} else {
redirectCycles.add(urlToFollow);
}
if (redirectCycles.size() >= MAX_REDIRECTS) {
throw new HTTPException(
"Maximum number of redirects (" +
MAX_REDIRECTS + ") has been exceeded.");
}
}
} else {
// not a redirect so stop this variable from containing the
// request url. Only do so if a redirect was not followed
// to get this response.
if (redirectCycles.size() == 0) {
urlToFollow = null;
}
}
} while (isRedirect(responseAccessor.getStatusCode()) &&
urlToFollow != null);
// populate the resoponse
if (request.getResponse() != null) {
if (urlToFollow != null) {
// We have been redirected but because urlToFollow is
// not null we know that the redirect has been remapped.
// So we need to set the location header to the remapped
// redirect url.
Header location = retrieveHeader(LOCATION_HEADER,
responseAccessor);
if (location != null) {
// update the header so that it points to the remapped
// redirect url
location.setValue(urlToFollow);
}
}
populateWebDriverResponse(request.getResponse(),
responseAccessor,
cookieJar);
}
// initalise the statusCode
statusCode = responseAccessor.getStatusCode();
// stream the output to the responseProcessor
InputStream responseStream = null;
try {
responseStream = responseAccessor.getResponseStream();
// process the response
Header contentTypeHeader = retrieveHeader(CONTENT_TYPE,
responseAccessor);
String contentType = (contentTypeHeader == null) ?
null : contentTypeHeader.getValue();
Header header = retrieveHeader(CONTENT_ENCODING,
responseAccessor);
String contentEncoding = (header != null) ?
header.getValue() : null;
request.getResponseProcessor().processHTTPResponse(
urlToFollow,
responseStream,
statusCode,
contentType,