1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.cwe;
19
20 import org.owasp.dependencycheck.utils.FileUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29 import javax.annotation.concurrent.ThreadSafe;
30
31
32
33
34
35 @ThreadSafe
36 public final class CweDB {
37
38
39
40
41 private static final Logger LOGGER = LoggerFactory.getLogger(CweDB.class);
42
43
44
45 private static final Map<String, String> CWE = loadData();
46
47
48
49
50 private CweDB() {
51
52 }
53
54
55
56
57
58
59 @SuppressWarnings("unchecked")
60 private static Map<String, String> loadData() {
61 final String filePath = "data/cwe.hashmap.serialized";
62 try (InputStream input = FileUtils.getResourceAsStream(filePath);
63 ObjectInputStream oin = new ObjectInputStream(input)) {
64 return (HashMap<String, String>) oin.readObject();
65 } catch (ClassNotFoundException ex) {
66 LOGGER.warn("Unable to load CWE data. This should not be an issue.");
67 LOGGER.debug("", ex);
68 } catch (IOException ex) {
69 LOGGER.warn("Unable to load CWE data due to an IO Error. This should not be an issue.");
70 LOGGER.debug("", ex);
71 }
72 return null;
73 }
74
75
76
77
78
79
80
81
82 public static synchronized String getName(String cweId) {
83 if (cweId != null) {
84 return CWE.get(cweId);
85 }
86 return null;
87 }
88
89
90
91
92
93
94
95
96 public static synchronized String getFullName(String cweId) {
97 final String name = getName(cweId);
98 if (name != null) {
99 return cweId + " " + name;
100 }
101 return cweId;
102 }
103 }