##### Coverage Report - org.owasp.dependencycheck.data.update.Updateable

Classes in this File | Line Coverage | Branch Coverage | Complexity
Updateable |
93% | 27/29
100% | 4/4
| 1.25;1.25

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