| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.utils; |
| 19 | |
|
| 20 | |
import java.io.BufferedOutputStream; |
| 21 | |
import java.io.File; |
| 22 | |
import java.io.FileOutputStream; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.io.InputStream; |
| 25 | |
import java.net.HttpURLConnection; |
| 26 | |
import java.net.URISyntaxException; |
| 27 | |
import java.net.URL; |
| 28 | |
import java.util.logging.Level; |
| 29 | |
import java.util.logging.Logger; |
| 30 | |
import java.util.zip.GZIPInputStream; |
| 31 | |
import java.util.zip.InflaterInputStream; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class Downloader { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
private Downloader() { |
| 44 | |
} |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public static void fetchFile(URL url, File outputPath) throws DownloadFailedException { |
| 54 | 0 | HttpURLConnection conn = null; |
| 55 | |
try { |
| 56 | 0 | conn = URLConnectionFactory.createHttpURLConnection(url); |
| 57 | 0 | conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); |
| 58 | 0 | conn.connect(); |
| 59 | 0 | } catch (IOException ex) { |
| 60 | |
try { |
| 61 | 0 | if (conn != null) { |
| 62 | 0 | conn.disconnect(); |
| 63 | |
} |
| 64 | |
} finally { |
| 65 | 0 | conn = null; |
| 66 | 0 | } |
| 67 | 0 | throw new DownloadFailedException("Error downloading file.", ex); |
| 68 | 0 | } |
| 69 | 0 | final String encoding = conn.getContentEncoding(); |
| 70 | |
|
| 71 | 0 | BufferedOutputStream writer = null; |
| 72 | 0 | InputStream reader = null; |
| 73 | |
try { |
| 74 | 0 | if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { |
| 75 | 0 | reader = new GZIPInputStream(conn.getInputStream()); |
| 76 | 0 | } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) { |
| 77 | 0 | reader = new InflaterInputStream(conn.getInputStream()); |
| 78 | |
} else { |
| 79 | 0 | reader = conn.getInputStream(); |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | writer = new BufferedOutputStream(new FileOutputStream(outputPath)); |
| 83 | 0 | final byte[] buffer = new byte[4096]; |
| 84 | |
int bytesRead; |
| 85 | 0 | while ((bytesRead = reader.read(buffer)) > 0) { |
| 86 | 0 | writer.write(buffer, 0, bytesRead); |
| 87 | |
} |
| 88 | 0 | } catch (Throwable ex) { |
| 89 | 0 | throw new DownloadFailedException("Error saving downloaded file.", ex); |
| 90 | |
} finally { |
| 91 | 0 | if (writer != null) { |
| 92 | |
try { |
| 93 | 0 | writer.close(); |
| 94 | 0 | } catch (Throwable ex) { |
| 95 | 0 | Logger.getLogger(Downloader.class.getName()).log(Level.FINEST, |
| 96 | |
"Error closing the writer in Downloader.", ex); |
| 97 | 0 | } |
| 98 | |
} |
| 99 | 0 | if (reader != null) { |
| 100 | |
try { |
| 101 | 0 | reader.close(); |
| 102 | 0 | } catch (Throwable ex) { |
| 103 | 0 | Logger.getLogger(Downloader.class.getName()).log(Level.FINEST, |
| 104 | |
"Error closing the reader in Downloader.", ex); |
| 105 | 0 | } |
| 106 | |
} |
| 107 | |
try { |
| 108 | 0 | conn.disconnect(); |
| 109 | |
} finally { |
| 110 | 0 | conn = null; |
| 111 | 0 | } |
| 112 | 0 | } |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public static long getLastModified(URL url) throws DownloadFailedException { |
| 124 | 10 | long timestamp = 0; |
| 125 | |
|
| 126 | 10 | if ("file".equalsIgnoreCase(url.getProtocol())) { |
| 127 | |
File lastModifiedFile; |
| 128 | |
try { |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 10 | lastModifiedFile = new File(url.toURI()); |
| 138 | |
|
| 139 | 0 | } catch (URISyntaxException ex) { |
| 140 | 0 | final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString()); |
| 141 | 0 | throw new DownloadFailedException(msg); |
| 142 | 10 | } |
| 143 | 10 | timestamp = lastModifiedFile.lastModified(); |
| 144 | 10 | } else { |
| 145 | 0 | HttpURLConnection conn = null; |
| 146 | |
try { |
| 147 | 0 | conn = URLConnectionFactory.createHttpURLConnection(url); |
| 148 | 0 | conn.setRequestMethod("HEAD"); |
| 149 | 0 | conn.connect(); |
| 150 | 0 | timestamp = conn.getLastModified(); |
| 151 | 0 | } catch (URLConnectionFailureException ex) { |
| 152 | 0 | throw new DownloadFailedException("Error creating URL Connection for HTTP HEAD request.", ex); |
| 153 | 0 | } catch (IOException ex) { |
| 154 | 0 | throw new DownloadFailedException("Error making HTTP HEAD request.", ex); |
| 155 | |
} finally { |
| 156 | 0 | if (conn != null) { |
| 157 | |
try { |
| 158 | 0 | conn.disconnect(); |
| 159 | |
} finally { |
| 160 | 0 | conn = null; |
| 161 | 0 | } |
| 162 | |
} |
| 163 | |
} |
| 164 | |
} |
| 165 | 10 | return timestamp; |
| 166 | |
} |
| 167 | |
} |