diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java index f22ef5f8604..e2aa807856a 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java @@ -141,7 +141,8 @@ byte[] sendRequest(HttpSender.Request request) throws Throwable { return Failsafe.with(retryPolicy).get(() -> { try (HttpSender.Response response = httpSender.send(request)) { if (!response.isSuccessful()) { - throw new HttpSenderResponseException(null, response.getCode()); + throw new HttpSenderResponseException(null, response.getCode(), + new String(response.getBodyAsBytes())); } return response.getBodyAsBytes(); } @@ -733,13 +734,17 @@ public MavenRepository normalizeRepository(MavenRepository originalRepository, M sendRequest(request.build()); normalized = repository.withUri(httpsUri).withKnownToExist(true); } catch (Throwable t) { - ctx.getResolutionListener().repositoryAccessFailed(httpsUri, t); if (t instanceof HttpSenderResponseException) { HttpSenderResponseException e = (HttpSenderResponseException) t; // response was returned from the server, but it was not a 200 OK. The server therefore exists. if (e.getResponseCode() != null) { normalized = repository.withUri(httpsUri); } + if (!"Directory listing forbidden".equals(e.getBody())) { + ctx.getResolutionListener().repositoryAccessFailed(httpsUri, t); + } + } else { + ctx.getResolutionListener().repositoryAccessFailed(httpsUri, t); } if (normalized == null) { if (!httpsUri.equals(originalUrl)) { @@ -857,9 +862,13 @@ public static class HttpSenderResponseException extends Exception { @Nullable private final Integer responseCode; - public HttpSenderResponseException(@Nullable Throwable cause, @Nullable Integer responseCode) { + private final String body; + + public HttpSenderResponseException(@Nullable Throwable cause, @Nullable Integer responseCode, + String body) { super(cause); this.responseCode = responseCode; + this.body = body; } /** diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java index 29466213242..3fdb4a97303 100755 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/MavenPomDownloaderTest.java @@ -45,6 +45,7 @@ import java.util.function.Consumer; import static java.util.Collections.*; +import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -74,6 +75,16 @@ public MockResponse dispatch(RecordedRequest recordedRequest) { } } + @Test + void ossSonatype() { + InMemoryExecutionContext ctx = new InMemoryExecutionContext(); + MavenRepository ossSonatype = new MavenRepository("oss", "https://oss.sonatype.org/content/repositories/snapshots", + null, "true", false, null, null, null); + MavenRepository repo = new MavenPomDownloader(ctx).normalizeRepository(ossSonatype, + MavenExecutionContextView.view(ctx), null); + assertThat(requireNonNull(repo).getUri()).isEqualTo(ossSonatype.getUri()); + } + @Issue("https://github.com/openrewrite/rewrite/issues/3908") @Test void centralIdOverridesDefaultRepository() {