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