|
36 | 36 | import org.codehaus.plexus.util.xml.Xpp3Dom; |
37 | 37 | import org.jspecify.annotations.Nullable; |
38 | 38 | import org.openrewrite.ExecutionContext; |
| 39 | +import org.openrewrite.HttpSenderExecutionContextView; |
39 | 40 | import org.openrewrite.ParseExceptionResult; |
40 | 41 | import org.openrewrite.PathUtils; |
41 | 42 | import org.openrewrite.SourceFile; |
| 43 | +import org.openrewrite.ipc.http.HttpUrlConnectionSender; |
42 | 44 | import org.openrewrite.groovy.GroovyParser; |
43 | 45 | import org.openrewrite.internal.StringUtils; |
44 | 46 | import org.openrewrite.java.JavaParser; |
|
78 | 80 | import java.io.IOException; |
79 | 81 | import java.io.InputStream; |
80 | 82 | import java.io.UncheckedIOException; |
| 83 | +import java.net.Authenticator; |
| 84 | +import java.net.InetSocketAddress; |
| 85 | +import java.net.PasswordAuthentication; |
81 | 86 | import java.nio.charset.Charset; |
82 | 87 | import java.nio.charset.StandardCharsets; |
83 | 88 | import java.nio.file.*; |
84 | 89 | import java.nio.file.attribute.BasicFileAttributes; |
| 90 | +import java.time.Duration; |
85 | 91 | import java.util.*; |
86 | 92 | import java.util.concurrent.atomic.AtomicBoolean; |
87 | 93 | import java.util.function.Function; |
88 | 94 | import java.util.function.Supplier; |
89 | 95 | import java.util.function.UnaryOperator; |
| 96 | +import java.util.regex.Pattern; |
| 97 | +import java.util.stream.Collectors; |
90 | 98 | import java.util.stream.Stream; |
91 | 99 |
|
92 | 100 | import static java.util.Collections.emptyList; |
@@ -666,6 +674,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec |
666 | 674 | MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView.view(ctx); |
667 | 675 | mavenExecutionContext.setMavenSettings(settings); |
668 | 676 | mavenExecutionContext.setResolutionListener(new MavenLoggingResolutionEventListener(logger)); |
| 677 | + configureProxy(settings, ctx); |
669 | 678 |
|
670 | 679 | // The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb |
671 | 680 | // If the flag is set to false, only the default, in-memory cache is used. |
@@ -864,7 +873,84 @@ public MavenSettings buildSettings() { |
864 | 873 | ); |
865 | 874 | }).collect(toList())); |
866 | 875 |
|
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 | + } |
868 | 954 | } |
869 | 955 |
|
870 | 956 | private static @Nullable RawRepositories buildRawRepositories(@Nullable List<Repository> repositoriesToMap) { |
|
0 commit comments