mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-01-15 16:23:37 +01:00
added URL Proxy & Timeouts
This commit is contained in:
@@ -23,6 +23,10 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -59,14 +63,42 @@ public class Downloader {
|
||||
* @throws DownloadFailedException is thrown if there is an error downloading the file.
|
||||
*/
|
||||
public static void fetchFile(URL url, File outputPath) throws DownloadFailedException {
|
||||
HttpURLConnection conn = null;
|
||||
Proxy proxy = null;
|
||||
String proxyUrl = Settings.getString(Settings.KEYS.PROXY_URL);
|
||||
|
||||
try {
|
||||
url.openConnection();
|
||||
if (proxyUrl != null) {
|
||||
int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
|
||||
SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
|
||||
proxy = new Proxy(Proxy.Type.HTTP, addr);
|
||||
conn = (HttpURLConnection) url.openConnection(proxy);
|
||||
} else {
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
}
|
||||
if (Settings.getString(Settings.KEYS.CONNECTION_TIMEOUT) != null) {
|
||||
int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT);
|
||||
conn.setConnectTimeout(timeout);
|
||||
}
|
||||
|
||||
conn.connect();
|
||||
} catch (IOException ex) {
|
||||
try {
|
||||
if (conn!=null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
} finally {
|
||||
conn = null;
|
||||
}
|
||||
throw new DownloadFailedException("Error downloading file.", ex);
|
||||
}
|
||||
|
||||
BufferedOutputStream writer = null;
|
||||
try {
|
||||
InputStream reader = url.openStream();
|
||||
//the following times out on some systems because the CPE is big.
|
||||
//InputStream reader = url.openStream();
|
||||
InputStream reader = conn.getInputStream();
|
||||
|
||||
writer = new BufferedOutputStream(new FileOutputStream(outputPath));
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead = 0;
|
||||
@@ -83,6 +115,11 @@ public class Downloader {
|
||||
Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
|
||||
"Error closing the writter in Downloader.", ex);
|
||||
}
|
||||
try {
|
||||
conn.disconnect();
|
||||
} finally {
|
||||
conn = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,9 @@ public class Settings {
|
||||
* The properties key prefix for the analyzer assocations.
|
||||
*/
|
||||
public static final String FILE_EXTENSION_ANALYZER_ASSOCIATION_PREFIX = "file.extension.analyzer.association.";
|
||||
public static final String PROXY_URL = "proxy.url";
|
||||
public static final String PROXY_PORT = "proxy.port";
|
||||
public static final String CONNECTION_TIMEOUT = "connection.timeout";
|
||||
}
|
||||
private static final String PROPERTIES_FILE = "dependencycheck.properties";
|
||||
private static Settings instance = new Settings();
|
||||
@@ -100,6 +103,15 @@ public class Settings {
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a property value.
|
||||
* @param key the key for the property.
|
||||
* @param value the value for the property.
|
||||
*/
|
||||
public static void setString(String key, String value) {
|
||||
instance.props.setProperty(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value from the properties file. If the value was specified as a
|
||||
* system property or passed in via the -Dprop=value argument - this method
|
||||
|
||||
Reference in New Issue
Block a user