String urlString = url.toString();
// presuming that not following redirects means no
// preemptive authN
HttpClient client = getHttpClient(followRedirects, followRedirects);
HttpGet getMethod = new HttpGet(urlString);
HttpInputStream in = new HttpInputStream(client, getMethod);
int status = in.getStatusCode();
logger.debug("GET {} : {}", urlString, status);
if (failIfNotOK) {
if (status != 200) {
if (followRedirects && 300 <= status && status <= 399) {
// Handle the redirect here !
logger.debug(
"FedoraClient is handling redirect for HTTP STATUS={}",
status);
Header hLoc = in.getResponseHeader(HttpHeaders.LOCATION);
if (hLoc != null) {
String location = hLoc.getValue();
logger.debug("FedoraClient is trying redirect location: {}",
location);
// Try the redirect location, but don't try to handle another level of redirection.
in.close();
return get(location, true, false);
} else {
try {
throw new IOException("Request failed [" + status
+ " " + in.getStatusText() + "]");
} finally {
try {
in.close();
} catch (Exception e) {
logger.error("Can't close InputStream: "
+ e.getMessage());
}
}
}
} else {
try {
throw new IOException("Request failed ["
+ in.getStatusCode() + " " + in.getStatusText()
+ "] : " + urlString);
} finally {
try {
in.close();
} catch (Exception e) {
logger.error("Can't close InputStream: "
+ e.getMessage());
}
}