|
| 1 | +package org.codejive.jpm.util; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.net.URLEncoder; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 14 | +import org.apache.http.impl.client.HttpClients; |
| 15 | +import org.eclipse.aether.artifact.Artifact; |
| 16 | +import org.eclipse.aether.artifact.DefaultArtifact; |
| 17 | + |
| 18 | +public class SearchUtils { |
| 19 | + |
| 20 | + public static class SearchResult { |
| 21 | + public final List<? extends Artifact> artifacts; |
| 22 | + public final String query; |
| 23 | + public final int start; |
| 24 | + public final int count; |
| 25 | + public final int total; |
| 26 | + |
| 27 | + public SearchResult( |
| 28 | + List<? extends Artifact> artifacts, String query, int start, int count, int total) { |
| 29 | + this.artifacts = Collections.unmodifiableList(artifacts); |
| 30 | + this.query = query; |
| 31 | + this.start = start; |
| 32 | + this.count = count; |
| 33 | + this.total = total; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static SearchResult findArtifacts(String artifactPattern, int count) throws IOException { |
| 38 | + return select(artifactPattern, 0, count); |
| 39 | + } |
| 40 | + |
| 41 | + public static SearchResult findNextArtifacts(SearchResult prevResult) throws IOException { |
| 42 | + if (prevResult.start + prevResult.count >= prevResult.total) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + SearchResult result = |
| 46 | + select(prevResult.query, prevResult.start + prevResult.count, prevResult.count); |
| 47 | + return result.artifacts.isEmpty() ? null : result; |
| 48 | + } |
| 49 | + |
| 50 | + private static SearchResult select(String query, int start, int count) throws IOException { |
| 51 | + String[] parts = query.split(":", -1); |
| 52 | + String finalQuery; |
| 53 | + if (parts.length >= 3) { |
| 54 | + finalQuery = "g:%s AND a:%s".formatted(parts[0], parts[1]); |
| 55 | + } else if (parts.length == 2) { |
| 56 | + finalQuery = "%s AND %s".formatted(parts[0], parts[1]); |
| 57 | + } else { |
| 58 | + finalQuery = query; |
| 59 | + } |
| 60 | + String searchUrl = |
| 61 | + "https://search.maven.org/solrsearch/select?start=%d&rows=%d&q=%s" |
| 62 | + .formatted(start, count, URLEncoder.encode(finalQuery, "UTF-8")); |
| 63 | + if (parts.length >= 3) { |
| 64 | + searchUrl += "&core=gav"; |
| 65 | + } |
| 66 | + String agent = |
| 67 | + String.format( |
| 68 | + "jpm/%s (%s %s)", |
| 69 | + Version.get(), |
| 70 | + System.getProperty("os.name"), |
| 71 | + System.getProperty("os.arch")); |
| 72 | + try (CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(agent).build()) { |
| 73 | + HttpGet request = new HttpGet(searchUrl); |
| 74 | + try (CloseableHttpResponse response = httpClient.execute(request)) { |
| 75 | + Gson gson = new GsonBuilder().create(); |
| 76 | + InputStream ins = response.getEntity().getContent(); |
| 77 | + InputStreamReader rdr = new InputStreamReader(ins); |
| 78 | + MvnSearchResult result = gson.fromJson(rdr, MvnSearchResult.class); |
| 79 | + if (result.responseHeader.status != 0) { |
| 80 | + throw new IOException("Search failed"); |
| 81 | + } |
| 82 | + List<DefaultArtifact> artifacts = |
| 83 | + result.response.docs.stream() |
| 84 | + .filter( |
| 85 | + d -> |
| 86 | + parts.length != 2 |
| 87 | + || d.g.contains(parts[0]) |
| 88 | + && d.a.contains(parts[1])) |
| 89 | + .map( |
| 90 | + d -> |
| 91 | + new DefaultArtifact( |
| 92 | + d.g, |
| 93 | + d.a, |
| 94 | + "", |
| 95 | + d.v != null ? d.v : d.latestVersion)) |
| 96 | + .toList(); |
| 97 | + return new SearchResult(artifacts, query, start, count, result.response.numFound); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class MvnSearchResult { |
| 104 | + public MsrHeader responseHeader; |
| 105 | + public MsrResponse response; |
| 106 | +} |
| 107 | + |
| 108 | +class MsrHeader { |
| 109 | + public int status; |
| 110 | +} |
| 111 | + |
| 112 | +class MsrResponse { |
| 113 | + public List<MsrDoc> docs; |
| 114 | + public int numFound; |
| 115 | + public int start; |
| 116 | +} |
| 117 | + |
| 118 | +class MsrDoc { |
| 119 | + public String g; |
| 120 | + public String a; |
| 121 | + public String v; |
| 122 | + public String latestVersion; |
| 123 | +} |
0 commit comments