added URL Proxy & Timeouts

This commit is contained in:
Jeremy Long
2012-09-10 21:27:26 -04:00
parent 559d35b519
commit 391a410e5b
3 changed files with 56 additions and 2 deletions

View File

@@ -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;
}
}
}
}

View File

@@ -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

View File

@@ -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);