Coverage Report - org.owasp.dependencycheck.data.update.task.CallableDownloadTask
 
Classes in this File Line Coverage Branch Coverage Complexity
CallableDownloadTask
0%
0/57
0%
0/22
2.455
 
 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) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.data.update.task;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.net.URL;
 24  
 import java.util.concurrent.Callable;
 25  
 import java.util.concurrent.ExecutorService;
 26  
 import java.util.concurrent.Future;
 27  
 import java.util.logging.Level;
 28  
 import java.util.logging.Logger;
 29  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 30  
 import org.owasp.dependencycheck.data.update.NvdCveInfo;
 31  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 32  
 import org.owasp.dependencycheck.utils.Downloader;
 33  
 
 34  
 /**
 35  
  * A callable object to download two files.
 36  
  *
 37  
  * @author Jeremy Long <jeremy.long@owasp.org>
 38  
  */
 39  0
 public class CallableDownloadTask implements Callable<Future<ProcessTask>> {
 40  
 
 41  
     /**
 42  
      * Simple constructor for the callable download task.
 43  
      *
 44  
      * @param nvdCveInfo the NVD CVE info
 45  
      * @param processor the processor service to submit the downloaded files to
 46  
      * @param cveDB the CVE DB to use to store the vulnerability data
 47  
      */
 48  0
     public CallableDownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB) {
 49  0
         this.nvdCveInfo = nvdCveInfo;
 50  0
         this.processorService = processor;
 51  0
         this.cveDB = cveDB;
 52  
 
 53  
         final File file1;
 54  
         final File file2;
 55  
 
 56  
 
 57  
         try {
 58  0
             file1 = File.createTempFile("cve" + nvdCveInfo.getId() + "_", ".xml");
 59  0
             file2 = File.createTempFile("cve_1_2_" + nvdCveInfo.getId() + "_", ".xml");
 60  0
         } catch (IOException ex) {
 61  0
             return;
 62  0
         }
 63  0
         this.first = file1;
 64  0
         this.second = file2;
 65  
 
 66  0
     }
 67  
     /**
 68  
      * The CVE DB to use when processing the files.
 69  
      */
 70  
     private CveDB cveDB;
 71  
     /**
 72  
      * The processor service to pass the results of the download to.
 73  
      */
 74  
     private ExecutorService processorService;
 75  
     /**
 76  
      * The NVD CVE Meta Data.
 77  
      */
 78  
     private NvdCveInfo nvdCveInfo;
 79  
 
 80  
     /**
 81  
      * Get the value of nvdCveInfo.
 82  
      *
 83  
      * @return the value of nvdCveInfo
 84  
      */
 85  
     public NvdCveInfo getNvdCveInfo() {
 86  0
         return nvdCveInfo;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Set the value of nvdCveInfo.
 91  
      *
 92  
      * @param nvdCveInfo new value of nvdCveInfo
 93  
      */
 94  
     public void setNvdCveInfo(NvdCveInfo nvdCveInfo) {
 95  0
         this.nvdCveInfo = nvdCveInfo;
 96  0
     }
 97  
     /**
 98  
      * a file.
 99  
      */
 100  
     private File first;
 101  
 
 102  
     /**
 103  
      * Get the value of first.
 104  
      *
 105  
      * @return the value of first
 106  
      */
 107  
     public File getFirst() {
 108  0
         return first;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Set the value of first.
 113  
      *
 114  
      * @param first new value of first
 115  
      */
 116  
     public void setFirst(File first) {
 117  0
         this.first = first;
 118  0
     }
 119  
     /**
 120  
      * a file.
 121  
      */
 122  
     private File second;
 123  
 
 124  
     /**
 125  
      * Get the value of second.
 126  
      *
 127  
      * @return the value of second
 128  
      */
 129  
     public File getSecond() {
 130  0
         return second;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Set the value of second.
 135  
      *
 136  
      * @param second new value of second
 137  
      */
 138  
     public void setSecond(File second) {
 139  0
         this.second = second;
 140  0
     }
 141  
     /**
 142  
      * A placeholder for an exception.
 143  
      */
 144  0
     private Exception exception = null;
 145  
 
 146  
     /**
 147  
      * Get the value of exception.
 148  
      *
 149  
      * @return the value of exception
 150  
      */
 151  
     public Exception getException() {
 152  0
         return exception;
 153  
     }
 154  
 
 155  
     /**
 156  
      * returns whether or not an exception occurred during download.
 157  
      *
 158  
      * @return whether or not an exception occurred during download
 159  
      */
 160  
     public boolean hasException() {
 161  0
         return exception != null;
 162  
     }
 163  
 
 164  
     @Override
 165  
     public Future<ProcessTask> call() throws Exception {
 166  
         try {
 167  0
             final URL url1 = new URL(nvdCveInfo.getUrl());
 168  0
             final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl());
 169  0
             String msg = String.format("Download Started for NVD CVE - %s", nvdCveInfo.getId());
 170  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.INFO, msg);
 171  
             try {
 172  0
                 Downloader.fetchFile(url1, first);
 173  0
                 Downloader.fetchFile(url2, second);
 174  0
             } catch (DownloadFailedException ex) {
 175  0
                 msg = String.format("Download Failed for NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId());
 176  0
                 Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.WARNING, msg);
 177  0
                 Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.FINE, null, ex);
 178  0
                 return null;
 179  0
             }
 180  
 
 181  0
             msg = String.format("Download Complete for NVD CVE - %s", nvdCveInfo.getId());
 182  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.INFO, msg);
 183  
 
 184  0
             final ProcessTask task = new ProcessTask(cveDB, this);
 185  0
             return this.processorService.submit(task);
 186  
 
 187  0
         } catch (Throwable ex) {
 188  0
             final String msg = String.format("An exception occurred downloading NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId());
 189  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.WARNING, msg);
 190  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.FINE, "Download Task Failed", ex);
 191  
         }
 192  0
         return null;
 193  
     }
 194  
 
 195  
     /**
 196  
      * Attempts to delete the files that were downloaded.
 197  
      */
 198  
     public void cleanup() {
 199  0
         boolean deleted = false;
 200  
         try {
 201  0
             if (first != null && first.exists()) {
 202  0
                 deleted = first.delete();
 203  
             }
 204  
         } finally {
 205  0
             if (first != null && (first.exists() || !deleted)) {
 206  0
                 first.deleteOnExit();
 207  
             }
 208  
         }
 209  
         try {
 210  0
             deleted = false;
 211  0
             if (second != null && second.exists()) {
 212  0
                 deleted = second.delete();
 213  
             }
 214  
         } finally {
 215  0
             if (second != null && (second.exists() || !deleted)) {
 216  0
                 second.deleteOnExit();
 217  
             }
 218  
         }
 219  0
     }
 220  
 }