Coverage Report - org.owasp.dependencycheck.utils.Downloader
 
Classes in this File Line Coverage Branch Coverage Complexity
Downloader
11%
7/63
3%
1/26
8.333
 
 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
         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  
      * Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// protocol is
 117  
      * specified, then the lastTimestamp of the file is returned.
 118  
      *
 119  
      * @param url the URL to retrieve the timestamp from
 120  
      * @return an epoch timestamp
 121  
      * @throws DownloadFailedException is thrown if an exception occurs making the HTTP request
 122  
      */
 123  
     public static long getLastModified(URL url) throws DownloadFailedException {
 124  10
         long timestamp = 0;
 125  
         //TODO add the FPR protocol?
 126  10
         if ("file".equalsIgnoreCase(url.getProtocol())) {
 127  
             File lastModifiedFile;
 128  
             try {
 129  
 //                if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
 130  
 //                    String filePath = url.toString();
 131  
 //                    if (filePath.matches("file://[a-zA-Z]:.*")) {
 132  
 //                        f = new File(filePath.substring(7));
 133  
 //                    } else {
 134  
 //                        f = new File(url.toURI());
 135  
 //                    }
 136  
 //                } else {
 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  
 }