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