diff --git a/src/main/java/org/codesecure/dependencycheck/utils/Downloader.java b/src/main/java/org/codesecure/dependencycheck/utils/Downloader.java index d05b82454..d27571e45 100644 --- a/src/main/java/org/codesecure/dependencycheck/utils/Downloader.java +++ b/src/main/java/org/codesecure/dependencycheck/utils/Downloader.java @@ -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; + } } } } diff --git a/src/main/java/org/codesecure/dependencycheck/utils/Settings.java b/src/main/java/org/codesecure/dependencycheck/utils/Settings.java index a53ddba99..15e6e90df 100644 --- a/src/main/java/org/codesecure/dependencycheck/utils/Settings.java +++ b/src/main/java/org/codesecure/dependencycheck/utils/Settings.java @@ -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 diff --git a/src/test/java/org/codesecure/dependencycheck/utils/DownloaderTest.java b/src/test/java/org/codesecure/dependencycheck/utils/DownloaderTest.java index 4ed3910f8..4a067a8ef 100644 --- a/src/test/java/org/codesecure/dependencycheck/utils/DownloaderTest.java +++ b/src/test/java/org/codesecure/dependencycheck/utils/DownloaderTest.java @@ -45,6 +45,11 @@ public class DownloaderTest { // @Test // public void testFetchFile_URL_String() throws Exception { // System.out.println("fetchFile"); +// +//// Settings.setString(Settings.KEYS.PROXY_URL, "test"); +//// Settings.setString(Settings.KEYS.PROXY_PORT, "80"); +//// Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, "1000"); +// // URL url = new URL(Settings.getString(Settings.KEYS.CPE_URL)); // String outputPath = "target\\downloaded_cpe.xml"; // Downloader.fetchFile(url, outputPath);