Coverage Report - org.owasp.dependencycheck.utils.Downloader
 
Classes in this File Line Coverage Branch Coverage Complexity
Downloader
0%
0/80
0%
0/30
8.25
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 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  
  * A utility to download files from the Internet.
 35  
  *
 36  
  * @author Jeremy Long <jeremy.long@owasp.org>
 37  
  */
 38  
 public final class Downloader {
 39  
 
 40  
     /**
 41  
      * Private constructor for utility class.
 42  
      */
 43  
     private Downloader() {
 44  
     }
 45  
 
 46  
     /**
 47  
      * Retrieves a file from a given URL and saves it to the outputPath.
 48  
      *
 49  
      * @param url the URL of the file to download
 50  
      * @param outputPath the path to the save the file to
 51  
      * @throws DownloadFailedException is thrown if there is an error downloading the file
 52  
      */
 53  
     public static void fetchFile(URL url, File outputPath) throws DownloadFailedException {
 54  0
         fetchFile(url, outputPath, true);
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Retrieves a file from a given URL and saves it to the outputPath.
 59  
      *
 60  
      * @param url the URL of the file to download
 61  
      * @param outputPath the path to the save the file to
 62  
      * @param useProxy whether to use the configured proxy when downloading files
 63  
      * @throws DownloadFailedException is thrown if there is an error downloading the file
 64  
      */
 65  
     public static void fetchFile(URL url, File outputPath, boolean useProxy) throws DownloadFailedException {
 66  0
         if ("file".equalsIgnoreCase(url.getProtocol())) {
 67  
             File file;
 68  
             try {
 69  0
                 file = new File(url.toURI());
 70  0
             } catch (URISyntaxException ex) {
 71  0
                 final String msg = String.format("Download failed, unable to locate '%s'", url.toString());
 72  0
                 throw new DownloadFailedException(msg);
 73  0
             }
 74  0
             if (file.exists()) {
 75  
                 try {
 76  0
                     org.apache.commons.io.FileUtils.copyFile(file, outputPath);
 77  0
                 } catch (IOException ex) {
 78  0
                     final String msg = String.format("Download failed, unable to copy '%s'", url.toString());
 79  0
                     throw new DownloadFailedException(msg);
 80  0
                 }
 81  
             } else {
 82  0
                 final String msg = String.format("Download failed, file does not exist '%s'", url.toString());
 83  0
                 throw new DownloadFailedException(msg);
 84  
             }
 85  0
         } else {
 86  0
             HttpURLConnection conn = null;
 87  
             try {
 88  0
                 conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
 89  0
                 conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
 90  0
                 conn.connect();
 91  0
             } catch (IOException ex) {
 92  
                 try {
 93  0
                     if (conn != null) {
 94  0
                         conn.disconnect();
 95  
                     }
 96  
                 } finally {
 97  0
                     conn = null;
 98  0
                 }
 99  0
                 throw new DownloadFailedException("Error downloading file.", ex);
 100  0
             }
 101  0
             final String encoding = conn.getContentEncoding();
 102  
 
 103  0
             BufferedOutputStream writer = null;
 104  0
             InputStream reader = null;
 105  
             try {
 106  0
                 if (encoding != null && "gzip".equalsIgnoreCase(encoding)) {
 107  0
                     reader = new GZIPInputStream(conn.getInputStream());
 108  0
                 } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) {
 109  0
                     reader = new InflaterInputStream(conn.getInputStream());
 110  
                 } else {
 111  0
                     reader = conn.getInputStream();
 112  
                 }
 113  
 
 114  0
                 writer = new BufferedOutputStream(new FileOutputStream(outputPath));
 115  0
                 final byte[] buffer = new byte[4096];
 116  
                 int bytesRead;
 117  0
                 while ((bytesRead = reader.read(buffer)) > 0) {
 118  0
                     writer.write(buffer, 0, bytesRead);
 119  
                 }
 120  0
             } catch (Throwable ex) {
 121  0
                 throw new DownloadFailedException("Error saving downloaded file.", ex);
 122  
             } finally {
 123  0
                 if (writer != null) {
 124  
                     try {
 125  0
                         writer.close();
 126  0
                     } catch (Throwable ex) {
 127  0
                         Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
 128  
                                 "Error closing the writer in Downloader.", ex);
 129  0
                     }
 130  
                 }
 131  0
                 if (reader != null) {
 132  
                     try {
 133  0
                         reader.close();
 134  0
                     } catch (Throwable ex) {
 135  0
                         Logger.getLogger(Downloader.class.getName()).log(Level.FINEST,
 136  
                                 "Error closing the reader in Downloader.", ex);
 137  0
                     }
 138  
                 }
 139  
                 try {
 140  0
                     conn.disconnect();
 141  
                 } finally {
 142  0
                     conn = null;
 143  0
                 }
 144  0
             }
 145  
         }
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// protocol is
 150  
      * specified, then the lastTimestamp of the file is returned.
 151  
      *
 152  
      * @param url the URL to retrieve the timestamp from
 153  
      * @return an epoch timestamp
 154  
      * @throws DownloadFailedException is thrown if an exception occurs making the HTTP request
 155  
      */
 156  
     public static long getLastModified(URL url) throws DownloadFailedException {
 157  0
         long timestamp = 0;
 158  
         //TODO add the FTP protocol?
 159  0
         if ("file".equalsIgnoreCase(url.getProtocol())) {
 160  
             File lastModifiedFile;
 161  
             try {
 162  0
                 lastModifiedFile = new File(url.toURI());
 163  0
             } catch (URISyntaxException ex) {
 164  0
                 final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString());
 165  0
                 throw new DownloadFailedException(msg);
 166  0
             }
 167  0
             timestamp = lastModifiedFile.lastModified();
 168  0
         } else {
 169  0
             HttpURLConnection conn = null;
 170  
             try {
 171  0
                 conn = URLConnectionFactory.createHttpURLConnection(url);
 172  0
                 conn.setRequestMethod("HEAD");
 173  0
                 conn.connect();
 174  0
                 timestamp = conn.getLastModified();
 175  0
             } catch (URLConnectionFailureException ex) {
 176  0
                 throw new DownloadFailedException("Error creating URL Connection for HTTP HEAD request.", ex);
 177  0
             } catch (IOException ex) {
 178  0
                 throw new DownloadFailedException("Error making HTTP HEAD request.", ex);
 179  
             } finally {
 180  0
                 if (conn != null) {
 181  
                     try {
 182  0
                         conn.disconnect();
 183  
                     } finally {
 184  0
                         conn = null;
 185  0
                     }
 186  
                 }
 187  
             }
 188  
         }
 189  0
         return timestamp;
 190  
     }
 191  
 }