|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javadocs; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.URI; |
| 11 | +import java.net.http.HttpClient; |
| 12 | +import java.net.http.HttpRequest; |
| 13 | +import java.net.http.HttpResponse; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.logging.Level; |
| 20 | +import java.util.logging.Logger; |
| 21 | + |
| 22 | +/** |
| 23 | + * The javadoc.io site relies on someone accessing the page for an artifact version in order to |
| 24 | + * update the contents of the site. This will query Maven Central for all artifacts under |
| 25 | + * io.opentelemetry in order to identify the latest versions. Then it will crawl the associated |
| 26 | + * pages on the javadoc.io site to trigger updates. |
| 27 | + */ |
| 28 | +public final class JavaDocsCrawler { |
| 29 | + private static final String GROUP = "io.opentelemetry"; |
| 30 | + private static final String MAVEN_CENTRAL_BASE_URL = |
| 31 | + "https://search.maven.org/solrsearch/select?q=g:"; |
| 32 | + private static final String JAVA_DOCS_BASE_URL = "https://javadoc.io/doc/"; |
| 33 | + private static final int PAGE_SIZE = 20; |
| 34 | + private static final int THROTTLE_MS = 500; |
| 35 | + |
| 36 | + // visible for testing |
| 37 | + static final String JAVA_DOC_DOWNLOADED_TEXT = "Javadoc is being downloaded"; |
| 38 | + |
| 39 | + private static final Logger logger = Logger.getLogger(JavaDocsCrawler.class.getName()); |
| 40 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 41 | + |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + HttpClient client = HttpClient.newHttpClient(); |
| 44 | + List<Artifact> artifacts = getArtifacts(client); |
| 45 | + if (artifacts.isEmpty()) { |
| 46 | + logger.log(Level.SEVERE, "No artifacts found"); |
| 47 | + return; |
| 48 | + } |
| 49 | + logger.info(String.format(Locale.ROOT, "Found %d artifacts", artifacts.size())); |
| 50 | + |
| 51 | + List<String> updated = crawlJavaDocs(client, artifacts); |
| 52 | + if (updated.isEmpty()) { |
| 53 | + logger.info("No updates were needed"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + logger.info("Artifacts that triggered updates:\n" + String.join("\n", updated)); |
| 58 | + } |
| 59 | + |
| 60 | + static List<Artifact> getArtifacts(HttpClient client) throws IOException, InterruptedException { |
| 61 | + int start = 0; |
| 62 | + Integer numFound; |
| 63 | + List<Artifact> result = new ArrayList<>(); |
| 64 | + |
| 65 | + do { |
| 66 | + if (start != 0) { |
| 67 | + Thread.sleep(THROTTLE_MS); // try not to DDoS the site, it gets knocked over easily |
| 68 | + } |
| 69 | + |
| 70 | + Map<?, ?> map = queryMavenCentral(client, start); |
| 71 | + |
| 72 | + numFound = |
| 73 | + Optional.ofNullable(map) |
| 74 | + .map(mavenResult -> (Map<?, ?>) mavenResult.get("response")) |
| 75 | + .map(response -> (Integer) response.get("numFound")) |
| 76 | + .orElse(null); |
| 77 | + |
| 78 | + List<Artifact> artifacts = convertToArtifacts(map); |
| 79 | + result.addAll(artifacts); |
| 80 | + |
| 81 | + start += PAGE_SIZE; |
| 82 | + } while (numFound != null && start < numFound); |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + private static List<Artifact> convertToArtifacts(Map<?, ?> map) { |
| 88 | + return Optional.ofNullable(map) |
| 89 | + .map(mavenResults -> (Map<?, ?>) mavenResults.get("response")) |
| 90 | + .map(response -> (List<?>) response.get("docs")) |
| 91 | + .map( |
| 92 | + docs -> { |
| 93 | + List<Artifact> artifacts = new ArrayList<>(); |
| 94 | + for (Object doc : docs) { |
| 95 | + Map<?, ?> docMap = (Map<?, ?>) doc; |
| 96 | + String artifact = (String) docMap.get("a"); |
| 97 | + String version = (String) docMap.get("latestVersion"); |
| 98 | + if (artifact != null && version != null) { |
| 99 | + artifacts.add(new Artifact(artifact, version)); |
| 100 | + } |
| 101 | + } |
| 102 | + return artifacts; |
| 103 | + }) |
| 104 | + .orElseGet(ArrayList::new); |
| 105 | + } |
| 106 | + |
| 107 | + private static Map<?, ?> queryMavenCentral(HttpClient client, int start) |
| 108 | + throws IOException, InterruptedException { |
| 109 | + URI uri = |
| 110 | + URI.create( |
| 111 | + String.format( |
| 112 | + Locale.ROOT, |
| 113 | + "%s%s&rows=%d&start=%d&wt=json", |
| 114 | + MAVEN_CENTRAL_BASE_URL, |
| 115 | + GROUP, |
| 116 | + PAGE_SIZE, |
| 117 | + start)); |
| 118 | + |
| 119 | + HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
| 120 | + |
| 121 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 122 | + if (response.statusCode() != 200) { |
| 123 | + logger.log( |
| 124 | + Level.SEVERE, |
| 125 | + "Unexpected response code: " + response.statusCode() + ": " + response.body()); |
| 126 | + throw new IOException("Unable to pull Maven central artifacts list"); |
| 127 | + } |
| 128 | + return objectMapper.readValue(response.body(), Map.class); |
| 129 | + } |
| 130 | + |
| 131 | + static List<String> crawlJavaDocs(HttpClient client, List<Artifact> artifacts) |
| 132 | + throws IOException, InterruptedException { |
| 133 | + List<String> updatedArtifacts = new ArrayList<>(); |
| 134 | + |
| 135 | + for (Artifact artifact : artifacts) { |
| 136 | + String[] parts = artifact.getName().split("-"); |
| 137 | + StringBuilder path = new StringBuilder(); |
| 138 | + path.append(JAVA_DOCS_BASE_URL) |
| 139 | + .append(GROUP) |
| 140 | + .append("/") |
| 141 | + .append(artifact.getName()) |
| 142 | + .append("/") |
| 143 | + .append(artifact.getVersion()) |
| 144 | + .append("/") |
| 145 | + .append(String.join("/", parts)) |
| 146 | + .append("/package-summary.html"); |
| 147 | + |
| 148 | + HttpRequest crawlRequest = HttpRequest.newBuilder(URI.create(path.toString())).GET().build(); |
| 149 | + HttpResponse<String> crawlResponse = |
| 150 | + client.send(crawlRequest, HttpResponse.BodyHandlers.ofString()); |
| 151 | + |
| 152 | + // gets a status code 303 when version exists and the site redirects it to use /latest/ |
| 153 | + if (crawlResponse.statusCode() != 200 && crawlResponse.statusCode() != 303) { |
| 154 | + logger.log( |
| 155 | + Level.WARNING, |
| 156 | + String.format( |
| 157 | + Locale.ROOT, |
| 158 | + "Crawl failed for %s with status code %d at URL %s\nResponse: %s", |
| 159 | + artifact.getName(), |
| 160 | + crawlResponse.statusCode(), |
| 161 | + path, |
| 162 | + crawlResponse.body())); |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + if (crawlResponse.body().contains(JAVA_DOC_DOWNLOADED_TEXT)) { |
| 167 | + updatedArtifacts.add(artifact.getName()); |
| 168 | + } |
| 169 | + |
| 170 | + Thread.sleep(THROTTLE_MS); // some light throttling |
| 171 | + } |
| 172 | + return updatedArtifacts; |
| 173 | + } |
| 174 | + |
| 175 | + private JavaDocsCrawler() {} |
| 176 | +} |
0 commit comments