Coverage Report - org.owasp.dependencycheck.data.update.nvd.DownloadTask
 
Classes in this File Line Coverage Branch Coverage Complexity
DownloadTask
53%
50/94
22%
10/44
3.583
 
 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) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.update.nvd;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileInputStream;
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.net.URL;
 26  
 import java.util.concurrent.Callable;
 27  
 import java.util.concurrent.ExecutorService;
 28  
 import java.util.concurrent.Future;
 29  
 import java.util.zip.GZIPInputStream;
 30  
 import org.apache.commons.io.FileUtils;
 31  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 32  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 33  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 34  
 import org.owasp.dependencycheck.utils.Downloader;
 35  
 import org.owasp.dependencycheck.utils.Settings;
 36  
 import org.slf4j.Logger;
 37  
 import org.slf4j.LoggerFactory;
 38  
 
 39  
 /**
 40  
  * A callable object to download two files.
 41  
  *
 42  
  * @author Jeremy Long
 43  
  */
 44  0
 public class DownloadTask implements Callable<Future<ProcessTask>> {
 45  
 
 46  
     /**
 47  
      * The Logger.
 48  
      */
 49  1
     private static final Logger LOGGER = LoggerFactory.getLogger(DownloadTask.class);
 50  
 
 51  
     /**
 52  
      * Simple constructor for the callable download task.
 53  
      *
 54  
      * @param nvdCveInfo the NVD CVE info
 55  
      * @param processor the processor service to submit the downloaded files to
 56  
      * @param cveDB the CVE DB to use to store the vulnerability data
 57  
      * @param settings a reference to the global settings object; this is necessary so that when the thread is started the
 58  
      * dependencies have a correct reference to the global settings.
 59  
      * @throws UpdateException thrown if temporary files could not be created
 60  
      */
 61  1
     public DownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException {
 62  1
         this.nvdCveInfo = nvdCveInfo;
 63  1
         this.processorService = processor;
 64  1
         this.cveDB = cveDB;
 65  1
         this.settings = settings;
 66  
 
 67  
         final File file1;
 68  
         final File file2;
 69  
 
 70  
         try {
 71  1
             file1 = File.createTempFile("cve" + nvdCveInfo.getId() + '_', ".xml", Settings.getTempDirectory());
 72  1
             file2 = File.createTempFile("cve_1_2_" + nvdCveInfo.getId() + '_', ".xml", Settings.getTempDirectory());
 73  0
         } catch (IOException ex) {
 74  0
             throw new UpdateException("Unable to create temporary files", ex);
 75  1
         }
 76  1
         this.first = file1;
 77  1
         this.second = file2;
 78  
 
 79  1
     }
 80  
     /**
 81  
      * The CVE DB to use when processing the files.
 82  
      */
 83  
     private CveDB cveDB;
 84  
     /**
 85  
      * The processor service to pass the results of the download to.
 86  
      */
 87  
     private ExecutorService processorService;
 88  
     /**
 89  
      * The NVD CVE Meta Data.
 90  
      */
 91  
     private NvdCveInfo nvdCveInfo;
 92  
     /**
 93  
      * A reference to the global settings object.
 94  
      */
 95  
     private Settings settings;
 96  
 
 97  
     /**
 98  
      * Get the value of nvdCveInfo.
 99  
      *
 100  
      * @return the value of nvdCveInfo
 101  
      */
 102  
     public NvdCveInfo getNvdCveInfo() {
 103  0
         return nvdCveInfo;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Set the value of nvdCveInfo.
 108  
      *
 109  
      * @param nvdCveInfo new value of nvdCveInfo
 110  
      */
 111  
     public void setNvdCveInfo(NvdCveInfo nvdCveInfo) {
 112  0
         this.nvdCveInfo = nvdCveInfo;
 113  0
     }
 114  
     /**
 115  
      * a file.
 116  
      */
 117  
     private File first;
 118  
 
 119  
     /**
 120  
      * Get the value of first.
 121  
      *
 122  
      * @return the value of first
 123  
      */
 124  
     public File getFirst() {
 125  0
         return first;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Set the value of first.
 130  
      *
 131  
      * @param first new value of first
 132  
      */
 133  
     public void setFirst(File first) {
 134  0
         this.first = first;
 135  0
     }
 136  
     /**
 137  
      * a file.
 138  
      */
 139  
     private File second;
 140  
 
 141  
     /**
 142  
      * Get the value of second.
 143  
      *
 144  
      * @return the value of second
 145  
      */
 146  
     public File getSecond() {
 147  0
         return second;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Set the value of second.
 152  
      *
 153  
      * @param second new value of second
 154  
      */
 155  
     public void setSecond(File second) {
 156  0
         this.second = second;
 157  0
     }
 158  
     /**
 159  
      * A placeholder for an exception.
 160  
      */
 161  1
     private Exception exception = null;
 162  
 
 163  
     /**
 164  
      * Get the value of exception.
 165  
      *
 166  
      * @return the value of exception
 167  
      */
 168  
     public Exception getException() {
 169  0
         return exception;
 170  
     }
 171  
 
 172  
     /**
 173  
      * returns whether or not an exception occurred during download.
 174  
      *
 175  
      * @return whether or not an exception occurred during download
 176  
      */
 177  
     public boolean hasException() {
 178  0
         return exception != null;
 179  
     }
 180  
 
 181  
     @Override
 182  
     public Future<ProcessTask> call() throws Exception {
 183  
         try {
 184  1
             Settings.setInstance(settings);
 185  1
             final URL url1 = new URL(nvdCveInfo.getUrl());
 186  1
             final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl());
 187  1
             LOGGER.info("Download Started for NVD CVE - {}", nvdCveInfo.getId());
 188  1
             final long startDownload = System.currentTimeMillis();
 189  
             try {
 190  1
                 Downloader.fetchFile(url1, first);
 191  1
                 Downloader.fetchFile(url2, second);
 192  0
             } catch (DownloadFailedException ex) {
 193  0
                 LOGGER.warn("Download Failed for NVD CVE - {}\nSome CVEs may not be reported.", nvdCveInfo.getId());
 194  0
                 if (Settings.getString(Settings.KEYS.PROXY_SERVER) == null) {
 195  0
                     LOGGER.info(
 196  
                             "If you are behind a proxy you may need to configure dependency-check to use the proxy.");
 197  
                 }
 198  0
                 LOGGER.debug("", ex);
 199  0
                 return null;
 200  1
             }
 201  1
             if (url1.toExternalForm().endsWith(".xml.gz")) {
 202  1
                 extractGzip(first);
 203  
             }
 204  1
             if (url2.toExternalForm().endsWith(".xml.gz")) {
 205  1
                 extractGzip(second);
 206  
             }
 207  
 
 208  1
             LOGGER.info("Download Complete for NVD CVE - {}  ({} ms)", nvdCveInfo.getId(),
 209  
                 System.currentTimeMillis() - startDownload);
 210  1
             if (this.processorService == null) {
 211  1
                 return null;
 212  
             }
 213  0
             final ProcessTask task = new ProcessTask(cveDB, this, settings);
 214  0
             return this.processorService.submit(task);
 215  
 
 216  0
         } catch (Throwable ex) {
 217  0
             LOGGER.warn("An exception occurred downloading NVD CVE - {}\nSome CVEs may not be reported.", nvdCveInfo.getId());
 218  0
             LOGGER.debug("Download Task Failed", ex);
 219  
         } finally {
 220  1
             Settings.cleanup(false);
 221  0
         }
 222  0
         return null;
 223  
     }
 224  
 
 225  
     /**
 226  
      * Attempts to delete the files that were downloaded.
 227  
      */
 228  
     public void cleanup() {
 229  0
         boolean deleted = false;
 230  
         try {
 231  0
             if (first != null && first.exists()) {
 232  0
                 deleted = first.delete();
 233  
             }
 234  
         } finally {
 235  0
             if (first != null && (first.exists() || !deleted)) {
 236  0
                 first.deleteOnExit();
 237  
             }
 238  
         }
 239  
         try {
 240  0
             deleted = false;
 241  0
             if (second != null && second.exists()) {
 242  0
                 deleted = second.delete();
 243  
             }
 244  
         } finally {
 245  0
             if (second != null && (second.exists() || !deleted)) {
 246  0
                 second.deleteOnExit();
 247  
             }
 248  
         }
 249  0
     }
 250  
 
 251  
     /**
 252  
      * Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified.
 253  
      *
 254  
      * @param file the archive file
 255  
      * @throws FileNotFoundException thrown if the file does not exist
 256  
      * @throws IOException thrown if there is an error extracting the file.
 257  
      */
 258  
     private void extractGzip(File file) throws FileNotFoundException, IOException {
 259  2
         final String originalPath = file.getPath();
 260  2
         final File gzip = new File(originalPath + ".gz");
 261  2
         if (gzip.isFile() && !gzip.delete()) {
 262  0
             gzip.deleteOnExit();
 263  
         }
 264  2
         if (!file.renameTo(gzip)) {
 265  0
             throw new IOException("Unable to rename '" + file.getPath() + "'");
 266  
         }
 267  2
         final File newfile = new File(originalPath);
 268  
 
 269  2
         final byte[] buffer = new byte[4096];
 270  
 
 271  2
         GZIPInputStream cin = null;
 272  2
         FileOutputStream out = null;
 273  
         try {
 274  2
             cin = new GZIPInputStream(new FileInputStream(gzip));
 275  2
             out = new FileOutputStream(newfile);
 276  
 
 277  
             int len;
 278  733
             while ((len = cin.read(buffer)) > 0) {
 279  731
                 out.write(buffer, 0, len);
 280  
             }
 281  
         } finally {
 282  2
             if (cin != null) {
 283  
                 try {
 284  2
                     cin.close();
 285  0
                 } catch (IOException ex) {
 286  0
                     LOGGER.trace("ignore", ex);
 287  2
                 }
 288  
             }
 289  2
             if (out != null) {
 290  
                 try {
 291  2
                     out.close();
 292  0
                 } catch (IOException ex) {
 293  0
                     LOGGER.trace("ignore", ex);
 294  2
                 }
 295  
             }
 296  2
             if (gzip.isFile()) {
 297  2
                 FileUtils.deleteQuietly(gzip);
 298  
             }
 299  
         }
 300  2
     }
 301  
 }