5
5
import java .math .BigDecimal ;
6
6
import java .nio .file .Files ;
7
7
import java .nio .file .StandardOpenOption ;
8
+ import java .util .Collection ;
8
9
import java .util .HashSet ;
9
10
import java .util .List ;
10
11
import java .util .Objects ;
18
19
import com .devonfw .tools .ide .version .VersionIdentifier ;
19
20
import com .devonfw .tools .ide .version .VersionRange ;
20
21
import com .fasterxml .jackson .core .JsonProcessingException ;
22
+ import com .fasterxml .jackson .core .type .TypeReference ;
21
23
import com .fasterxml .jackson .databind .ObjectMapper ;
22
24
23
25
/**
@@ -28,7 +30,7 @@ public class UrlSecurityJsonFile extends AbstractUrlFile<UrlEdition> {
28
30
/** {@link #getName() Name} of security json file. */
29
31
public static final String FILENAME_SECURITY = "security.json" ;
30
32
31
- private UrlSecurityWarningsJson urlSecurityWarningsJson = new UrlSecurityWarningsJson () ;
33
+ private Collection < UrlSecurityWarning > urlSecurityWarnings ;
32
34
33
35
/**
34
36
* The constructor.
@@ -38,17 +40,19 @@ public class UrlSecurityJsonFile extends AbstractUrlFile<UrlEdition> {
38
40
public UrlSecurityJsonFile (UrlEdition parent ) {
39
41
40
42
super (parent , FILENAME_SECURITY );
43
+ this .urlSecurityWarnings = new HashSet <>();
41
44
}
42
45
43
46
/**
44
47
* A wrapper for {@link #addSecurityWarning(VersionRange, BigDecimal, String, String, String)} used in the unit tests.
48
+ *
49
+ * @param versionRange the {@link VersionRange}.
45
50
*/
46
- public boolean addSecurityWarning (VersionRange versionRange ) {
51
+ public void addSecurityWarning (VersionRange versionRange ) {
47
52
48
53
UrlSecurityWarning newWarning = new UrlSecurityWarning (versionRange , null , null , null , null );
49
- boolean added = this . urlSecurityWarningsJson . getWarnings () .add (newWarning );
54
+ boolean added = urlSecurityWarnings .add (newWarning );
50
55
this .modified = this .modified || added ;
51
- return added ;
52
56
}
53
57
54
58
/**
@@ -65,7 +69,7 @@ public boolean addSecurityWarning(VersionRange versionRange, BigDecimal severity
65
69
String nistUrl ) {
66
70
67
71
UrlSecurityWarning newWarning = new UrlSecurityWarning (versionRange , severity , cveName , description , nistUrl );
68
- boolean added = this . urlSecurityWarningsJson . getWarnings () .add (newWarning );
72
+ boolean added = urlSecurityWarnings .add (newWarning );
69
73
this .modified = this .modified || added ;
70
74
return added ;
71
75
}
@@ -93,7 +97,7 @@ public boolean contains(VersionIdentifier version, boolean ignoreWarningsThatAff
93
97
edition .getName ());
94
98
}
95
99
96
- for (UrlSecurityWarning warning : this .urlSecurityWarningsJson . getWarnings () ) {
100
+ for (UrlSecurityWarning warning : this .urlSecurityWarnings ) {
97
101
VersionRange versionRange = warning .getVersionRange ();
98
102
if (ignoreWarningsThatAffectAllVersions ) {
99
103
boolean includesOldestVersion = versionRange .getMin () == null
@@ -114,6 +118,9 @@ public boolean contains(VersionIdentifier version, boolean ignoreWarningsThatAff
114
118
/**
115
119
* For a given version, returns whether there is a security warning in the {@link UrlSecurityWarningsJson JSON
116
120
* object}. This method does not ignore warnings that affect all versions.
121
+ *
122
+ * @param version the {@link VersionIdentifier}.
123
+ * @return {@code true} if there is a security risk for the given version, {@code false} otherwise.
117
124
*/
118
125
public boolean contains (VersionIdentifier version ) {
119
126
@@ -129,7 +136,7 @@ public boolean contains(VersionIdentifier version) {
129
136
public Set <UrlSecurityWarning > getMatchingSecurityWarnings (VersionIdentifier version ) {
130
137
131
138
Set <UrlSecurityWarning > matchedWarnings = new HashSet <>();
132
- for (UrlSecurityWarning warning : this .urlSecurityWarningsJson . getWarnings () ) {
139
+ for (UrlSecurityWarning warning : this .urlSecurityWarnings ) {
133
140
if (warning .getVersionRange ().contains (version )) {
134
141
matchedWarnings .add (warning );
135
142
}
@@ -140,7 +147,7 @@ public Set<UrlSecurityWarning> getMatchingSecurityWarnings(VersionIdentifier ver
140
147
/** Clears all security warnings. */
141
148
public void clearSecurityWarnings () {
142
149
143
- this .urlSecurityWarningsJson . getWarnings () .clear ();
150
+ this .urlSecurityWarnings .clear ();
144
151
this .modified = true ;
145
152
}
146
153
@@ -152,7 +159,8 @@ protected void doLoad() {
152
159
}
153
160
ObjectMapper mapper = JsonMapping .create ();
154
161
try {
155
- this .urlSecurityWarningsJson = mapper .readValue (getPath ().toFile (), UrlSecurityWarningsJson .class );
162
+ urlSecurityWarnings = mapper .readValue (getPath ().toFile (), new TypeReference <Set <UrlSecurityWarning >>() {
163
+ });
156
164
} catch (IOException e ) {
157
165
throw new IllegalStateException ("Failed to load the UrlSecurityJsonFile " + getPath (), e );
158
166
}
@@ -163,13 +171,13 @@ protected void doSave() {
163
171
164
172
ObjectMapper mapper = JsonMapping .create ();
165
173
166
- if (this .urlSecurityWarningsJson . getWarnings () .isEmpty () && !Files .exists (getPath ())) {
174
+ if (this .urlSecurityWarnings .isEmpty () && !Files .exists (getPath ())) {
167
175
return ;
168
176
}
169
177
170
178
String jsonString ;
171
179
try {
172
- jsonString = mapper .writeValueAsString (this . urlSecurityWarningsJson );
180
+ jsonString = mapper .writeValueAsString (urlSecurityWarnings );
173
181
} catch (JsonProcessingException e ) {
174
182
throw new RuntimeException (e );
175
183
}
@@ -182,8 +190,11 @@ protected void doSave() {
182
190
}
183
191
}
184
192
185
- public UrlSecurityWarningsJson getUrlSecurityWarningsJson () {
193
+ /**
194
+ * @return Collection of {@link UrlSecurityWarning}.
195
+ */
196
+ public Collection <UrlSecurityWarning > getUrlSecurityWarnings () {
186
197
187
- return this .urlSecurityWarningsJson ;
198
+ return this .urlSecurityWarnings ;
188
199
}
189
200
}
0 commit comments