@@ -127,7 +127,7 @@ protected HttpResponse<InputStream> doGetResponseAsStream(String url) {
127
127
/**
128
128
* Updates a tool version with the given arguments (OS independent).
129
129
*
130
- * @param urlVersion the {@link UrlVersion} instance to update .
130
+ * @param urlVersion the {@link UrlVersion} with the {@link UrlVersion#getName() version-number} to process .
131
131
* @param downloadUrl the URL of the download for the tool.
132
132
* @return true if the version was successfully updated, false otherwise.
133
133
*/
@@ -139,20 +139,35 @@ protected boolean doAddVersion(UrlVersion urlVersion, String downloadUrl) {
139
139
/**
140
140
* Updates a tool version with the given arguments.
141
141
*
142
- * @param urlVersion the {@link UrlVersion} instance to update .
142
+ * @param urlVersion the {@link UrlVersion} with the {@link UrlVersion#getName() version-number} to process .
143
143
* @param downloadUrl the URL of the download for the tool.
144
144
* @param os the {@link OperatingSystem} for the tool (can be null).
145
145
* @return true if the version was successfully updated, false otherwise.
146
146
*/
147
147
protected boolean doAddVersion (UrlVersion urlVersion , String downloadUrl , OperatingSystem os ) {
148
148
149
- return doAddVersion (urlVersion , downloadUrl , os , null , "" );
149
+ return doAddVersion (urlVersion , downloadUrl , os , null );
150
150
}
151
151
152
152
/**
153
153
* Updates a tool version with the given arguments.
154
154
*
155
- * @param urlVersion the UrlVersion instance to update.
155
+ * @param urlVersion the {@link UrlVersion} with the {@link UrlVersion#getName() version-number} to process.
156
+ * @param downloadUrl the URL of the download for the tool.
157
+ * @param os the {@link OperatingSystem} for the tool (can be null).
158
+ * @param architecture the optional {@link SystemArchitecture}.
159
+ * @return true if the version was successfully updated, false otherwise.
160
+ */
161
+ protected boolean doAddVersion (UrlVersion urlVersion , String downloadUrl , OperatingSystem os ,
162
+ SystemArchitecture architecture ) {
163
+
164
+ return doAddVersion (urlVersion , downloadUrl , os , architecture , "" );
165
+ }
166
+
167
+ /**
168
+ * Updates a tool version with the given arguments.
169
+ *
170
+ * @param urlVersion the {@link UrlVersion} with the {@link UrlVersion#getName() version-number} to process.
156
171
* @param url the URL of the download for the tool.
157
172
* @param os the optional {@link OperatingSystem}.
158
173
* @param architecture the optional {@link SystemArchitecture}.
@@ -172,11 +187,7 @@ protected boolean doAddVersion(UrlVersion urlVersion, String url, OperatingSyste
172
187
}
173
188
url = url .replace ("${edition}" , getEdition ());
174
189
175
- if (checksum .isEmpty ()) {
176
- return checkDownloadUrl (url , urlVersion , os , architecture );
177
- } else {
178
- return checkDownloadUrl (url , urlVersion , os , architecture , checksum );
179
- }
190
+ return checkDownloadUrl (url , urlVersion , os , architecture , checksum );
180
191
181
192
}
182
193
@@ -190,58 +201,62 @@ protected boolean isSuccess(HttpResponse<?> response) {
190
201
}
191
202
192
203
/**
193
- * Checks the download URL and takes the provided checksum into account instead of downloading the file and generating
194
- * the checksum from it
204
+ * Checks if the download file checksum is still valid
195
205
*
196
- * @param url the URL of the download to check.
197
- * @param urlVersion the {@link UrlVersion} where to store the collected information like status and checksum .
206
+ * @param url String of the URL to check
207
+ * @param urlVersion the {@link UrlVersion} with the {@link UrlVersion#getName() version-number} to process .
198
208
* @param os the {@link OperatingSystem}
199
209
* @param architecture the {@link SystemArchitecture}
200
- * @param checksum String of the checksum to use
201
- * @return {@code true} if the download was checked successfully, {@code false} otherwise.
210
+ * @param checksum String of the new checksum to check
211
+ * @param tool String of the tool
212
+ * @param version String of the version
213
+ * @return {@code true} if update of checksum was successful, {@code false} otherwise.
202
214
*/
203
- private boolean checkDownloadUrl (String url , UrlVersion urlVersion , OperatingSystem os ,
204
- SystemArchitecture architecture , String checksum ) {
215
+ private static boolean isChecksumStillValid (String url , UrlVersion urlVersion , OperatingSystem os ,
216
+ SystemArchitecture architecture , String checksum , String tool , String version ) {
205
217
206
- HttpResponse <?> response = doCheckDownloadViaHeadRequest (url );
207
- int statusCode = response .statusCode ();
208
- boolean success = isSuccess (response );
209
- String tool = getToolWithEdition ();
210
- String version = urlVersion .getName ();
211
- String contentType = response .headers ().firstValue ("content-type" ).orElse ("undefined" );
212
- if (success && contentType .startsWith ("text" )) {
213
- logger .error ("For tool {} and version {} the download has an invalid content type {} for URL {}" , tool , version ,
214
- contentType , url );
215
- success = false ;
216
- }
217
- if (success ) {
218
- UrlDownloadFile urlDownloadFile = urlVersion .getOrCreateUrls (os , architecture );
219
- UrlChecksum urlChecksum = urlVersion .getOrCreateChecksum (urlDownloadFile .getName ());
218
+ UrlDownloadFile urlDownloadFile = urlVersion .getOrCreateUrls (os , architecture );
219
+ UrlChecksum urlChecksum = urlVersion .getOrCreateChecksum (urlDownloadFile .getName ());
220
+ String oldChecksum = urlChecksum .getChecksum ();
221
+
222
+ if ((oldChecksum != null ) && !Objects .equal (oldChecksum , checksum )) {
223
+ logger .error ("For tool {} and version {} the mirror URL {} points to a different checksum {} but expected {}." ,
224
+ tool , version , url , checksum , oldChecksum );
225
+ return false ;
226
+ } else {
220
227
urlDownloadFile .addUrl (url );
221
228
urlChecksum .setChecksum (checksum );
222
- String oldChecksum = urlChecksum .getChecksum ();
223
-
224
- if ((oldChecksum != null ) && !Objects .equal (oldChecksum , checksum )) {
225
- logger .error ("For tool {} and version {} the mirror URL {} points to a different checksum {} but expected {}." ,
226
- tool , version , url , checksum , oldChecksum );
227
- success = false ;
228
- } else {
229
- urlDownloadFile .addUrl (url );
230
- urlChecksum .setChecksum (checksum );
231
- }
232
229
}
230
+ return true ;
231
+ }
233
232
234
- doUpdateStatusJson (success , statusCode , urlVersion , url , false );
233
+ /**
234
+ * Checks if the content type is valid (not of type text)
235
+ *
236
+ * @param url String of the url to check
237
+ * @param tool String of the tool name
238
+ * @param version String of the version
239
+ * @param response the {@link HttpResponse}.
240
+ * @return {@code true} if the content type is not of type text, {@code false} otherwise.
241
+ */
242
+ private boolean isValidDownload (String url , String tool , String version , HttpResponse <?> response ) {
235
243
236
- if (success ) {
237
- urlVersion .save ();
244
+ if (isSuccess (response )) {
245
+ String contentType = response .headers ().firstValue ("content-type" ).orElse ("undefined" );
246
+ if (contentType .startsWith ("text" )) {
247
+ logger .error ("For tool {} and version {} the download has an invalid content type {} for URL {}" , tool , version ,
248
+ contentType , url );
249
+ return false ;
250
+ }
251
+ } else {
252
+ return false ;
238
253
}
239
254
240
- return success ;
255
+ return true ;
241
256
}
242
257
243
258
/**
244
- * Checks the download URL by downloading the file and generating the checksum from it
259
+ * Checks the download URL by checksum or by downloading the file and generating the checksum from it
245
260
*
246
261
* @param url the URL of the download to check.
247
262
* @param urlVersion the {@link UrlVersion} where to store the collected information like status and checksum.
@@ -250,37 +265,30 @@ private boolean checkDownloadUrl(String url, UrlVersion urlVersion, OperatingSys
250
265
* @return {@code true} if the download was checked successfully, {@code false} otherwise.
251
266
*/
252
267
private boolean checkDownloadUrl (String url , UrlVersion urlVersion , OperatingSystem os ,
253
- SystemArchitecture architecture ) {
268
+ SystemArchitecture architecture , String checksum ) {
254
269
255
- HttpResponse <InputStream > response = doGetResponseAsStream (url );
270
+ HttpResponse <? > response = doCheckDownloadViaHeadRequest (url );
256
271
int statusCode = response .statusCode ();
257
- boolean success = isSuccess (response );
258
- String contentType = response .headers ().firstValue ("content-type" ).orElse ("undefined" );
259
272
String tool = getToolWithEdition ();
260
273
String version = urlVersion .getName ();
261
- if (success && contentType .startsWith ("text" )) {
262
- logger .error ("For tool {} and version {} the download has an invalid content type {} for URL {}" , tool , version ,
263
- contentType , url );
264
- success = false ;
265
- }
274
+
275
+ boolean success = isValidDownload (url , tool , version , response );
276
+
266
277
if (success ) {
267
- String checksum = doGenerateChecksum (response , url , version , contentType );
268
- UrlDownloadFile urlDownloadFile = urlVersion .getOrCreateUrls (os , architecture );
269
- UrlChecksum urlChecksum = urlVersion .getOrCreateChecksum (urlDownloadFile .getName ());
270
- String oldChecksum = urlChecksum .getChecksum ();
271
- if ((oldChecksum != null ) && !Objects .equal (oldChecksum , checksum )) {
272
- logger .error ("For tool {} and version {} the mirror URL {} points to a different checksum {} but expected {}." ,
273
- tool , version , url , checksum , oldChecksum );
274
- success = false ;
275
- } else {
276
- urlDownloadFile .addUrl (url );
277
- urlChecksum .setChecksum (checksum );
278
+ if (checksum == null || checksum .isEmpty ()) {
279
+ String contentType = response .headers ().firstValue ("content-type" ).orElse ("undefined" );
280
+ checksum = doGenerateChecksum (doGetResponseAsStream (url ), url , version , contentType );
278
281
}
282
+
283
+ success = isChecksumStillValid (url , urlVersion , os , architecture , checksum , tool , version );
279
284
}
280
- doUpdateStatusJson ( success , statusCode , urlVersion , url , false );
285
+
281
286
if (success ) {
282
287
urlVersion .save ();
283
288
}
289
+
290
+ doUpdateStatusJson (success , statusCode , urlVersion , url , false );
291
+
284
292
return success ;
285
293
}
286
294
@@ -333,7 +341,7 @@ protected HttpResponse<?> doCheckDownloadViaHeadRequest(String url) {
333
341
334
342
return this .client .send (request , HttpResponse .BodyHandlers .ofString ());
335
343
} catch (Exception e ) {
336
- logger .error ("Failed to preform HEAD request of URL {}" , url , e );
344
+ logger .error ("Failed to perform HEAD request of URL {}" , url , e );
337
345
return null ;
338
346
}
339
347
}
0 commit comments