minor updates for issue #58

Former-commit-id: 6f4d0edc03654c73dc6de29a47d65e6297814613
This commit is contained in:
Jeremy Long
2014-03-09 12:40:42 -04:00
parent 05a1096e25
commit a627ca2127
2 changed files with 76 additions and 63 deletions

View File

@@ -109,10 +109,11 @@ public abstract class AbstractSuppressionAnalyzer extends AbstractAnalyzer {
File file = null; File file = null;
boolean deleteTempFile = false; boolean deleteTempFile = false;
try { try {
Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE); final Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE);
if (uriRx.matcher(suppressionFilePath).matches()) { if (uriRx.matcher(suppressionFilePath).matches()) {
deleteTempFile = true;
file = File.createTempFile("suppression", "xml", Settings.getTempDirectory()); file = File.createTempFile("suppression", "xml", Settings.getTempDirectory());
URL url = new URL(suppressionFilePath); final URL url = new URL(suppressionFilePath);
try { try {
Downloader.fetchFile(url, file, false); Downloader.fetchFile(url, file, false);
} catch (DownloadFailedException ex) { } catch (DownloadFailedException ex) {

View File

@@ -63,6 +63,26 @@ public final class Downloader {
* @throws DownloadFailedException is thrown if there is an error downloading the file * @throws DownloadFailedException is thrown if there is an error downloading the file
*/ */
public static void fetchFile(URL url, File outputPath, boolean useProxy) throws DownloadFailedException { public static void fetchFile(URL url, File outputPath, boolean useProxy) throws DownloadFailedException {
if ("file".equalsIgnoreCase(url.getProtocol())) {
File file;
try {
file = new File(url.toURI());
} catch (URISyntaxException ex) {
final String msg = String.format("Download failed, unable to locate '%s'", url.toString());
throw new DownloadFailedException(msg);
}
if (file.exists()) {
try {
org.apache.commons.io.FileUtils.copyFile(file, outputPath);
} catch (IOException ex) {
final String msg = String.format("Download failed, unable to copy '%s'", url.toString());
throw new DownloadFailedException(msg);
}
} else {
final String msg = String.format("Download failed, file does not exist '%s'", url.toString());
throw new DownloadFailedException(msg);
}
} else {
HttpURLConnection conn = null; HttpURLConnection conn = null;
try { try {
conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
@@ -123,6 +143,7 @@ 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
@@ -134,20 +155,11 @@ public final class Downloader {
*/ */
public static long getLastModified(URL url) throws DownloadFailedException { public static long getLastModified(URL url) throws DownloadFailedException {
long timestamp = 0; long timestamp = 0;
//TODO add the FPR protocol? //TODO add the FTP protocol?
if ("file".equalsIgnoreCase(url.getProtocol())) { if ("file".equalsIgnoreCase(url.getProtocol())) {
File lastModifiedFile; File lastModifiedFile;
try { try {
// if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
// String filePath = url.toString();
// if (filePath.matches("file://[a-zA-Z]:.*")) {
// f = new File(filePath.substring(7));
// } else {
// f = new File(url.toURI());
// }
// } else {
lastModifiedFile = new File(url.toURI()); lastModifiedFile = new File(url.toURI());
// }
} catch (URISyntaxException ex) { } catch (URISyntaxException ex) {
final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString()); final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString());
throw new DownloadFailedException(msg); throw new DownloadFailedException(msg);