9
9
import static org .hibernate .search .build .enforcer .MavenProjectUtils .isProjectDeploySkipped ;
10
10
11
11
import java .io .IOException ;
12
- import java .io .InputStreamReader ;
13
12
import java .net .URI ;
14
13
import java .net .URLEncoder ;
15
14
import java .net .http .HttpClient ;
33
32
34
33
import com .google .gson .Gson ;
35
34
import com .google .gson .GsonBuilder ;
36
- import com .google .gson .stream .JsonReader ;
37
35
38
36
import org .apache .maven .enforcer .rule .api .AbstractEnforcerRule ;
39
37
import org .apache .maven .enforcer .rule .api .EnforcerRuleException ;
@@ -47,8 +45,9 @@ public class DependencyManagementIncludesAllGroupIdArtifactsRule extends Abstrac
47
45
* See <a href="https://central.sonatype.org/search/rest-api-guide/">Maven Central REST API</a>
48
46
*/
49
47
private static final String BASE_URL_FORMAT =
50
- "https://search.maven.org/solrsearch/select?q=%s&core=gav&start=%d&rows=%d&wt=json" ;
51
- private static final String CENTRAL_SEARCH_URL = "https://central.sonatype.com/api/internal/browse/components" ;
48
+ "https://central.sonatype.com/solrsearch/select?q=%s&core=gav&start=%d&rows=%d&wt=json" ;
49
+ // This one here is more for a test purpose rather than to be used in "prod":
50
+ private static final String CENTRAL_SEARCH_INTERNAL_URL = "https://central.sonatype.com/api/internal/browse/components" ;
52
51
private static final String MAVEN_STATUS_URL = "https://status.maven.org/api/v2/summary.json" ;
53
52
private static final int ROWS_PER_PAGE = 100 ;
54
53
private static final int MAX_RETRIES = 5 ;
@@ -129,7 +128,7 @@ public void execute() throws EnforcerRuleException {
129
128
130
129
if ( mavenStatus .isSearchAvailable () ) {
131
130
for ( Artifact filter : toQuery ) {
132
- mavenCentralSearch ( filter , gson , foundArtifacts );
131
+ mavenCentralSolrSearch ( filter , gson , foundArtifacts );
133
132
}
134
133
}
135
134
else {
@@ -153,12 +152,13 @@ public void execute() throws EnforcerRuleException {
153
152
}
154
153
}
155
154
156
- private void mavenCentralSearch (Artifact filter , Gson gson , Set <Artifact > foundArtifacts ) throws EnforcerRuleException {
155
+ private void mavenCentralInternalSearch (Artifact filter , Gson gson , Set <Artifact > foundArtifacts )
156
+ throws EnforcerRuleException {
157
157
CentralSearchRequest request = new CentralSearchRequest ( filter );
158
158
getLog ().info ( "Fetching information for " + request );
159
159
do {
160
160
CentralSearchResponse response =
161
- post ( gson , client , CENTRAL_SEARCH_URL , request , CentralSearchResponse .class ,
161
+ post ( gson , client , CENTRAL_SEARCH_INTERNAL_URL , request , CentralSearchResponse .class ,
162
162
CentralSearchResponse ::empty );
163
163
foundArtifacts .addAll ( response .components .stream ().map ( Artifact ::new ).toList () );
164
164
if ( request .nextPage () >= response .pageCount ) {
@@ -168,7 +168,7 @@ private void mavenCentralSearch(Artifact filter, Gson gson, Set<Artifact> foundA
168
168
while ( true );
169
169
}
170
170
171
- private void legacyMavenSolrSearch (Artifact filter , Gson gson , Set <Artifact > foundArtifacts ) throws EnforcerRuleException {
171
+ private void mavenCentralSolrSearch (Artifact filter , Gson gson , Set <Artifact > foundArtifacts ) throws EnforcerRuleException {
172
172
StringBuilder queryBuilder = new StringBuilder ();
173
173
queryBuilder .append ( "g:" ).append ( encodeValue ( filter .g ) );
174
174
@@ -180,14 +180,16 @@ private void legacyMavenSolrSearch(Artifact filter, Gson gson, Set<Artifact> fou
180
180
}
181
181
182
182
int start = 0 ;
183
+ int collectedSoFar = 0 ;
183
184
do {
184
185
String url = String .format ( Locale .ROOT , BASE_URL_FORMAT , queryBuilder , start , ROWS_PER_PAGE );
185
186
SearchResponse response = fetch ( gson , url , SearchResponse .class , SearchResponse ::empty );
187
+ collectedSoFar += response .response .docs .size ();
186
188
foundArtifacts .addAll ( response .response .docs );
187
- if ( response .response .start + response .response .docs .size () >= response . response . numFound ) {
189
+ if ( collectedSoFar == response .response .numFound || response .response .docs .isEmpty () ) {
188
190
break ;
189
191
}
190
- start += ROWS_PER_PAGE ;
192
+ start ++ ;
191
193
}
192
194
while ( true );
193
195
}
@@ -208,13 +210,16 @@ private static boolean shouldSkip(String gav, Set<Pattern> skipPatterns) {
208
210
private <T > T fetch (Gson gson , String url , Class <T > klass , Supplier <T > empty ) throws EnforcerRuleException {
209
211
return withRetry (
210
212
() -> {
211
- try (
212
- var stream = URI .create ( url ).toURL ().openStream (); var isr = new InputStreamReader ( stream );
213
- var reader = new JsonReader ( isr )
214
- ) {
215
- getLog ().info ( "Fetching from " + url );
216
- return gson .fromJson ( reader , klass );
217
- }
213
+ HttpRequest request = HttpRequest .newBuilder ()
214
+ .uri ( URI .create ( url ) )
215
+ .header ( "Content-Type" , "application/json" )
216
+ .GET ()
217
+ .build ();
218
+
219
+ getLog ().info ( "Fetching from " + url );
220
+ HttpResponse <String > response = client .send ( request , HttpResponse .BodyHandlers .ofString () );
221
+
222
+ return gson .fromJson ( response .body (), klass );
218
223
}, empty
219
224
);
220
225
}
0 commit comments