Coverage Report - org.owasp.dependencycheck.data.update.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  
  * 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.io.FileNotFoundException;
 23  
 import java.io.IOException;
 24  
 import java.sql.SQLException;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.concurrent.Callable;
 28  
 import java.util.logging.Level;
 29  
 import java.util.logging.Logger;
 30  
 import javax.xml.parsers.ParserConfigurationException;
 31  
 import javax.xml.parsers.SAXParser;
 32  
 import javax.xml.parsers.SAXParserFactory;
 33  
 import org.owasp.dependencycheck.data.UpdateException;
 34  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 35  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 36  
 import org.owasp.dependencycheck.data.nvdcve.NvdCve12Handler;
 37  
 import org.owasp.dependencycheck.data.nvdcve.NvdCve20Handler;
 38  
 import org.owasp.dependencycheck.dependency.VulnerableSoftware;
 39  
 import org.xml.sax.SAXException;
 40  
 
 41  
 /**
 42  
  * A callable task that will process a given set of NVD CVE xml files and update
 43  
  * 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  
     private final CveDB cveDB;
 72  
     private final CallableDownloadTask filePair;
 73  
     private final DataStoreMetaInfo properties;
 74  
 
 75  0
     public ProcessTask(final CveDB cveDB, final DataStoreMetaInfo properties, final CallableDownloadTask filePair) {
 76  0
         this.cveDB = cveDB;
 77  0
         this.filePair = filePair;
 78  0
         this.properties = properties;
 79  0
     }
 80  
 
 81  
     @Override
 82  
     public ProcessTask call() throws Exception {
 83  
         try {
 84  0
             processFiles();
 85  0
         } catch (UpdateException ex) {
 86  0
             this.exception = ex;
 87  0
         }
 88  0
         return this;
 89  
     }
 90  
 
 91  
     /**
 92  
      * Imports the NVD CVE XML File into the Lucene Index.
 93  
      *
 94  
      * @param file the file containing the NVD CVE XML
 95  
      * @param oldVersion contains the file containing the NVD CVE XML 1.2
 96  
      * @throws ParserConfigurationException is thrown if there is a parser
 97  
      * configuration exception
 98  
      * @throws SAXException is thrown if there is a SAXException
 99  
      * @throws IOException is thrown if there is a IO Exception
 100  
      * @throws SQLException is thrown if there is a SQL exception
 101  
      * @throws DatabaseException is thrown if there is a database exception
 102  
      * @throws ClassNotFoundException thrown if the h2 database driver cannot be
 103  
      * loaded
 104  
      */
 105  
     protected void importXML(File file, File oldVersion) throws ParserConfigurationException,
 106  
             SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException {
 107  
 
 108  0
         final SAXParserFactory factory = SAXParserFactory.newInstance();
 109  0
         final SAXParser saxParser = factory.newSAXParser();
 110  
 
 111  0
         final NvdCve12Handler cve12Handler = new NvdCve12Handler();
 112  0
         saxParser.parse(oldVersion, cve12Handler);
 113  0
         final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities();
 114  
 
 115  0
         final NvdCve20Handler cve20Handler = new NvdCve20Handler();
 116  0
         cve20Handler.setCveDB(cveDB);
 117  0
         cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap);
 118  0
         saxParser.parse(file, cve20Handler);
 119  0
     }
 120  
 
 121  
     private void processFiles() throws UpdateException {
 122  0
         String msg = String.format("Processing Started for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 123  0
         Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, msg);
 124  
         try {
 125  0
             importXML(filePair.getFirst(), filePair.getSecond());
 126  0
             cveDB.commit();
 127  0
             properties.save(filePair.getNvdCveInfo());
 128  0
         } catch (FileNotFoundException ex) {
 129  0
             throw new UpdateException(ex);
 130  0
         } catch (ParserConfigurationException ex) {
 131  0
             throw new UpdateException(ex);
 132  0
         } catch (SAXException ex) {
 133  0
             throw new UpdateException(ex);
 134  0
         } catch (IOException ex) {
 135  0
             throw new UpdateException(ex);
 136  0
         } catch (SQLException ex) {
 137  0
             throw new UpdateException(ex);
 138  0
         } catch (DatabaseException ex) {
 139  0
             throw new UpdateException(ex);
 140  0
         } catch (ClassNotFoundException ex) {
 141  0
             throw new UpdateException(ex);
 142  
         } finally {
 143  0
             filePair.cleanup();
 144  0
         }
 145  0
         msg = String.format("Processing Complete for NVD CVE - %s", filePair.getNvdCveInfo().getId());
 146  0
         Logger.getLogger(StandardUpdate.class.getName()).log(Level.INFO, msg);
 147  0
     }
 148  
 }