6
6
import java .net .http .HttpClient ;
7
7
import java .net .http .HttpRequest ;
8
8
import java .net .http .HttpResponse ;
9
- import java .nio .file .Files ;
10
9
import java .security .MessageDigest ;
11
10
import java .util .Locale ;
12
11
import java .util .Optional ;
@@ -27,8 +26,12 @@ public RemoteChecksumCalculator(
27
26
ProjectBuildingRequest artifactBuildingRequest ,
28
27
ProjectBuildingRequest pluginBuildingRequest ) {
29
28
super (checksumAlgorithm );
30
- if (!(checksumAlgorithm .equals ("md5" ) || checksumAlgorithm .equals ("sha1" ) || checksumAlgorithm .equals ("sha256" ) || checksumAlgorithm .equals ("sha512" ))) {
31
- throw new IllegalArgumentException ("Invalid checksum algorithm maven central only supports md5, sha1, sha256 or sha512." );
29
+ if (!(checksumAlgorithm .equals ("md5" )
30
+ || checksumAlgorithm .equals ("sha1" )
31
+ || checksumAlgorithm .equals ("sha256" )
32
+ || checksumAlgorithm .equals ("sha512" ))) {
33
+ throw new IllegalArgumentException (
34
+ "Invalid checksum algorithm maven central only supports md5, sha1, sha256 or sha512." );
32
35
}
33
36
34
37
this .artifactBuildingRequest = artifactBuildingRequest ;
@@ -52,35 +55,45 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
52
55
.build ();
53
56
54
57
for (ArtifactRepository repository : buildingRequest .getRemoteRepositories ()) {
55
- String artifactUrl = repository .getUrl ().replaceAll ("/$" , "" ) + "/" + groupId + "/" + artifactId + "/" + version
56
- + "/" + filename ;
58
+ String artifactUrl = repository .getUrl ().replaceAll ("/$" , "" ) + "/" + groupId + "/" + artifactId + "/"
59
+ + version + "/" + filename ;
57
60
String checksumUrl = artifactUrl + "." + checksumAlgorithm ;
58
61
59
62
LOGGER .debug ("Checking: " + checksumUrl );
60
63
61
64
HttpRequest checksumRequest =
62
65
HttpRequest .newBuilder ().uri (URI .create (checksumUrl )).build ();
63
- HttpResponse <String > checksumResponse = client .send (checksumRequest , HttpResponse .BodyHandlers .ofString ());
66
+ HttpResponse <String > checksumResponse =
67
+ client .send (checksumRequest , HttpResponse .BodyHandlers .ofString ());
64
68
65
69
if (checksumResponse .statusCode () >= 200 && checksumResponse .statusCode () < 300 ) {
66
70
return Optional .of (checksumResponse .body ().strip ());
67
71
}
68
72
69
73
if (checksumResponse .statusCode () == 404 ) {
70
- HttpRequest artifactRequest = HttpRequest .newBuilder ().uri (URI .create (artifactUrl )).build ();
71
- HttpResponse <byte []> artifactResponse = client .send (artifactRequest , HttpResponse .BodyHandlers .ofByteArray ());
74
+ HttpRequest artifactRequest = HttpRequest .newBuilder ()
75
+ .uri (URI .create (artifactUrl ))
76
+ .build ();
77
+ HttpResponse <byte []> artifactResponse =
78
+ client .send (artifactRequest , HttpResponse .BodyHandlers .ofByteArray ());
72
79
73
80
if (artifactResponse .statusCode () < 200 || artifactResponse .statusCode () >= 300 ) {
74
81
continue ;
75
82
}
76
83
77
84
// Fallback to and verify downloaded artifact with sha1
78
- HttpRequest artifactVerificationRequest = HttpRequest .newBuilder ().uri (URI .create (artifactUrl + ".sha1" )).build ();
79
- HttpResponse <String > artifactVerificationResponse = client .send (artifactVerificationRequest , HttpResponse .BodyHandlers .ofString ());
80
-
81
- if (artifactVerificationResponse .statusCode () >= 200 && artifactVerificationResponse .statusCode () < 300 ) {
85
+ HttpRequest artifactVerificationRequest = HttpRequest .newBuilder ()
86
+ .uri (URI .create (artifactUrl + ".sha1" ))
87
+ .build ();
88
+ HttpResponse <String > artifactVerificationResponse =
89
+ client .send (artifactVerificationRequest , HttpResponse .BodyHandlers .ofString ());
90
+
91
+ if (artifactVerificationResponse .statusCode () >= 200
92
+ && artifactVerificationResponse .statusCode () < 300 ) {
82
93
MessageDigest verificationMessageDigest = MessageDigest .getInstance ("sha1" );
83
- String sha1 = baseEncoding .encode (verificationMessageDigest .digest (artifactResponse .body ())).toLowerCase (Locale .ROOT );
94
+ String sha1 = baseEncoding
95
+ .encode (verificationMessageDigest .digest (artifactResponse .body ()))
96
+ .toLowerCase (Locale .ROOT );
84
97
85
98
if (!sha1 .equals (artifactVerificationResponse .body ().strip ())) {
86
99
LOGGER .error ("Invalid sha1 checksum for download of: " + artifactUrl );
@@ -91,7 +104,9 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
91
104
}
92
105
93
106
MessageDigest messageDigest = MessageDigest .getInstance (checksumAlgorithm );
94
- String checksum = baseEncoding .encode (messageDigest .digest (artifactResponse .body ())).toLowerCase (Locale .ROOT );
107
+ String checksum = baseEncoding
108
+ .encode (messageDigest .digest (artifactResponse .body ()))
109
+ .toLowerCase (Locale .ROOT );
95
110
return Optional .of (checksum );
96
111
}
97
112
}
0 commit comments