View Javadoc
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) 2018 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
34   */
35  @ThreadSafe
36  public final class CweDB {
37  
38      /**
39       * The Logger.
40       */
41      private static final Logger LOGGER = LoggerFactory.getLogger(CweDB.class);
42      /**
43       * A HashMap of the CWE data.
44       */
45      private static final Map<String, String> CWE = loadData();
46  
47      /**
48       * Empty private constructor as this is a utility class.
49       */
50      private CweDB() {
51          //empty
52      }
53  
54      /**
55       * Loads a HashMap containing the CWE data from a resource found in the jar.
56       *
57       * @return a HashMap of CWE data
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       * <p>
77       * Returns the full CWE name from the CWE ID.</p>
78       *
79       * @param cweId the CWE ID
80       * @return the full name of the CWE
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       * <p>
91       * Returns the full CWE name from the CWE ID.</p>
92       *
93       * @param cweId the CWE ID
94       * @return the full name of the CWE
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 }