Coverage Report - org.owasp.dependencycheck.data.update.task.ProcessTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessTask
0%
0/49
N/A
3.5
 
 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.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import java.sql.SQLException;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import java.util.concurrent.Callable;
 27  
 import java.util.logging.Level;
 28  
 import java.util.logging.Logger;
 29  
 import javax.xml.parsers.ParserConfigurationException;
 30  
 import javax.xml.parsers.SAXParser;
 31  
 import javax.xml.parsers.SAXParserFactory;
 32  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 33  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 34  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 35  
 import org.owasp.dependencycheck.data.update.StandardUpdate;
 36  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 37  
 import org.owasp.dependencycheck.data.update.xml.NvdCve12Handler;
 38  
 import org.owasp.dependencycheck.data.update.xml.NvdCve20Handler;
 39  
 import org.owasp.dependencycheck.dependency.VulnerableSoftware;
 40  
 import org.xml.sax.SAXException;
 41  
 
 42  
 /**
 43  
  * A callable task that will process a given set of NVD CVE xml files and update the Cve Database accordingly.
 44  
  *
 45  
  * @author Jeremy Long <jeremy.long@owasp.org>
 46  
  */
 47  0
 public class ProcessTask implements Callable<ProcessTask> {
 48  
 
 49  
     /**
 50  
      * A field to store any update exceptions that occur during the "call".
 51  
      */
 52  0
     private UpdateException exception = null;
 53  
 
 54  
     /**
 55  
      * Get the value of exception.
 56  
      *
 57  
      * @return the value of exception
 58  
      */
 59  
     public UpdateException getException() {
 60  0
         return exception;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Set the value of exception.
 65  
      *
 66  
      * @param exception new value of exception
 67  
      */
 68  
     public void setException(UpdateException exception) {
 69  0
         this.exception = exception;
 70  0
     }
 71  
     /**
 72  
      * A reference to the CveDB.
 73  
      */
 74  
     private final CveDB cveDB;
 75  
     /**
 76  
      * A reference to the callable download task.
 77  
      */
 78  
     private final CallableDownloadTask filePair;
 79  
     /**
 80  
      * A reference to the properties.
 81  
      */
 82  
     private final DatabaseProperties properties;
 83  
 
 84  
     /**
 85  
      * Constructs a new ProcessTask used to process an NVD CVE update.
 86  
      *
 87  
      * @param cveDB the data store object
 88  
      * @param filePair the download task that contains the URL references to download
 89  
      */
 90  0
     public ProcessTask(final CveDB cveDB, final CallableDownloadTask filePair) {
 91  0
         this.cveDB = cveDB;
 92  0
         this.filePair = filePair;
 93  0
         this.properties = cveDB.getDatabaseProperties();
 94  0
     }
 95  
 
 96  
     /**
 97  
      * Implements the callable interface.
 98  
      *
 99  
      * @return this object
 100  
      * @throws Exception thrown if there is an exception; note that any UpdateExceptions are simply added to the tasks
 101  
      * exception collection
 102  
      */
 103  
     @Override
 104  
     public ProcessTask call() throws Exception {
 105  
         try {
 106  0
             processFiles();
 107  0
         } catch (UpdateException ex) {
 108  0
             this.exception = ex;
 109  0
         }
 110  0
         return this;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Imports the NVD CVE XML File into the Lucene Index.
 115  
      *
 116  
      * @param file the file containing the NVD CVE XML
 117  
      * @param oldVersion contains the file containing the NVD CVE XML 1.2
 118  
      * @throws ParserConfigurationException is thrown if there is a parser configuration exception
 119  
      * @throws SAXException is thrown if there is a SAXException
 120  
      * @throws IOException is thrown if there is a IO Exception
 121  
      * @throws SQLException is thrown if there is a SQL exception
 122  
      * @throws DatabaseException is thrown if there is a database exception
 123  
      * @throws ClassNotFoundException thrown if the h2 database driver cannot be loaded
 124  
      */
 125  
     protected void importXML(File file, File oldVersion) throws ParserConfigurationException,
 126  
             SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException {
 127  
 
 128  0
         final SAXParserFactory factory = SAXParserFactory.newInstance();
 129  0
         final SAXParser saxParser = factory.newSAXParser();
 130  
 
 131  0
         final NvdCve12Handler cve12Handler = new NvdCve12Handler();
 132  0
         saxParser.parse(oldVersion, cve12Handler);
 133  0
         final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities();
 134  
 
 135  0
         final NvdCve20Handler cve20Handler = new NvdCve20Handler();
 136  0
         cve20Handler.setCveDB(cveDB);
 137  0
         cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap);
 138  0
         saxParser.parse(file, cve20Handler);
 139  0
     }
 140  
 
 141  
     /**
 142  
      * Processes the NVD CVE XML file and imports the data into the DB.
 143  
      *
 144  
      * @throws UpdateException thrown if there is an error loading the data into the database
 145  
      */
 146  
     private void processFiles() throws UpdateException {
 147  0
         String msg = String.format("Processing Started for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 148  0
         Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, msg);
 149  
         try {
 150  0
             importXML(filePair.getFirst(), filePair.getSecond());
 151  0
             cveDB.commit();
 152  0
             properties.save(filePair.getNvdCveInfo());
 153  0
         } catch (FileNotFoundException ex) {
 154  0
             throw new UpdateException(ex);
 155  0
         } catch (ParserConfigurationException ex) {
 156  0
             throw new UpdateException(ex);
 157  0
         } catch (SAXException ex) {
 158  0
             throw new UpdateException(ex);
 159  0
         } catch (IOException ex) {
 160  0
             throw new UpdateException(ex);
 161  0
         } catch (SQLException ex) {
 162  0
             throw new UpdateException(ex);
 163  0
         } catch (DatabaseException ex) {
 164  0
             throw new UpdateException(ex);
 165  0
         } catch (ClassNotFoundException ex) {
 166  0
             throw new UpdateException(ex);
 167  
         } finally {
 168  0
             filePair.cleanup();
 169  0
         }
 170  0
         msg = String.format("Processing Complete for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 171  0
         Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, msg);
 172  0
     }
 173  
 }