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