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