mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-21 08:39:24 +01:00
added the capability to follow more then a single redirect when downloading a file
Former-commit-id: cc3382fec2c168119474ee6bae7b5f250163c2da
This commit is contained in:
@@ -42,6 +42,10 @@ public final class Downloader {
|
|||||||
* The logger.
|
* The logger.
|
||||||
*/
|
*/
|
||||||
private static final Logger LOGGER = Logger.getLogger(Downloader.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(Downloader.class.getName());
|
||||||
|
/**
|
||||||
|
* The maximum number of redirects that will be followed when attempting to download a file.
|
||||||
|
*/
|
||||||
|
private static final int MAX_REDIRECT_ATTEMPTS = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor for utility class.
|
* Private constructor for utility class.
|
||||||
@@ -91,26 +95,27 @@ public final class Downloader {
|
|||||||
} else {
|
} else {
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
try {
|
try {
|
||||||
|
LOGGER.fine(String.format("Attempting download of %s", url.toString()));
|
||||||
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();
|
int status = conn.getResponseCode();
|
||||||
if (status != HttpURLConnection.HTTP_OK) {
|
int redirectCount = 0;
|
||||||
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|
while ((status == HttpURLConnection.HTTP_MOVED_TEMP
|
||||||
|| status == HttpURLConnection.HTTP_MOVED_PERM
|
|| status == HttpURLConnection.HTTP_MOVED_PERM
|
||||||
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
|
|| status == HttpURLConnection.HTTP_SEE_OTHER)
|
||||||
final String location = conn.getHeaderField("Location");
|
&& MAX_REDIRECT_ATTEMPTS > redirectCount++) {
|
||||||
try {
|
final String location = conn.getHeaderField("Location");
|
||||||
conn.disconnect();
|
try {
|
||||||
} finally {
|
conn.disconnect();
|
||||||
conn = null;
|
} 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();
|
|
||||||
}
|
}
|
||||||
|
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) {
|
if (status != 200) {
|
||||||
try {
|
try {
|
||||||
@@ -152,6 +157,7 @@ public final class Downloader {
|
|||||||
while ((bytesRead = reader.read(buffer)) > 0) {
|
while ((bytesRead = reader.read(buffer)) > 0) {
|
||||||
writer.write(buffer, 0, bytesRead);
|
writer.write(buffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
|
LOGGER.fine(String.format("Download of %s complete", url.toString()));
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
analyzeException(ex);
|
analyzeException(ex);
|
||||||
final String msg = String.format("Error saving '%s' to file '%s'%nConnection Timeout: %d%nEncoding: %s%n",
|
final String msg = String.format("Error saving '%s' to file '%s'%nConnection Timeout: %d%nEncoding: %s%n",
|
||||||
|
|||||||
Reference in New Issue
Block a user