mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-20 16:24:11 +01:00
minor updates for issue #58
Former-commit-id: 6f4d0edc03654c73dc6de29a47d65e6297814613
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
@@ -63,63 +63,84 @@ 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 {
|
||||||
HttpURLConnection conn = null;
|
if ("file".equalsIgnoreCase(url.getProtocol())) {
|
||||||
try {
|
File file;
|
||||||
conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
|
|
||||||
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
|
||||||
conn.connect();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
try {
|
try {
|
||||||
if (conn != null) {
|
file = new File(url.toURI());
|
||||||
conn.disconnect();
|
} catch (URISyntaxException ex) {
|
||||||
}
|
final String msg = String.format("Download failed, unable to locate '%s'", url.toString());
|
||||||
} finally {
|
throw new DownloadFailedException(msg);
|
||||||
conn = null;
|
|
||||||
}
|
}
|
||||||
throw new DownloadFailedException("Error downloading file.", ex);
|
if (file.exists()) {
|
||||||
}
|
try {
|
||||||
final String encoding = conn.getContentEncoding();
|
org.apache.commons.io.FileUtils.copyFile(file, outputPath);
|
||||||
|
} catch (IOException ex) {
|
||||||
BufferedOutputStream writer = null;
|
final String msg = String.format("Download failed, unable to copy '%s'", url.toString());
|
||||||
InputStream reader = null;
|
throw new DownloadFailedException(msg);
|
||||||
try {
|
}
|
||||||
if (encoding != null && "gzip".equalsIgnoreCase(encoding)) {
|
|
||||||
reader = new GZIPInputStream(conn.getInputStream());
|
|
||||||
} else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) {
|
|
||||||
reader = new InflaterInputStream(conn.getInputStream());
|
|
||||||
} else {
|
} else {
|
||||||
reader = conn.getInputStream();
|
final String msg = String.format("Download failed, file does not exist '%s'", url.toString());
|
||||||
}
|
throw new DownloadFailedException(msg);
|
||||||
|
|
||||||
writer = new BufferedOutputStream(new FileOutputStream(outputPath));
|
|
||||||
final byte[] buffer = new byte[4096];
|
|
||||||
int bytesRead;
|
|
||||||
while ((bytesRead = reader.read(buffer)) > 0) {
|
|
||||||
writer.write(buffer, 0, bytesRead);
|
|
||||||
}
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
throw new DownloadFailedException("Error saving downloaded file.", ex);
|
|
||||||
} finally {
|
|
||||||
if (writer != null) {
|
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
|
|
||||||
"Error closing the writer in Downloader.", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (reader != null) {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
|
|
||||||
"Error closing the reader in Downloader.", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
HttpURLConnection conn = null;
|
||||||
try {
|
try {
|
||||||
conn.disconnect();
|
conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
|
||||||
|
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
|
||||||
|
conn.connect();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
try {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
conn = null;
|
||||||
|
}
|
||||||
|
throw new DownloadFailedException("Error downloading file.", ex);
|
||||||
|
}
|
||||||
|
final String encoding = conn.getContentEncoding();
|
||||||
|
|
||||||
|
BufferedOutputStream writer = null;
|
||||||
|
InputStream reader = null;
|
||||||
|
try {
|
||||||
|
if (encoding != null && "gzip".equalsIgnoreCase(encoding)) {
|
||||||
|
reader = new GZIPInputStream(conn.getInputStream());
|
||||||
|
} else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) {
|
||||||
|
reader = new InflaterInputStream(conn.getInputStream());
|
||||||
|
} else {
|
||||||
|
reader = conn.getInputStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer = new BufferedOutputStream(new FileOutputStream(outputPath));
|
||||||
|
final byte[] buffer = new byte[4096];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = reader.read(buffer)) > 0) {
|
||||||
|
writer.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
throw new DownloadFailedException("Error saving downloaded file.", ex);
|
||||||
} finally {
|
} finally {
|
||||||
conn = null;
|
if (writer != null) {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
|
||||||
|
"Error closing the writer in Downloader.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reader != null) {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
|
||||||
|
"Error closing the reader in Downloader.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn.disconnect();
|
||||||
|
} finally {
|
||||||
|
conn = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user