From 47d079d937765de486f28e505acb9e5c8c1a217d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 28 Nov 2013 10:10:53 +0000 Subject: [PATCH] Add default proxy settings from System proerties Aether apparently doesn't use the java.net.* APIs for TCP connections so it doesn't notice when a user sets -Dhttp.Proxy*. To fix it is painful, and leads me to suspect that actually we might want to parse a settings.xml at some point (however unpalatable that is). For now I have added a Proxy to all RemoteRepository instances that we create in the CLI if the user has set -Dhttp.proxyHost (and/or -Dhttps.proxyHost for a secure repository). TODO: authentication. Is there a standard way to specify that globally via system properties. TODO: maybe use per-repository settings if provided (e.g. in settings.xml). --- .../boot/cli/compiler/GroovyCompiler.java | 3 +- .../cli/compiler/grape/AetherGrapeEngine.java | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java index 6564775af6..af75f3e9aa 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java @@ -140,7 +140,8 @@ public class GroovyCompiler { RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE)); } - + builder.setProxy(AetherGrapeEngine.defaultProxy(repositoryConfiguration + .getUri().getScheme())); repositories.add(builder.build()); } return repositories; diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java index 60487a5a99..d9afbd6967 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java @@ -42,6 +42,7 @@ import org.eclipse.aether.impl.DefaultServiceLocator; import org.eclipse.aether.internal.impl.DefaultRepositorySystem; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.LocalRepositoryManager; +import org.eclipse.aether.repository.Proxy; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; @@ -94,6 +95,33 @@ public class AetherGrapeEngine implements GrapeEngine { this.progressReporter = getProgressReporter(session); } + public static Proxy defaultProxy(String protocol) { + // TODO: proxy authentication + if ("http".equals(protocol) || "dav".equals(protocol)) { + String proxyHost = System.getProperty("http.proxyHost"); + if (proxyHost != null) { + // Use defaults from normal JVM proxy handler + return new Proxy("http", proxyHost, new Integer(System.getProperty( + "http.proxyPort", "80"))); + } + } + else if ("https".equals(protocol) || "davs".equals(protocol)) { + String secureProxyHost = System.getProperty("https.proxyHost"); + if (secureProxyHost != null) { + return new Proxy("https", secureProxyHost, new Integer( + System.getProperty("https.proxyPort", "443"))); + } + } + else if ("ftp".equals(protocol)) { + String secureProxyHost = System.getProperty("ftp.proxyHost"); + if (secureProxyHost != null) { + return new Proxy("ftp", secureProxyHost, new Integer(System.getProperty( + "ftp.proxyPort", "443"))); + } + } + return null; + } + private ServiceLocator createServiceLocator() { DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator(); locator.addService(RepositorySystem.class, DefaultRepositorySystem.class); @@ -245,9 +273,13 @@ public class AetherGrapeEngine implements GrapeEngine { public void addResolver(Map args) { String name = (String) args.get("name"); String root = (String) args.get("root"); + RemoteRepository.Builder builder = new RemoteRepository.Builder(name, "default", + root); + String protocol = root.contains(":") ? root.substring(0, root.indexOf(":")) + : "none"; + builder.setProxy(AetherGrapeEngine.defaultProxy(protocol)); - this.repositories - .add(new RemoteRepository.Builder(name, "default", root).build()); + this.repositories.add(builder.build()); } @Override