From fb0c122c021ce4e6bae251bfac4ef81d7f5492aa Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Thu, 10 Oct 2024 12:21:20 +0200 Subject: [PATCH] Set default connection/read timeouts for `MavenPomDownloader` (#4564) * Set default connection/read timeouts for `MavenPomDownloader` Connection: 10s Read: 30s * update method name --- .../maven/internal/MavenPomDownloader.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 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..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 @@ -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,25 +777,21 @@ 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)); + 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); } @@ -851,10 +847,8 @@ 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()); - return sendRequest(applyAuthenticationToRequest(repo, request).build()); + HttpSender.Request.Builder request = httpSender.get(uriString); + return sendRequest(applyAuthenticationAndTimeoutToRequest(repo, request).build()); } catch (HttpSenderResponseException e) { if (hasCredentials(repo) && e.isClientSideException()) { return retryRequestAnonymously(uriString, e); @@ -886,8 +880,10 @@ 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()); for (MavenSettings.Server server : mavenSettings.getServers().getServers()) { if (server.getId().equals(repository.getId()) && server.getConfiguration() != null) { MavenSettings.ServerConfiguration configuration = server.getConfiguration();