| 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 | 1 | private static final Logger LOGGER = Logger.getLogger(Downloader.class.getName()); |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private Downloader() { |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public static void fetchFile(URL url, File outputPath) throws DownloadFailedException { |
| 59 | 0 | fetchFile(url, outputPath, true); |
| 60 | 0 | } |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public static void fetchFile(URL url, File outputPath, boolean useProxy) throws DownloadFailedException { |
| 71 | 2 | if ("file".equalsIgnoreCase(url.getProtocol())) { |
| 72 | |
File file; |
| 73 | |
try { |
| 74 | 2 | file = new File(url.toURI()); |
| 75 | 0 | } catch (URISyntaxException ex) { |
| 76 | 0 | final String msg = String.format("Download failed, unable to locate '%s'", url.toString()); |
| 77 | 0 | throw new DownloadFailedException(msg); |
| 78 | 2 | } |
| 79 | 2 | if (file.exists()) { |
| 80 | |
try { |
| 81 | 2 | org.apache.commons.io.FileUtils.copyFile(file, outputPath); |
| 82 | 0 | } catch (IOException ex) { |
| 83 | 0 | final String msg = String.format("Download failed, unable to copy '%s'", url.toString()); |
| 84 | 0 | throw new DownloadFailedException(msg); |
| 85 | 2 | } |
| 86 | |
} else { |
| 87 | 0 | final String msg = String.format("Download failed, file does not exist '%s'", url.toString()); |
| 88 | 0 | throw new DownloadFailedException(msg); |
| 89 | |
} |
| 90 | 2 | } else { |
| 91 | 0 | HttpURLConnection conn = null; |
| 92 | |
try { |
| 93 | 0 | conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); |
| 94 | 0 | conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); |
| 95 | 0 | conn.connect(); |
| 96 | 0 | } catch (IOException ex) { |
| 97 | |
try { |
| 98 | 0 | if (conn != null) { |
| 99 | 0 | conn.disconnect(); |
| 100 | |
} |
| 101 | |
} finally { |
| 102 | 0 | conn = null; |
| 103 | 0 | } |
| 104 | 0 | throw new DownloadFailedException("Error downloading file.", ex); |
| 105 | 0 | } |
| 106 | 0 | final String encoding = conn.getContentEncoding(); |
| 107 | |
|
| 108 | 0 | BufferedOutputStream writer = null; |
| 109 | 0 | InputStream reader = null; |
| 110 | |
try { |
| 111 | 0 | if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { |
| 112 | 0 | reader = new GZIPInputStream(conn.getInputStream()); |
| 113 | 0 | } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) { |
| 114 | 0 | reader = new InflaterInputStream(conn.getInputStream()); |
| 115 | |
} else { |
| 116 | 0 | reader = conn.getInputStream(); |
| 117 | |
} |
| 118 | |
|
| 119 | 0 | writer = new BufferedOutputStream(new FileOutputStream(outputPath)); |
| 120 | 0 | final byte[] buffer = new byte[4096]; |
| 121 | |
int bytesRead; |
| 122 | 0 | while ((bytesRead = reader.read(buffer)) > 0) { |
| 123 | 0 | writer.write(buffer, 0, bytesRead); |
| 124 | |
} |
| 125 | 0 | } catch (Throwable ex) { |
| 126 | 0 | throw new DownloadFailedException("Error saving downloaded file.", ex); |
| 127 | |
} finally { |
| 128 | 0 | if (writer != null) { |
| 129 | |
try { |
| 130 | 0 | writer.close(); |
| 131 | 0 | } catch (Throwable ex) { |
| 132 | 0 | LOGGER.log(Level.FINEST, |
| 133 | |
"Error closing the writer in Downloader.", ex); |
| 134 | 0 | } |
| 135 | |
} |
| 136 | 0 | if (reader != null) { |
| 137 | |
try { |
| 138 | 0 | reader.close(); |
| 139 | 0 | } catch (Throwable ex) { |
| 140 | 0 | LOGGER.log(Level.FINEST, |
| 141 | |
"Error closing the reader in Downloader.", ex); |
| 142 | 0 | } |
| 143 | |
} |
| 144 | |
try { |
| 145 | 0 | conn.disconnect(); |
| 146 | |
} finally { |
| 147 | 0 | conn = null; |
| 148 | 0 | } |
| 149 | 0 | } |
| 150 | |
} |
| 151 | 2 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public static long getLastModified(URL url) throws DownloadFailedException { |
| 162 | 10 | long timestamp = 0; |
| 163 | |
|
| 164 | 10 | if ("file".equalsIgnoreCase(url.getProtocol())) { |
| 165 | |
File lastModifiedFile; |
| 166 | |
try { |
| 167 | 10 | lastModifiedFile = new File(url.toURI()); |
| 168 | 0 | } catch (URISyntaxException ex) { |
| 169 | 0 | final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString()); |
| 170 | 0 | throw new DownloadFailedException(msg); |
| 171 | 10 | } |
| 172 | 10 | timestamp = lastModifiedFile.lastModified(); |
| 173 | 10 | } else { |
| 174 | 0 | HttpURLConnection conn = null; |
| 175 | |
try { |
| 176 | 0 | conn = URLConnectionFactory.createHttpURLConnection(url); |
| 177 | 0 | conn.setRequestMethod("HEAD"); |
| 178 | 0 | conn.connect(); |
| 179 | 0 | timestamp = conn.getLastModified(); |
| 180 | 0 | } catch (URLConnectionFailureException ex) { |
| 181 | 0 | throw new DownloadFailedException("Error creating URL Connection for HTTP HEAD request.", ex); |
| 182 | 0 | } catch (IOException ex) { |
| 183 | 0 | throw new DownloadFailedException("Error making HTTP HEAD request.", ex); |
| 184 | |
} finally { |
| 185 | 0 | if (conn != null) { |
| 186 | |
try { |
| 187 | 0 | conn.disconnect(); |
| 188 | |
} finally { |
| 189 | 0 | conn = null; |
| 190 | 0 | } |
| 191 | |
} |
| 192 | |
} |
| 193 | |
} |
| 194 | 10 | return timestamp; |
| 195 | |
} |
| 196 | |
} |