Coverage Report - org.owasp.dependencycheck.data.update.nvd.UpdateableNvdCve
 
Classes in this File Line Coverage Branch Coverage Complexity
UpdateableNvdCve
93%
28/30
100%
4/4
1.25
 
 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) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.update.nvd;
 19  
 
 20  
 import java.net.MalformedURLException;
 21  
 import java.net.URL;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Map.Entry;
 25  
 import java.util.TreeMap;
 26  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 27  
 import org.owasp.dependencycheck.utils.Downloader;
 28  
 
 29  
 /**
 30  
  * Contains a collection of updateable NvdCveInfo objects. This is used to determine which files need to be downloaded and
 31  
  * processed.
 32  
  *
 33  
  * @author Jeremy Long
 34  
  */
 35  12
 public class UpdateableNvdCve implements Iterable<NvdCveInfo>, Iterator<NvdCveInfo> {
 36  
 
 37  
     /**
 38  
      * A collection of sources of data.
 39  
      */
 40  5
     private final Map<String, NvdCveInfo> collection = new TreeMap<String, NvdCveInfo>();
 41  
 
 42  
     /**
 43  
      * Returns the collection of NvdCveInfo objects. This method is mainly used for testing.
 44  
      *
 45  
      * @return the collection of NvdCveInfo objects
 46  
      */
 47  
     protected Map<String, NvdCveInfo> getCollection() {
 48  3
         return collection;
 49  
     }
 50  
 
 51  
     /**
 52  
      * Gets whether or not an update is needed.
 53  
      *
 54  
      * @return true or false depending on whether an update is needed
 55  
      */
 56  
     public boolean isUpdateNeeded() {
 57  3
         for (NvdCveInfo item : this) {
 58  4
             if (item.getNeedsUpdate()) {
 59  1
                 return true;
 60  
             }
 61  3
         }
 62  2
         return false;
 63  
     }
 64  
 
 65  
     /**
 66  
      * Adds a new entry of updateable information to the contained collection.
 67  
      *
 68  
      * @param id the key for the item to be added
 69  
      * @param url the URL to download the item
 70  
      * @param oldUrl the URL for the old version of the item (the NVD CVE old schema still contains useful data we need).
 71  
      * @throws MalformedURLException thrown if the URL provided is invalid
 72  
      * @throws DownloadFailedException thrown if the download fails.
 73  
      */
 74  
     public void add(String id, String url, String oldUrl) throws MalformedURLException, DownloadFailedException {
 75  1
         add(id, url, oldUrl, false);
 76  1
     }
 77  
 
 78  
     /**
 79  
      * Adds a new entry of updateable information to the contained collection.
 80  
      *
 81  
      * @param id the key for the item to be added
 82  
      * @param url the URL to download the item
 83  
      * @param oldUrl the URL for the old version of the item (the NVD CVE old schema still contains useful data we need).
 84  
      * @param needsUpdate whether or not the data needs to be updated
 85  
      * @throws MalformedURLException thrown if the URL provided is invalid
 86  
      * @throws DownloadFailedException thrown if the download fails.
 87  
      */
 88  
     public void add(String id, String url, String oldUrl, boolean needsUpdate) throws MalformedURLException, DownloadFailedException {
 89  9
         final NvdCveInfo item = new NvdCveInfo();
 90  9
         item.setNeedsUpdate(needsUpdate); //the others default to true, to make life easier later this should default to false.
 91  9
         item.setId(id);
 92  9
         item.setUrl(url);
 93  9
         item.setOldSchemaVersionUrl(oldUrl);
 94  9
         item.setTimestamp(Downloader.getLastModified(new URL(url)));
 95  9
         collection.put(id, item);
 96  9
     }
 97  
 
 98  
     /**
 99  
      * Clears the contained collection of NvdCveInfo entries.
 100  
      */
 101  
     public void clear() {
 102  1
         collection.clear();
 103  1
     }
 104  
 
 105  
     /**
 106  
      * Returns the timestamp for the given entry.
 107  
      *
 108  
      * @param key the key to lookup in the collection of NvdCveInfo items
 109  
      * @return the timestamp for the given entry
 110  
      */
 111  
     public long getTimeStamp(String key) {
 112  0
         return collection.get(key).getTimestamp();
 113  
     }
 114  
     /**
 115  
      * An internal iterator used to implement iterable.
 116  
      */
 117  5
     private Iterator<Entry<String, NvdCveInfo>> iterableContent = null;
 118  
 
 119  
     /**
 120  
      * <p>
 121  
      * Returns an iterator for the NvdCveInfo contained.</p>
 122  
      * <p>
 123  
      * <b>This method is not thread safe.</b></p>
 124  
      *
 125  
      * @return an NvdCveInfo Iterator
 126  
      */
 127  
     @Override
 128  
     public Iterator<NvdCveInfo> iterator() {
 129  4
         iterableContent = collection.entrySet().iterator();
 130  4
         return this;
 131  
     }
 132  
 
 133  
     /**
 134  
      * <p>
 135  
      * Returns whether or not there is another item in the collection.</p>
 136  
      * <p>
 137  
      * <b>This method is not thread safe.</b></p>
 138  
      *
 139  
      * @return true or false depending on whether or not another item exists in the collection
 140  
      */
 141  
     @Override
 142  
     public boolean hasNext() {
 143  10
         return iterableContent.hasNext();
 144  
     }
 145  
 
 146  
     /**
 147  
      * <p>
 148  
      * Returns the next item in the collection.</p>
 149  
      * <p>
 150  
      * <b>This method is not thread safe.</b></p>
 151  
      *
 152  
      * @return the next NvdCveInfo item in the collection
 153  
      */
 154  
     @Override
 155  
     public NvdCveInfo next() {
 156  7
         return iterableContent.next().getValue();
 157  
     }
 158  
 
 159  
     /**
 160  
      * <p>
 161  
      * Removes the current NvdCveInfo object from the collection.</p>
 162  
      * <p>
 163  
      * <b>This method is not thread safe.</b></p>
 164  
      */
 165  
     @Override
 166  
     public void remove() {
 167  1
         iterableContent.remove();
 168  1
     }
 169  
 
 170  
     /**
 171  
      * Returns the specified item from the collection.
 172  
      *
 173  
      * @param key the key to lookup the return value
 174  
      * @return the NvdCveInfo object stored using the specified key
 175  
      */
 176  
     public NvdCveInfo get(String key) {
 177  2
         return collection.get(key);
 178  
     }
 179  
 
 180  
     @Override
 181  
     public String toString() {
 182  0
         return "Updateable{" + "size=" + collection.size() + '}';
 183  
     }
 184  
 }