Skip to content

Commit

Permalink
Enable following redirects. Improve error logging slightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 25, 2024
1 parent 0e2745d commit 35dd971
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -21,6 +22,8 @@
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Function;

Expand All @@ -37,11 +40,19 @@ public static List<Library> resolveLibraries(List<URI> repositoryUrls, Collectio
var result = collector.libraries.stream().map(future -> {
try {
return future.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException re) {
throw re;
} else if (e.getCause() instanceof IOException re) {
throw new UncheckedIOException(re);
} else {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}).toList();
LOGGER.info("Collected %d libraries".formatted(result.size()));
LOGGER.info("Collected {} libraries", result.size());
return result;
}

Expand All @@ -63,7 +74,9 @@ public static List<Library> resolveLibraries(List<URI> repositoryUrls, Collectio

private final List<Future<Library>> libraries = new ArrayList<>();

private final HttpClient httpClient = HttpClient.newBuilder().build();
private final HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build();

private LibraryCollector(List<URI> repoUrl) {
this.repositoryUrls = new ArrayList<>(repoUrl);
Expand All @@ -86,7 +99,7 @@ private LibraryCollector(List<URI> repoUrl) {

LOGGER.info("Collecting libraries from:");
for (var repo : repositoryUrls) {
LOGGER.info(" - " + repo);
LOGGER.info(" - {}", repo);
}
}

Expand All @@ -109,15 +122,15 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
.thenApply(response -> {
if (response.statusCode() != 200) {
LOGGER.info(" Got %d for %s".formatted(response.statusCode(), artifactUri));
LOGGER.info(" Got {} for {}", response.statusCode(), artifactUri);
String message = "Could not find %s: %d".formatted(artifactUri, response.statusCode());
// Prepend error message from previous repo if they all fail
if (previousError != null) {
message = previousError + "\n" + message;
}
throw new RuntimeException(message);
}
LOGGER.info(" Found %s -> %s".formatted(name, artifactUri));
LOGGER.info(" Found {} -> {}", name, artifactUri);
return new Library(
name,
new LibraryDownload(new LibraryArtifact(
Expand All @@ -132,6 +145,9 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
libraryFuture = makeRequest.apply(null);
} else {
libraryFuture = libraryFuture.exceptionallyCompose(error -> {
if (error instanceof CompletionException e) {
return makeRequest.apply(e.getCause().getMessage());
}
return makeRequest.apply(error.getMessage());
});
}
Expand Down

0 comments on commit 35dd971

Please sign in to comment.