added code so that the Downloader now follows 1 level of redirection to download the file (part of patch for issue #196)

Former-commit-id: ecd914dbcacad1e12a243fdff90f043ef114c160
This commit is contained in:
Jeremy Long
2015-02-18 20:10:44 -05:00
parent a28c2819fa
commit 085ab48f3f

View File

@@ -94,6 +94,34 @@ public final class Downloader {
conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.connect(); conn.connect();
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
String location = conn.getHeaderField("Location");
try {
conn.disconnect();
} finally {
conn = null;
}
LOGGER.fine(String.format("Download is being redirected from %s to %s", url.toString(), location));
conn = URLConnectionFactory.createHttpURLConnection(new URL(location), useProxy);
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.connect();
status = conn.getResponseCode();
}
}
if (status != 200) {
try {
conn.disconnect();
} finally {
conn = null;
}
final String msg = String.format("Error downloading file %s; received response code %s.", url.toString(), status);
throw new DownloadFailedException(msg);
}
} catch (IOException ex) { } catch (IOException ex) {
try { try {
if (conn != null) { if (conn != null) {
@@ -105,8 +133,8 @@ public final class Downloader {
final String msg = String.format("Error downloading file %s; unable to connect.", url.toString()); final String msg = String.format("Error downloading file %s; unable to connect.", url.toString());
throw new DownloadFailedException(msg, ex); throw new DownloadFailedException(msg, ex);
} }
final String encoding = conn.getContentEncoding();
final String encoding = conn.getContentEncoding();
BufferedOutputStream writer = null; BufferedOutputStream writer = null;
InputStream reader = null; InputStream reader = null;
try { try {
@@ -138,16 +166,14 @@ public final class Downloader {
try { try {
writer.close(); writer.close();
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.log(Level.FINEST, LOGGER.log(Level.FINEST, "Error closing the writer in Downloader.", ex);
"Error closing the writer in Downloader.", ex);
} }
} }
if (reader != null) { if (reader != null) {
try { try {
reader.close(); reader.close();
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.log(Level.FINEST, LOGGER.log(Level.FINEST, "Error closing the reader in Downloader.", ex);
"Error closing the reader in Downloader.", ex);
} }
} }
try { try {
@@ -160,8 +186,8 @@ public final class Downloader {
} }
/** /**
* Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// protocol is * Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// protocol is specified, then
* specified, then the lastTimestamp of the file is returned. * the lastTimestamp of the file is returned.
* *
* @param url the URL to retrieve the timestamp from * @param url the URL to retrieve the timestamp from
* @return an epoch timestamp * @return an epoch timestamp