Sitelet https://github.com/openrewrite/rewrite-maven-plugin/commit/f06cf1cb25a903bb927718b34e680ab77aa62b1f
Skip to content

Commit f06cf1c

Browse files
authored
Load proxy settings from Maven settings.xml (#1127)
* Load proxy settings from Maven settings.xml for dependency resolution * Bump rewrite.version to 8.77.0-SNAPSHOT for proxy support * Respect nonProxyHosts when configuring proxy for Maven resolution * Revert rewrite.version to 8.76.0-SNAPSHOT until upstream is published * Use Maven proxy objects directly and clean up fully-qualified types Read proxy settings from MavenExecutionRequest instead of routing through MavenSettings.Proxy/Proxies classes that aren't published yet in rewrite-core 8.76.0-SNAPSHOT. Replace fully-qualified java.net and java.time types with imports per review feedback. * Restore MavenSettings.Proxy/Proxies types from upstream rewrite-core Pass proxy settings through MavenSettings so they participate in settings merge and property interpolation. Depends on the Proxy/Proxies classes added to rewrite-core main.
1 parent c6551ed commit f06cf1c

1 file changed

Lines changed: 87 additions & 1 deletion

File tree

‎src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import org.codehaus.plexus.util.xml.Xpp3Dom;
3737
import org.jspecify.annotations.Nullable;
3838
import org.openrewrite.ExecutionContext;
39+
import org.openrewrite.HttpSenderExecutionContextView;
3940
import org.openrewrite.ParseExceptionResult;
4041
import org.openrewrite.PathUtils;
4142
import org.openrewrite.SourceFile;
43+
import org.openrewrite.ipc.http.HttpUrlConnectionSender;
4244
import org.openrewrite.groovy.GroovyParser;
4345
import org.openrewrite.internal.StringUtils;
4446
import org.openrewrite.java.JavaParser;
@@ -78,15 +80,21 @@
7880
import java.io.IOException;
7981
import java.io.InputStream;
8082
import java.io.UncheckedIOException;
83+
import java.net.Authenticator;
84+
import java.net.InetSocketAddress;
85+
import java.net.PasswordAuthentication;
8186
import java.nio.charset.Charset;
8287
import java.nio.charset.StandardCharsets;
8388
import java.nio.file.*;
8489
import java.nio.file.attribute.BasicFileAttributes;
90+
import java.time.Duration;
8591
import java.util.*;
8692
import java.util.concurrent.atomic.AtomicBoolean;
8793
import java.util.function.Function;
8894
import java.util.function.Supplier;
8995
import java.util.function.UnaryOperator;
96+
import java.util.regex.Pattern;
97+
import java.util.stream.Collectors;
9098
import java.util.stream.Stream;
9199

92100
import static java.util.Collections.emptyList;
@@ -666,6 +674,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
666674
MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView.view(ctx);
667675
mavenExecutionContext.setMavenSettings(settings);
668676
mavenExecutionContext.setResolutionListener(new MavenLoggingResolutionEventListener(logger));
677+
configureProxy(settings, ctx);
669678

670679
// The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
671680
// If the flag is set to false, only the default, in-memory cache is used.
@@ -864,7 +873,84 @@ public MavenSettings buildSettings() {
864873
);
865874
}).collect(toList()));
866875

867-
return new MavenSettings(mer.getLocalRepositoryPath().toString(), profiles, activeProfiles, mirrors, servers);
876+
MavenSettings.Proxies proxies = new MavenSettings.Proxies();
877+
proxies.setProxies(mer.getProxies().stream().map(p -> {
878+
SettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest();
879+
decryptionRequest.setProxies(singletonList(p));
880+
SettingsDecryptionResult decryptionResult = settingsDecrypter.decrypt(decryptionRequest);
881+
org.apache.maven.settings.Proxy decryptedProxy = decryptionResult.getProxies().isEmpty() ? p : decryptionResult.getProxies().get(0);
882+
return new MavenSettings.Proxy(
883+
p.getId(),
884+
p.isActive(),
885+
p.getProtocol(),
886+
p.getHost(),
887+
p.getPort(),
888+
p.getUsername(),
889+
decryptedProxy.getPassword(),
890+
p.getNonProxyHosts()
891+
);
892+
}).collect(toList()));
893+
894+
return new MavenSettings(mer.getLocalRepositoryPath().toString(), profiles, activeProfiles, mirrors, servers, proxies);
895+
}
896+
897+
private void configureProxy(MavenSettings settings, ExecutionContext ctx) {
898+
if (settings.getProxies() == null || settings.getProxies().getProxies().isEmpty()) {
899+
return;
900+
}
901+
// Use the first active proxy
902+
MavenSettings.Proxy activeProxy = settings.getProxies().getProxies().stream()
903+
.filter(p -> p.getActive() == null || p.getActive())
904+
.findFirst()
905+
.orElse(null);
906+
if (activeProxy == null) {
907+
return;
908+
}
909+
910+
java.net.Proxy proxy = new java.net.Proxy(
911+
java.net.Proxy.Type.HTTP,
912+
new InetSocketAddress(activeProxy.getHost(), activeProxy.getPort())
913+
);
914+
915+
if (activeProxy.getUsername() != null && !activeProxy.getUsername().isEmpty()) {
916+
Authenticator.setDefault(new Authenticator() {
917+
@Override
918+
protected PasswordAuthentication getPasswordAuthentication() {
919+
if (getRequestorType() == RequestorType.PROXY) {
920+
return new PasswordAuthentication(
921+
activeProxy.getUsername(),
922+
activeProxy.getPassword() != null ? activeProxy.getPassword().toCharArray() : new char[0]
923+
);
924+
}
925+
return null;
926+
}
927+
});
928+
}
929+
930+
HttpUrlConnectionSender proxiedSender = new HttpUrlConnectionSender(
931+
Duration.ofSeconds(1), Duration.ofSeconds(10), proxy);
932+
HttpUrlConnectionSender directSender = new HttpUrlConnectionSender(
933+
Duration.ofSeconds(1), Duration.ofSeconds(10));
934+
935+
String nonProxyHosts = activeProxy.getNonProxyHosts();
936+
if (nonProxyHosts == null || nonProxyHosts.isEmpty()) {
937+
HttpSenderExecutionContextView.view(ctx).setHttpSender(proxiedSender);
938+
} else {
939+
// Parse nonProxyHosts patterns (pipe-delimited, * wildcards) into a regex
940+
String regex = Arrays.stream(nonProxyHosts.split("\\|"))
941+
.map(String::trim)
942+
.filter(s -> !s.isEmpty())
943+
.map(pattern -> pattern.replace(".", "\\.").replace("*", ".*"))
944+
.collect(Collectors.joining("|"));
945+
Pattern nonProxyPattern = Pattern.compile(regex);
946+
HttpSenderExecutionContextView.view(ctx).setHttpSender(request -> {
947+
String host = request.getUrl().getHost();
948+
if (nonProxyPattern.matcher(host).matches()) {
949+
return directSender.send(request);
950+
}
951+
return proxiedSender.send(request);
952+
});
953+
}
868954
}
869955

870956
private static @Nullable RawRepositories buildRawRepositories(@Nullable List<Repository> repositoriesToMap) {

0 commit comments

Comments
 (0)