From e341fb0b14caaceb6cce2324caa2663d8eaae170 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Thu, 10 Oct 2024 12:08:02 +0200
Subject: [PATCH 1/2] Set default connection/read timeouts for
`MavenPomDownloader`
Connection: 10s
Read: 30s
---
.../maven/internal/MavenPomDownloader.java | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
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 fe4cd7d06de..2876e4f3b7f 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
@@ -259,9 +259,9 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
try {
String scheme = URI.create(repo.getUri()).getScheme();
String baseUri = repo.getUri() + (repo.getUri().endsWith("/") ? "" : "/") +
- requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
- gav.getArtifactId() + '/' +
- (gav.getVersion() == null ? "" : gav.getVersion() + '/');
+ requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
+ gav.getArtifactId() + '/' +
+ (gav.getVersion() == null ? "" : gav.getVersion() + '/');
if ("file".equals(scheme)) {
// A maven repository can be expressed as a URI with a file scheme
@@ -777,10 +777,6 @@ MavenRepository normalizeRepository(MavenRepository repository) throws Throwable
}
HttpSender.Request.Builder request = httpSender.options(httpsUri);
- if (repository.getTimeout() != null) {
- request = request.withConnectTimeout(repository.getTimeout())
- .withReadTimeout(repository.getTimeout());
- }
ReachabilityResult reachability = reachable(applyAuthenticationToRequest(repository, request));
if (reachability.isSuccess()) {
@@ -851,9 +847,7 @@ private ReachabilityResult reachable(HttpSender.Request.Builder request) {
*/
private byte[] requestAsAuthenticatedOrAnonymous(MavenRepository repo, String uriString) throws HttpSenderResponseException, IOException {
try {
- HttpSender.Request.Builder request = httpSender.get(uriString)
- .withConnectTimeout(repo.getTimeout())
- .withReadTimeout(repo.getTimeout());
+ HttpSender.Request.Builder request = httpSender.get(uriString);
return sendRequest(applyAuthenticationToRequest(repo, request).build());
} catch (HttpSenderResponseException e) {
if (hasCredentials(repo) && e.isClientSideException()) {
@@ -888,6 +882,8 @@ private MavenRepository applyAuthenticationToRepository(MavenRepository reposito
*/
private HttpSender.Request.Builder applyAuthenticationToRequest(MavenRepository repository, HttpSender.Request.Builder request) {
if (mavenSettings != null && mavenSettings.getServers() != null) {
+ request.withConnectTimeout(repository.getTimeout() == null ? Duration.ofSeconds(10) : repository.getTimeout());
+ request.withReadTimeout(repository.getTimeout() == null ? Duration.ofSeconds(30) : repository.getTimeout());
for (MavenSettings.Server server : mavenSettings.getServers().getServers()) {
if (server.getId().equals(repository.getId()) && server.getConfiguration() != null) {
MavenSettings.ServerConfiguration configuration = server.getConfiguration();
From d856f5b187ebbd2dc5a93414fb46911e645dbae7 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Thu, 10 Oct 2024 12:08:50 +0200
Subject: [PATCH 2/2] update method name
---
.../maven/internal/MavenPomDownloader.java | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
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 2876e4f3b7f..4f0a26f50bd 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
@@ -778,20 +778,20 @@ MavenRepository normalizeRepository(MavenRepository repository) throws Throwable
HttpSender.Request.Builder request = httpSender.options(httpsUri);
- ReachabilityResult reachability = reachable(applyAuthenticationToRequest(repository, request));
+ ReachabilityResult reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request));
if (reachability.isSuccess()) {
return repository.withUri(httpsUri);
}
- reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(httpsUri)));
+ reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(httpsUri)));
if (reachability.isReachable()) {
return repository.withUri(httpsUri);
}
if (!originalUrl.equals(httpsUri)) {
- reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.OPTIONS).url(originalUrl)));
+ reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.OPTIONS).url(originalUrl)));
if (reachability.isSuccess()) {
return repository.withUri(originalUrl);
}
- reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(originalUrl)));
+ reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(originalUrl)));
if (reachability.isReachable()) {
return repository.withUri(originalUrl);
}
@@ -848,7 +848,7 @@ private ReachabilityResult reachable(HttpSender.Request.Builder request) {
private byte[] requestAsAuthenticatedOrAnonymous(MavenRepository repo, String uriString) throws HttpSenderResponseException, IOException {
try {
HttpSender.Request.Builder request = httpSender.get(uriString);
- return sendRequest(applyAuthenticationToRequest(repo, request).build());
+ return sendRequest(applyAuthenticationAndTimeoutToRequest(repo, request).build());
} catch (HttpSenderResponseException e) {
if (hasCredentials(repo) && e.isClientSideException()) {
return retryRequestAnonymously(uriString, e);
@@ -880,7 +880,7 @@ private MavenRepository applyAuthenticationToRepository(MavenRepository reposito
/**
* Returns a request builder with Authorization header set if the provided repository specifies credentials
*/
- private HttpSender.Request.Builder applyAuthenticationToRequest(MavenRepository repository, HttpSender.Request.Builder request) {
+ private HttpSender.Request.Builder applyAuthenticationAndTimeoutToRequest(MavenRepository repository, HttpSender.Request.Builder request) {
if (mavenSettings != null && mavenSettings.getServers() != null) {
request.withConnectTimeout(repository.getTimeout() == null ? Duration.ofSeconds(10) : repository.getTimeout());
request.withReadTimeout(repository.getTimeout() == null ? Duration.ofSeconds(30) : repository.getTimeout());