Skip to content

Commit ba8d1a2

Browse files
committed
HSEARCH-5481 Use the new Maven Central search API
1 parent 71fb4dc commit ba8d1a2

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

build/enforcer/src/main/java/org/hibernate/search/build/enforcer/DependencyManagementIncludesAllGroupIdArtifactsRule.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.hibernate.search.build.enforcer.MavenProjectUtils.isProjectDeploySkipped;
1010

1111
import java.io.IOException;
12-
import java.io.InputStreamReader;
1312
import java.net.URI;
1413
import java.net.URLEncoder;
1514
import java.net.http.HttpClient;
@@ -33,7 +32,6 @@
3332

3433
import com.google.gson.Gson;
3534
import com.google.gson.GsonBuilder;
36-
import com.google.gson.stream.JsonReader;
3735

3836
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
3937
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
@@ -47,8 +45,9 @@ public class DependencyManagementIncludesAllGroupIdArtifactsRule extends Abstrac
4745
* See <a href="https://central.sonatype.org/search/rest-api-guide/">Maven Central REST API</a>
4846
*/
4947
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";
5251
private static final String MAVEN_STATUS_URL = "https://status.maven.org/api/v2/summary.json";
5352
private static final int ROWS_PER_PAGE = 100;
5453
private static final int MAX_RETRIES = 5;
@@ -129,7 +128,7 @@ public void execute() throws EnforcerRuleException {
129128

130129
if ( mavenStatus.isSearchAvailable() ) {
131130
for ( Artifact filter : toQuery ) {
132-
mavenCentralSearch( filter, gson, foundArtifacts );
131+
mavenCentralSolrSearch( filter, gson, foundArtifacts );
133132
}
134133
}
135134
else {
@@ -153,12 +152,13 @@ public void execute() throws EnforcerRuleException {
153152
}
154153
}
155154

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 {
157157
CentralSearchRequest request = new CentralSearchRequest( filter );
158158
getLog().info( "Fetching information for " + request );
159159
do {
160160
CentralSearchResponse response =
161-
post( gson, client, CENTRAL_SEARCH_URL, request, CentralSearchResponse.class,
161+
post( gson, client, CENTRAL_SEARCH_INTERNAL_URL, request, CentralSearchResponse.class,
162162
CentralSearchResponse::empty );
163163
foundArtifacts.addAll( response.components.stream().map( Artifact::new ).toList() );
164164
if ( request.nextPage() >= response.pageCount ) {
@@ -168,7 +168,7 @@ private void mavenCentralSearch(Artifact filter, Gson gson, Set<Artifact> foundA
168168
while ( true );
169169
}
170170

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 {
172172
StringBuilder queryBuilder = new StringBuilder();
173173
queryBuilder.append( "g:" ).append( encodeValue( filter.g ) );
174174

@@ -180,14 +180,16 @@ private void legacyMavenSolrSearch(Artifact filter, Gson gson, Set<Artifact> fou
180180
}
181181

182182
int start = 0;
183+
int collectedSoFar = 0;
183184
do {
184185
String url = String.format( Locale.ROOT, BASE_URL_FORMAT, queryBuilder, start, ROWS_PER_PAGE );
185186
SearchResponse response = fetch( gson, url, SearchResponse.class, SearchResponse::empty );
187+
collectedSoFar += response.response.docs.size();
186188
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() ) {
188190
break;
189191
}
190-
start += ROWS_PER_PAGE;
192+
start++;
191193
}
192194
while ( true );
193195
}
@@ -208,13 +210,16 @@ private static boolean shouldSkip(String gav, Set<Pattern> skipPatterns) {
208210
private <T> T fetch(Gson gson, String url, Class<T> klass, Supplier<T> empty) throws EnforcerRuleException {
209211
return withRetry(
210212
() -> {
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 );
218223
}, empty
219224
);
220225
}

0 commit comments

Comments
 (0)