Skip to content

Commit

Permalink
Normalization of maven repositories that don't permit directory listi…
Browse files Browse the repository at this point in the history
…ng (#3961)
  • Loading branch information
jkschneider authored Feb 1, 2024
1 parent 1124e3d commit 53f421e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 53f421e

Please sign in to comment.