Coverage Report - org.owasp.dependencycheck.data.update.CallableDownloadTask
 
Classes in this File Line Coverage Branch Coverage Complexity
CallableDownloadTask
0%
0/41
0%
0/22
2
 
 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;
 20  
 
 21  
 import java.io.File;
 22  
 import java.net.URL;
 23  
 import java.util.concurrent.Callable;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 27  
 import org.owasp.dependencycheck.utils.Downloader;
 28  
 
 29  
 /**
 30  
  * A callable object to download two files.
 31  
  *
 32  
  * @author Jeremy Long (jeremy.long@owasp.org)
 33  
  */
 34  0
 public class CallableDownloadTask implements Callable<CallableDownloadTask> {
 35  
 
 36  
     /**
 37  
      * Simple constructor for the callable download task.
 38  
      *
 39  
      * @param nvdCveInfo the nvd cve info
 40  
      * @param first the first file
 41  
      * @param second the second file
 42  
      */
 43  0
     public CallableDownloadTask(NvdCveInfo nvdCveInfo, File first, File second) {
 44  0
         this.nvdCveInfo = nvdCveInfo;
 45  0
         this.first = first;
 46  0
         this.second = second;
 47  0
     }
 48  
     /**
 49  
      * The NVD CVE Meta Data.
 50  
      */
 51  
     private NvdCveInfo nvdCveInfo;
 52  
 
 53  
     /**
 54  
      * Get the value of nvdCveInfo.
 55  
      *
 56  
      * @return the value of nvdCveInfo
 57  
      */
 58  
     public NvdCveInfo getNvdCveInfo() {
 59  0
         return nvdCveInfo;
 60  
     }
 61  
 
 62  
     /**
 63  
      * Set the value of nvdCveInfo.
 64  
      *
 65  
      * @param nvdCveInfo new value of nvdCveInfo
 66  
      */
 67  
     public void setNvdCveInfo(NvdCveInfo nvdCveInfo) {
 68  0
         this.nvdCveInfo = nvdCveInfo;
 69  0
     }
 70  
     /**
 71  
      * a file.
 72  
      */
 73  
     private File first;
 74  
 
 75  
     /**
 76  
      * Get the value of first.
 77  
      *
 78  
      * @return the value of first
 79  
      */
 80  
     public File getFirst() {
 81  0
         return first;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Set the value of first.
 86  
      *
 87  
      * @param first new value of first
 88  
      */
 89  
     public void setFirst(File first) {
 90  0
         this.first = first;
 91  0
     }
 92  
     /**
 93  
      * a file.
 94  
      */
 95  
     private File second;
 96  
 
 97  
     /**
 98  
      * Get the value of second.
 99  
      *
 100  
      * @return the value of second
 101  
      */
 102  
     public File getSecond() {
 103  0
         return second;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Set the value of second.
 108  
      *
 109  
      * @param second new value of second
 110  
      */
 111  
     public void setSecond(File second) {
 112  0
         this.second = second;
 113  0
     }
 114  
     /**
 115  
      * A placeholder for an exception.
 116  
      */
 117  0
     private Exception exception = null;
 118  
 
 119  
     /**
 120  
      * Get the value of exception.
 121  
      *
 122  
      * @return the value of exception
 123  
      */
 124  
     public Exception getException() {
 125  0
         return exception;
 126  
     }
 127  
 
 128  
     /**
 129  
      * returns whether or not an exception occurred during download.
 130  
      *
 131  
      * @return whether or not an exception occurred during download
 132  
      */
 133  
     public boolean hasException() {
 134  0
         return exception != null;
 135  
     }
 136  
 
 137  
     @Override
 138  
     public CallableDownloadTask call() throws Exception {
 139  
         try {
 140  0
             final URL url1 = new URL(nvdCveInfo.getUrl());
 141  0
             final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl());
 142  0
             String msg = String.format("Download Started for NVD CVE - %s", nvdCveInfo.getId());
 143  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.INFO, msg);
 144  0
             Downloader.fetchFile(url1, first);
 145  0
             Downloader.fetchFile(url2, second);
 146  0
             msg = String.format("Download Complete for NVD CVE - %s", nvdCveInfo.getId());
 147  0
             Logger.getLogger(CallableDownloadTask.class.getName()).log(Level.INFO, msg);
 148  0
         } catch (DownloadFailedException ex) {
 149  0
             this.exception = ex;
 150  0
         }
 151  0
         return this;
 152  
     }
 153  
 
 154  
     /**
 155  
      * Attempts to delete the files that were downloaded.
 156  
      */
 157  
     public void cleanup() {
 158  0
         boolean deleted = false;
 159  
         try {
 160  0
             if (first != null && first.exists()) {
 161  0
                 deleted = first.delete();
 162  
             }
 163  
         } finally {
 164  0
             if (first != null && (first.exists() || !deleted)) {
 165  0
                 first.deleteOnExit();
 166  
             }
 167  
         }
 168  
         try {
 169  0
             deleted = false;
 170  0
             if (second != null && second.exists()) {
 171  0
                 deleted = second.delete();
 172  
             }
 173  
         } finally {
 174  0
             if (second != null && (second.exists() || !deleted)) {
 175  0
                 second.deleteOnExit();
 176  
             }
 177  
         }
 178  0
     }
 179  
 }