Coverage Report - org.owasp.dependencycheck.data.nvdcve.DatabaseProperties
 
Classes in this File Line Coverage Branch Coverage Complexity
DatabaseProperties
92%
13/14
50%
3/6
1.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.nvdcve;
 20  
 
 21  
 import java.util.Properties;
 22  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 23  
 import org.owasp.dependencycheck.data.update.NvdCveInfo;
 24  
 
 25  
 /**
 26  
  * This is a wrapper around a set of properties that are stored in the database.
 27  
  *
 28  
  * @author Jeremy Long <jeremy.long@owasp.org>
 29  
  */
 30  
 public class DatabaseProperties {
 31  
 
 32  
     /**
 33  
      * Modified key word, used as a key to store information about the modified
 34  
      * file (i.e. the containing the last 8 days of updates)..
 35  
      */
 36  
     public static final String MODIFIED = "modified";
 37  
     /**
 38  
      * The properties file key for the last updated field - used to store the
 39  
      * last updated time of the Modified NVD CVE xml file.
 40  
      */
 41  
     public static final String LAST_UPDATED = "lastupdated.modified";
 42  
     /**
 43  
      * Stores the last updated time for each of the NVD CVE files. These
 44  
      * timestamps should be updated if we process the modified file within 7
 45  
      * days of the last update.
 46  
      */
 47  
     public static final String LAST_UPDATED_BASE = "lastupdated.";
 48  
     /**
 49  
      * A collection of properties about the data.
 50  
      */
 51  
     private Properties properties;
 52  
     /**
 53  
      * A reference to the database.
 54  
      */
 55  
     private CveDB cveDB;
 56  
 
 57  
     /**
 58  
      * Constructs a new data properties object.
 59  
      *
 60  
      * @param cveDB the database object holding the properties
 61  
      */
 62  24
     DatabaseProperties(CveDB cveDB) {
 63  24
         this.cveDB = cveDB;
 64  24
         loadProperties();
 65  24
     }
 66  
 
 67  
     /**
 68  
      * Loads the properties from the database.
 69  
      */
 70  
     private void loadProperties() {
 71  24
         this.properties = cveDB.getProperties();
 72  24
     }
 73  
 
 74  
     /**
 75  
      * Returns whether or not any properties are set.
 76  
      *
 77  
      * @return whether or not any properties are set
 78  
      */
 79  
     public boolean isEmpty() {
 80  1
         return properties == null || properties.isEmpty();
 81  
     }
 82  
 
 83  
     /**
 84  
      * Writes a properties file containing the last updated date to the
 85  
      * VULNERABLE_CPE directory.
 86  
      *
 87  
      * @param updatedValue the updated NVD CVE entry
 88  
      * @throws UpdateException is thrown if there is an update exception
 89  
      */
 90  
     public void save(NvdCveInfo updatedValue) throws UpdateException {
 91  1
         if (updatedValue == null) {
 92  0
             return;
 93  
         }
 94  1
         properties.put(LAST_UPDATED_BASE + updatedValue.getId(), String.valueOf(updatedValue.getTimestamp()));
 95  1
         cveDB.saveProperty(LAST_UPDATED_BASE + updatedValue.getId(), String.valueOf(updatedValue.getTimestamp()));
 96  1
     }
 97  
 
 98  
     /**
 99  
      * Returns the property value for the given key. If the key is not contained
 100  
      * in the underlying properties null is returned.
 101  
      *
 102  
      * @param key the property key
 103  
      * @return the value of the property
 104  
      */
 105  
     public String getProperty(String key) {
 106  1
         return properties.getProperty(key);
 107  
     }
 108  
 
 109  
     /**
 110  
      * Returns the property value for the given key. If the key is not contained
 111  
      * in the underlying properties the default value is returned.
 112  
      *
 113  
      * @param key the property key
 114  
      * @param defaultValue the default value
 115  
      * @return the value of the property
 116  
      */
 117  
     public String getProperty(String key, String defaultValue) {
 118  1
         return properties.getProperty(key, defaultValue);
 119  
     }
 120  
 }