1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.owasp.dependencycheck.dependency.naming;
19  
20  import org.apache.commons.lang3.builder.CompareToBuilder;
21  import org.apache.commons.lang3.builder.EqualsBuilder;
22  import org.apache.commons.lang3.builder.HashCodeBuilder;
23  import org.jetbrains.annotations.NotNull;
24  import org.owasp.dependencycheck.dependency.Confidence;
25  import us.springett.parsers.cpe.Cpe;
26  import us.springett.parsers.cpe.CpeBuilder;
27  import us.springett.parsers.cpe.exceptions.CpeValidationException;
28  import us.springett.parsers.cpe.values.Part;
29  
30  
31  
32  
33  
34  
35  public class CpeIdentifier implements Identifier {
36  
37      
38  
39  
40      private static final long serialVersionUID = 2901855131887281680L;
41  
42      
43  
44  
45      private final Cpe cpe;
46      
47  
48  
49      private Confidence confidence;
50      
51  
52  
53      private String url;
54      
55  
56  
57  
58      private String notes;
59  
60      
61  
62  
63  
64  
65  
66  
67      public CpeIdentifier(Cpe cpe, Confidence confidence) {
68          this.cpe = cpe;
69          this.confidence = confidence;
70          this.url = null;
71      }
72  
73      
74  
75  
76  
77  
78  
79  
80  
81      public CpeIdentifier(Cpe cpe, String url, Confidence confidence) {
82          this.cpe = cpe;
83          this.confidence = confidence;
84          this.url = url;
85      }
86  
87      
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98      public CpeIdentifier(String vendor, String product, String version, Confidence confidence) throws CpeValidationException {
99          final CpeBuilder builder = new CpeBuilder();
100         this.cpe = builder.part(Part.APPLICATION).vendor(vendor).product(product).version(version).build();
101         this.confidence = confidence;
102     }
103 
104     
105 
106 
107 
108 
109     public Cpe getCpe() {
110         return cpe;
111     }
112 
113     @Override
114     public Confidence getConfidence() {
115         return confidence;
116     }
117 
118     @Override
119     public String getNotes() {
120         return notes;
121     }
122 
123     @Override
124     public String getUrl() {
125         return url;
126     }
127 
128     
129 
130 
131     @Override
132     public void setConfidence(Confidence confidence) {
133         this.confidence = confidence;
134     }
135 
136     
137 
138 
139     @Override
140     public void setUrl(String url) {
141         this.url = url;
142     }
143 
144     @Override
145     public void setNotes(String notes) {
146         this.notes = notes;
147     }
148 
149     @Override
150     public String getValue() {
151         return cpe.toCpe23FS();
152     }
153 
154     
155 
156 
157 
158 
159     @Override
160     public String toString() {
161         return cpe.toCpe23FS();
162     }
163 
164     @Override
165     public int hashCode() {
166         return new HashCodeBuilder(95, 183)
167                 .append(this.cpe)
168                 .append(this.confidence)
169                 .append(this.url)
170                 .append(this.notes)
171                 .toHashCode();
172     }
173 
174     @Override
175     public boolean equals(Object obj) {
176         if (obj == null || !(obj instanceof CpeIdentifier)) {
177             return false;
178         }
179         if (this == obj) {
180             return true;
181         }
182         final CpeIdentifier other = (CpeIdentifier) obj;
183         return new EqualsBuilder().append(cpe, other.cpe)
184                 .append(this.confidence, other.confidence)
185                 .append(this.url, other.url)
186                 .append(this.notes, other.notes).isEquals();
187     }
188 
189     @Override
190     public int compareTo(@NotNull Identifier o) {
191         if (o instanceof CpeIdentifier) {
192             final CpeIdentifier other = (CpeIdentifier) o;
193             return new CompareToBuilder()
194                     .append(this.cpe, other.cpe)
195                     .append(this.url, other.getUrl())
196                     .append(this.confidence, other.getConfidence())
197                     .toComparison();
198 
199         }
200         return new CompareToBuilder()
201                 .append(this.toString(), o.toString())
202                 .append(this.url, o.getUrl())
203                 .append(this.confidence, o.getConfidence())
204                 .toComparison();
205     }
206 }