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