Coverage Report - org.owasp.dependencycheck.utils.Downloader
 
Classes in this File Line Coverage Branch Coverage Complexity
Downloader
8%
7/83
4%
1/24
7
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.utils;
 20  
 
 21  
 import java.io.BufferedOutputStream;
 22  
 import java.io.File;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.net.HttpURLConnection;
 27  
 import java.net.InetSocketAddress;
 28  
 import java.net.Proxy;
 29  
 import java.net.SocketAddress;
 30  
 import java.net.URISyntaxException;
 31  
 import java.net.URL;
 32  
 import java.util.logging.Level;
 33  
 import java.util.logging.Logger;
 34  
 import java.util.zip.GZIPInputStream;
 35  
 import java.util.zip.InflaterInputStream;
 36  
 
 37  
 /**
 38  
  * A utility to download files from the Internet.
 39  
  *
 40  
  * @author Jeremy Long (jeremy.long@owasp.org)
 41  
  */
 42  
 public final class Downloader {
 43  
 
 44  
     /**
 45  
      * Private constructor for utility class.
 46  
      */
 47  0
     private Downloader() {
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Retrieves a file from a given URL and saves it to the outputPath.
 52  
      *
 53  
      * @param url the URL of the file to download.
 54  
      * @param outputPath the path to the save the file to.
 55  
      * @throws DownloadFailedException is thrown if there is an error
 56  
      * downloading the file.
 57  
      */
 58  
     public static void fetchFile(URL url, File outputPath) throws DownloadFailedException {
 59  0
         HttpURLConnection conn = null;
 60  
         try {
 61  0
             conn = Downloader.getConnection(url);
 62  0
             conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
 63  0
             conn.connect();
 64  0
         } catch (IOException ex) {
 65  
             try {
 66  0
                 if (conn != null) {
 67  0
                     conn.disconnect();
 68  
                 }
 69  
             } finally {
 70  0
                 conn = null;
 71  0
             }
 72  0
             throw new DownloadFailedException("Error downloading file.", ex);
 73  0
         }
 74  0
         final String encoding = conn.getContentEncoding();
 75  
 
 76  0
         BufferedOutputStream writer = null;
 77  0
         InputStream reader = null;
 78  
         try {
 79  0
             if (encoding != null && "gzip".equalsIgnoreCase(encoding)) {
 80  0
                 reader = new GZIPInputStream(conn.getInputStream());
 81  0
             } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) {
 82  0
                 reader = new InflaterInputStream(conn.getInputStream());
 83  
             } else {
 84  0
                 reader = conn.getInputStream();
 85  
             }
 86  
 
 87  0
             writer = new BufferedOutputStream(new FileOutputStream(outputPath));
 88  0
             final byte[] buffer = new byte[4096];
 89  
             int bytesRead;
 90  0
             while ((bytesRead = reader.read(buffer)) > 0) {
 91  0
                 writer.write(buffer, 0, bytesRead);
 92  
             }
 93  0
         } catch (Exception ex) {
 94  0
             throw new DownloadFailedException("Error saving downloaded file.", ex);
 95  
         } finally {
 96  0
             if (writer != null) {
 97  
                 try {
 98  0
                     writer.close();
 99  0
                 } catch (Exception ex) {
 100  0
                     Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
 101  
                             "Error closing the writer in Downloader.", ex);
 102  0
                 }
 103  
             }
 104  0
             if (reader != null) {
 105  
                 try {
 106  0
                     reader.close();
 107  0
                 } catch (Exception ex) {
 108  0
                     Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
 109  
                             "Error closing the reader in Downloader.", ex);
 110  0
                 }
 111  
             }
 112  
             try {
 113  0
                 conn.disconnect();
 114  
             } finally {
 115  0
                 conn = null;
 116  0
             }
 117  0
         }
 118  0
     }
 119  
 
 120  
     /**
 121  
      * Makes an HTTP Head request to retrieve the last modified date of the
 122  
      * given URL. If the file:// protocol is specified, then the lastTimestamp
 123  
      * of the file is returned.
 124  
      *
 125  
      * @param url the URL to retrieve the timestamp from
 126  
      * @return an epoch timestamp
 127  
      * @throws DownloadFailedException is thrown if an exception occurs making
 128  
      * the HTTP request
 129  
      */
 130  
     public static long getLastModified(URL url) throws DownloadFailedException {
 131  14
         long timestamp = 0;
 132  
         //TODO add the FPR protocol?
 133  14
         if ("file".equalsIgnoreCase(url.getProtocol())) {
 134  
             File lastModifiedFile;
 135  
             try {
 136  
 //                if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
 137  
 //                    String filePath = url.toString();
 138  
 //                    if (filePath.matches("file://[a-zA-Z]:.*")) {
 139  
 //                        f = new File(filePath.substring(7));
 140  
 //                    } else {
 141  
 //                        f = new File(url.toURI());
 142  
 //                    }
 143  
 //                } else {
 144  14
                 lastModifiedFile = new File(url.toURI());
 145  
 //                }
 146  0
             } catch (URISyntaxException ex) {
 147  0
                 final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString());
 148  0
                 throw new DownloadFailedException(msg);
 149  14
             }
 150  14
             timestamp = lastModifiedFile.lastModified();
 151  14
         } else {
 152  0
             HttpURLConnection conn = null;
 153  
             try {
 154  0
                 conn = Downloader.getConnection(url);
 155  0
                 conn.setRequestMethod("HEAD");
 156  0
                 conn.connect();
 157  0
                 timestamp = conn.getLastModified();
 158  0
             } catch (Exception ex) {
 159  0
                 throw new DownloadFailedException("Error making HTTP HEAD request.", ex);
 160  
             } finally {
 161  0
                 if (conn != null) {
 162  
                     try {
 163  0
                         conn.disconnect();
 164  
                     } finally {
 165  0
                         conn = null;
 166  0
                     }
 167  
                 }
 168  
             }
 169  
         }
 170  14
         return timestamp;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Utility method to get an HttpURLConnection. If the app is configured to
 175  
      * use a proxy this method will retrieve the proxy settings and use them
 176  
      * when setting up the connection.
 177  
      *
 178  
      * @param url the url to connect to
 179  
      * @return an HttpURLConnection
 180  
      * @throws DownloadFailedException thrown if there is an exception
 181  
      */
 182  
     private static HttpURLConnection getConnection(URL url) throws DownloadFailedException {
 183  0
         HttpURLConnection conn = null;
 184  0
         Proxy proxy = null;
 185  0
         final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_URL);
 186  
         try {
 187  0
             if (proxyUrl != null) {
 188  0
                 final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
 189  0
                 final SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
 190  0
                 proxy = new Proxy(Proxy.Type.HTTP, addr);
 191  0
                 conn = (HttpURLConnection) url.openConnection(proxy);
 192  0
             } else {
 193  0
                 conn = (HttpURLConnection) url.openConnection();
 194  
             }
 195  0
             final int timeout = Settings.getInt(Settings.KEYS.CONNECTION_TIMEOUT, 60000);
 196  0
             conn.setConnectTimeout(timeout);
 197  0
         } catch (IOException ex) {
 198  0
             if (conn != null) {
 199  
                 try {
 200  0
                     conn.disconnect();
 201  
                 } finally {
 202  0
                     conn = null;
 203  0
                 }
 204  
             }
 205  0
             throw new DownloadFailedException("Error getting connection.", ex);
 206  0
         }
 207  0
         return conn;
 208  
     }
 209  
 }