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) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.cpe;
19  
20  import java.io.Serializable;
21  import java.io.UnsupportedEncodingException;
22  import java.net.URLDecoder;
23  
24  /**
25   * A CPE entry containing the name, vendor, product, and version.
26   *
27   * @author Jeremy Long
28   */
29  public class IndexEntry implements Serializable {
30  
31      /**
32       * the serial version uid.
33       */
34      static final long serialVersionUID = 8011924485946326934L;
35      /**
36       * The vendor name.
37       */
38      private String vendor;
39      /**
40       * The documentId.
41       */
42      private String documentId;
43  
44      /**
45       * Get the value of documentId.
46       *
47       * @return the value of documentId
48       */
49      public String getDocumentId() {
50          if (documentId == null && vendor != null && product != null) {
51              documentId = vendor + ':' + product;
52          }
53          return documentId;
54      }
55  
56      /**
57       * Set the value of documentId.
58       *
59       * @param documentId new value of documentId
60       */
61      public void setDocumentId(String documentId) {
62          this.documentId = documentId;
63      }
64  
65      /**
66       * Get the value of vendor.
67       *
68       * @return the value of vendor
69       */
70      public String getVendor() {
71          return vendor;
72      }
73  
74      /**
75       * Set the value of vendor.
76       *
77       * @param vendor new value of vendor
78       */
79      public void setVendor(String vendor) {
80          this.vendor = vendor;
81      }
82      /**
83       * The product name.
84       */
85      private String product;
86  
87      /**
88       * Get the value of product.
89       *
90       * @return the value of product
91       */
92      public String getProduct() {
93          return product;
94      }
95  
96      /**
97       * Set the value of product.
98       *
99       * @param product new value of product
100      */
101     public void setProduct(String product) {
102         this.product = product;
103     }
104     /**
105      * The search score.
106      */
107     private float searchScore;
108 
109     /**
110      * Get the value of searchScore.
111      *
112      * @return the value of searchScore
113      */
114     public float getSearchScore() {
115         return searchScore;
116     }
117 
118     /**
119      * Set the value of searchScore.
120      *
121      * @param searchScore new value of searchScore
122      */
123     public void setSearchScore(float searchScore) {
124         this.searchScore = searchScore;
125     }
126 
127     /**
128      * <p>
129      * Parses a name attribute value, from the cpe.xml, into its corresponding parts: vendor, product.</p>
130      * <p>
131      * Example:</p>
132      * <code>nbsp;nbsp;nbsp;cpe:/a:apache:struts:1.1:rc2</code>
133      *
134      * <p>
135      * Results in:</p> <ul> <li>Vendor: apache</li> <li>Product: struts</li>
136      * </ul>
137      * <p>
138      * If it is necessary to parse the CPE into more parts (i.e. to include version and revision) then you should use
139      * the {@link org.owasp.dependencycheck.dependency.VulnerableSoftware#parseName VulnerableSoftware.parseName()}.
140      *
141      * @param cpeName the cpe name
142      * @throws UnsupportedEncodingException should never be thrown...
143      */
144     public void parseName(String cpeName) throws UnsupportedEncodingException {
145         if (cpeName != null && cpeName.length() > 7) {
146             final String[] data = cpeName.substring(7).split(":");
147             if (data.length >= 1) {
148                 vendor = URLDecoder.decode(data[0].replace("+", "%2B"), "UTF-8");
149                 if (data.length >= 2) {
150                     product = URLDecoder.decode(data[1].replace("+", "%2B"), "UTF-8");
151                 }
152             }
153         }
154     }
155 
156     @Override
157     public int hashCode() {
158         int hash = 7;
159         hash = 97 * hash + (this.getDocumentId() != null ? this.getDocumentId().hashCode() : 0);
160         return hash;
161     }
162 
163     @Override
164     public boolean equals(Object obj) {
165         if (obj == null) {
166             return false;
167         }
168         if (getClass() != obj.getClass()) {
169             return false;
170         }
171         final IndexEntry other = (IndexEntry) obj;
172         if ((this.vendor == null) ? (other.vendor != null) : !this.vendor.equals(other.vendor)) {
173             return false;
174         }
175         if ((this.product == null) ? (other.product != null) : !this.product.equals(other.product)) {
176             return false;
177         }
178         return true;
179     }
180 
181     /**
182      * Standard implementation of toString showing vendor and product.
183      *
184      * @return the string representation of the object
185      */
186     @Override
187     public String toString() {
188         return "IndexEntry{" + "vendor=" + vendor + ", product=" + product + '}';
189     }
190 }