Skip to content

Commit

Permalink
Allow configuration of HTTP timeouts for maven repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Jan 29, 2024
1 parent 719e812 commit 3aaf687
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -74,12 +75,16 @@ class Request {
private final byte[] entity;
private final Method method;
private final Map<String, String> requestHeaders;
private final Duration connectTimeout;
private final Duration readTimeout;

public Request(URL url, byte[] entity, Method method, Map<String, String> requestHeaders) {
public Request(URL url, byte[] entity, Method method, Map<String, String> requestHeaders, Duration connectTimeout, Duration readTimeout) {
this.url = url;
this.entity = entity;
this.method = method;
this.requestHeaders = requestHeaders;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}

public URL getUrl() {
Expand All @@ -98,6 +103,14 @@ public Map<String, String> getRequestHeaders() {
return requestHeaders;
}

public Duration getConnectTimeout() {
return connectTimeout;
}

public Duration getReadTimeout() {
return readTimeout;
}

public static Builder build(String uri, HttpSender sender) {
return new Builder(uri, sender);
}
Expand Down Expand Up @@ -125,6 +138,8 @@ public static class Builder {
private URL url;
private byte[] entity = new byte[0];
private Method method;
private Duration connectTimeout;
private Duration readTimeout;

Builder(String url, HttpSender sender) {
try {
Expand Down Expand Up @@ -277,8 +292,9 @@ public final Builder compress() throws IOException {
* @throws IOException If compression fails.
*/
public final Builder compressWhen(Supplier<Boolean> when) throws IOException {
if (when.get())
if (when.get()) {
return compress();
}
return this;
}

Expand Down Expand Up @@ -331,7 +347,17 @@ public Response send() {
}

public Request build() {
return new Request(url, entity, method, requestHeaders);
return new Request(url, entity, method, requestHeaders, connectTimeout, readTimeout);
}

public Builder withConnectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

public Builder withReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ public Response send(Request request) {
} else {
con = (HttpURLConnection) request.getUrl().openConnection();
}
con.setConnectTimeout(connectTimeoutMs);
con.setReadTimeout(readTimeoutMs);
if (request.getConnectTimeout() != null) {
con.setConnectTimeout((int) request.getConnectTimeout().toMillis());
} else {
con.setConnectTimeout(connectTimeoutMs);
}
if (request.getReadTimeout() != null) {
con.setReadTimeout((int) request.getReadTimeout().toMillis());
} else {
con.setReadTimeout(readTimeoutMs);
}
Method method = request.getMethod();
con.setRequestMethod(method.name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ private List<MavenRepository> mapRepositories(MavenSettings settings, List<Strin
repo.getReleases() == null ? null : repo.getReleases().getEnabled(),
repo.getSnapshots() == null ? null : repo.getSnapshots().getEnabled(),
null,
null,
null,
null
);
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package org.openrewrite.maven;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
Expand Down Expand Up @@ -46,12 +49,14 @@
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Data
@AllArgsConstructor
@JacksonXmlRootElement(localName = "settings")
public class MavenSettings {
@Nullable
String localRepository;

@Nullable
@NonFinal
@JsonIgnore
MavenRepository mavenLocal;

@Nullable
Expand Down Expand Up @@ -238,8 +243,11 @@ private ServerConfiguration interpolate(@Nullable ServerConfiguration configurat
if (configuration == null) {
return null;
}
return new ServerConfiguration(configuration.httpHeaders == null ? null :
ListUtils.map(configuration.httpHeaders, this::interpolate));
return new ServerConfiguration(
configuration.httpHeaders == null ? null : ListUtils.map(configuration.httpHeaders, this::interpolate),
configuration.connectTimeout == null ? null : configuration.connectTimeout,
configuration.readTimeout == null ? null : configuration.readTimeout
);
}

private HttpHeader interpolate(HttpHeader httpHeader) {
Expand Down Expand Up @@ -406,9 +414,16 @@ public static class Server {
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@Data
@With
@JsonIgnoreProperties(value = "httpHeaders")
public static class ServerConfiguration {
@JacksonXmlProperty(localName = "property")
@JacksonXmlElementWrapper(localName = "httpHeaders", useWrapping = true) // wrapping is disabled by default on MavenXmlMapper
@Nullable
List<HttpHeader> httpHeaders;
@Nullable
Long connectTimeout;
@Nullable
Long readTimeout;
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public Xml visitDocument(Xml.Document document, P p) {
t.getChild("releases").flatMap(s -> s.getChildValue("enabled")).orElse(null),
t.getChild("snapshots").flatMap(s -> s.getChildValue("enabled")).orElse(null),
null,
null,
null,
null
)).collect(Collectors.toList()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,9 @@ public MavenRepository normalizeRepository(MavenRepository originalRepository, M
repository.getReleases(),
repository.getSnapshots(),
repository.getUsername(),
repository.getPassword());
repository.getPassword(),
repository.getConnectTimeout(),
repository.getReadTimeout());
} catch (HttpSenderResponseException e) {
//Response was returned from the server, but it was not a 200 OK. The server therefore exists.
if (e.getResponseCode() != null) {
Expand All @@ -761,7 +763,9 @@ public MavenRepository normalizeRepository(MavenRepository originalRepository, M
repository.getReleases(),
repository.getSnapshots(),
repository.getUsername(),
repository.getPassword());
repository.getPassword(),
repository.getConnectTimeout(),
repository.getReadTimeout());
} else if (!e.isClientSideException()) {
return originalRepository;
}
Expand Down Expand Up @@ -792,7 +796,10 @@ public MavenRepository normalizeRepository(MavenRepository originalRepository, M
*/
private byte[] requestAsAuthenticatedOrAnonymous(MavenRepository repo, String uriString) throws HttpSenderResponseException {
try {
return sendRequest(applyAuthenticationToRequest(repo, httpSender.get(uriString)).build());
HttpSender.Request.Builder request = httpSender.get(uriString)
.withConnectTimeout(repo.getConnectTimeout())
.withReadTimeout(repo.getReadTimeout());
return sendRequest(applyAuthenticationToRequest(repo, request).build());
} catch (HttpSenderResponseException e) {
if (hasCredentials(repo) && e.isClientSideException()) {
return retryRequestAnonymously(uriString, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private List<MavenRepository> mapRepositories(@Nullable RawRepositories rawRepos
pomRepositories.add(new MavenRepository(r.getId(), r.getUrl(),
r.getReleases() == null ? null : r.getReleases().getEnabled(),
r.getSnapshots() == null ? null : r.getSnapshots().getEnabled(),
false, null, null, null));
false, null, null, null, null, null));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.Serializable;
import java.net.URI;
import java.nio.file.Paths;
import java.time.Duration;

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@ref")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand All @@ -37,9 +38,9 @@
@RequiredArgsConstructor
public class MavenRepository implements Serializable {

public static final MavenRepository MAVEN_LOCAL_USER_NEUTRAL = new MavenRepository("local", new File("~/.m2/repository").toString(), "true", "true", true, null, null, false);
public static final MavenRepository MAVEN_LOCAL_DEFAULT = new MavenRepository("local", Paths.get(System.getProperty("user.home"), ".m2", "repository").toUri().toString(), "true", "true", true, null, null, false);
public static final MavenRepository MAVEN_CENTRAL = new MavenRepository("central", "https://repo.maven.apache.org/maven2", "true", "false", true, null, null, true);
public static final MavenRepository MAVEN_LOCAL_USER_NEUTRAL = new MavenRepository("local", new File("~/.m2/repository").toString(), "true", "true", true, null, null, null, null, false);
public static final MavenRepository MAVEN_LOCAL_DEFAULT = new MavenRepository("local", Paths.get(System.getProperty("user.home"), ".m2", "repository").toUri().toString(), "true", "true", true, null, null, null, null, false);
public static final MavenRepository MAVEN_CENTRAL = new MavenRepository("central", "https://repo.maven.apache.org/maven2", "true", "false", true, null, null, null, null, true);

@EqualsAndHashCode.Include
@With
Expand Down Expand Up @@ -79,14 +80,51 @@ public class MavenRepository implements Serializable {
@Nullable
String password;

@With
@Nullable
Duration connectTimeout;

@With
@Nullable
Duration readTimeout;

@Nullable
@NonFinal
Boolean deriveMetadataIfMissing;

/**
* Constructor required by {@link org.openrewrite.maven.tree.OpenRewriteModelSerializableTest}.
*
* @deprecated Use {@link #MavenRepository(String, String, String, String, boolean, String, String, Duration, Duration, Boolean)}
*/
@Deprecated
@JsonIgnore
public MavenRepository(
@Nullable String id, String uri, @Nullable String releases, @Nullable String snapshots,
@Nullable String username, @Nullable String password
) {
this(id, uri, releases, snapshots, false, username, password, null, null, null);
}

/**
* Constructor required by {@link org.openrewrite.gradle.marker.GradleProject}.
*
* @deprecated Use {@link #MavenRepository(String, String, String, String, boolean, String, String, Duration, Duration, Boolean)}
*/
@Deprecated
@JsonIgnore
public MavenRepository(
@Nullable String id, String uri, @Nullable String releases, @Nullable String snapshots, boolean knownToExist,
@Nullable String username, @Nullable String password, @Nullable Boolean deriveMetadataIfMissing
) {
this(id, uri, releases, snapshots, knownToExist, username, password, null, null, deriveMetadataIfMissing);
}

@JsonIgnore
public MavenRepository(
@Nullable String id, String uri, @Nullable String releases, @Nullable String snapshots, boolean knownToExist,
@Nullable String username, @Nullable String password, @Nullable Duration connectTimeout,
@Nullable Duration readTimeout, @Nullable Boolean deriveMetadataIfMissing
) {
this.id = id;
this.uri = uri;
Expand All @@ -95,6 +133,8 @@ public MavenRepository(
this.knownToExist = knownToExist;
this.username = username;
this.password = password;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
this.deriveMetadataIfMissing = deriveMetadataIfMissing;
}

Expand All @@ -115,12 +155,14 @@ public static class Builder {
String username;
String password;
Boolean deriveMetadataIfMissing;
Duration connectTimeout;
Duration readTimeout;

private Builder() {
}

public MavenRepository build() {
return new MavenRepository(id, uri, releases, snapshots, knownToExist, username, password, deriveMetadataIfMissing);
return new MavenRepository(id, uri, releases, snapshots, knownToExist, username, password, connectTimeout, readTimeout, deriveMetadataIfMissing);
}

public Builder releases(boolean releases) {
Expand Down Expand Up @@ -163,6 +205,16 @@ public Builder password(String password) {
return this;
}

public Builder connectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

public Builder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}

@Nullable
private static String resolveEnvironmentProperty(@Nullable String rawProperty) {
if (rawProperty == null) {
Expand Down
Loading

0 comments on commit 3aaf687

Please sign in to comment.