From d670ecfa6d7554aadfb5d32f50835b1b14becab8 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 17 Mar 2010 09:27:39 -0700 Subject: [PATCH] Issue 122: add proxy config to default http provider --- core/src/main/java/org/jclouds/Constants.java | 31 ++++++++++++-- .../java/org/jclouds/PropertiesBuilder.java | 36 +++++++++++++++++ .../main/java/org/jclouds/http/HttpUtils.java | 40 +++++++++++++++++++ .../JavaUrlHttpCommandExecutorService.java | 17 +++++++- 4 files changed, 119 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/jclouds/Constants.java b/core/src/main/java/org/jclouds/Constants.java index 12494314aa..f4092ccd8d 100644 --- a/core/src/main/java/org/jclouds/Constants.java +++ b/core/src/main/java/org/jclouds/Constants.java @@ -87,6 +87,31 @@ public interface Constants { * Whether or not to use the proxy setup from the underlying operating system. */ public static final String PROPERTY_PROXY_SYSTEM = "jclouds.use_system_proxy"; + /** + * String property. + *

+ *Explicitly sets the host name of a HTTP proxy server. + */ + public static final String PROPERTY_PROXY_HOST = "jclouds.proxy_host"; + /** + * Integer property. + *

+ * Explicitly sets the port number of a HTTP proxy server. + */ + public static final String PROPERTY_PROXY_PORT = "jclouds.proxy_port"; + /** + * String property. + *

+ * Explicitly sets the user name credential for proxy authentication. + */ + public static final String PROPERTY_PROXY_USER = "jclouds.proxy_user"; + /** + * String property. + *

+ * Explicitly sets the password credential for proxy authentication. + */ + public static final String PROPERTY_PROXY_PASSWORD = "jclouds.proxy_password"; + /** * Integer property. *

@@ -125,9 +150,9 @@ public interface Constants { * Name of the logger that records the steps of the request signing process of the HTTP_service. */ public static final String LOGGER_SIGNATURE = "jclouds.signature"; - /** - * Name of the custom adapter bindings map for Gson - */ + /** + * Name of the custom adapter bindings map for Gson + */ public static final String PROPERTY_GSON_ADAPTERS = "jclouds.gson.adapters"; } diff --git a/core/src/main/java/org/jclouds/PropertiesBuilder.java b/core/src/main/java/org/jclouds/PropertiesBuilder.java index b05c7cdc0c..96888da645 100755 --- a/core/src/main/java/org/jclouds/PropertiesBuilder.java +++ b/core/src/main/java/org/jclouds/PropertiesBuilder.java @@ -26,7 +26,11 @@ import static org.jclouds.Constants.PROPERTY_MAX_CONNECTION_REUSE; import static org.jclouds.Constants.PROPERTY_MAX_REDIRECTS; import static org.jclouds.Constants.PROPERTY_MAX_RETRIES; import static org.jclouds.Constants.PROPERTY_MAX_SESSION_FAILURES; +import static org.jclouds.Constants.PROPERTY_PROXY_HOST; +import static org.jclouds.Constants.PROPERTY_PROXY_PASSWORD; +import static org.jclouds.Constants.PROPERTY_PROXY_PORT; import static org.jclouds.Constants.PROPERTY_PROXY_SYSTEM; +import static org.jclouds.Constants.PROPERTY_PROXY_USER; import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME; import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT; import static org.jclouds.Constants.PROPERTY_USER_THREADS; @@ -59,6 +63,38 @@ public abstract class PropertiesBuilder { return this; } + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_HOST + */ + public PropertiesBuilder withProxyHost(String proxyHost) { + properties.setProperty(PROPERTY_PROXY_HOST, proxyHost); + return this; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_PORT + */ + public PropertiesBuilder withProxyPort(int proxyPort) { + properties.setProperty(PROPERTY_PROXY_PORT, Integer.toString(proxyPort)); + return this; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_USER + */ + public PropertiesBuilder withProxyUser(String proxyUser) { + properties.setProperty(PROPERTY_PROXY_USER, proxyUser); + return this; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_PASSWORD + */ + public PropertiesBuilder withProxyPassword(String proxyPassword) { + properties.setProperty(PROPERTY_PROXY_PASSWORD, proxyPassword); + return this; + } + /** * @see org.jclouds.Constants.PROPERTY_SO_TIMEOUT */ diff --git a/core/src/main/java/org/jclouds/http/HttpUtils.java b/core/src/main/java/org/jclouds/http/HttpUtils.java index 826aaed0d9..ed54d71062 100644 --- a/core/src/main/java/org/jclouds/http/HttpUtils.java +++ b/core/src/main/java/org/jclouds/http/HttpUtils.java @@ -74,6 +74,18 @@ public class HttpUtils { private final int globalMaxConnectionsPerHost; private final int connectionTimeout; private final int soTimeout; + @Inject(optional = true) + @Named(Constants.PROPERTY_PROXY_HOST) + private String proxyHost; + @Inject(optional = true) + @Named(Constants.PROPERTY_PROXY_PORT) + private Integer proxyPort; + @Inject(optional = true) + @Named(Constants.PROPERTY_PROXY_USER) + private String proxyUser; + @Inject(optional = true) + @Named(Constants.PROPERTY_PROXY_PASSWORD) + private String proxyPassword; @Inject public HttpUtils(@Named(Constants.PROPERTY_CONNECTION_TIMEOUT) int connectionTimeout, @@ -86,6 +98,34 @@ public class HttpUtils { this.globalMaxConnectionsPerHost = globalMaxConnectionsPerHost; } + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_HOST + */ + public String getProxyHost() { + return proxyHost; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_PORT + */ + public Integer getProxyPort() { + return proxyPort; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_USER + */ + public String getProxyUser() { + return proxyUser; + } + + /** + * @see org.jclouds.Constants.PROPERTY_PROXY_PASSWORD + */ + public String getProxyPassword() { + return proxyPassword; + } + public int getSocketOpenTimeout() { return soTimeout; } diff --git a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java index 44fc93e4b9..51e8a245c6 100644 --- a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java +++ b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java @@ -23,9 +23,13 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.Authenticator; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.ProxySelector; +import java.net.SocketAddress; import java.net.URL; import java.util.concurrent.ExecutorService; @@ -105,7 +109,6 @@ public class JavaUrlHttpCommandExecutorService extends response.setMessage(connection.getResponseMessage()); return response; } - public InputStream consumeOnClose(InputStream in) { return new ConsumeOnCloseInputStream(in); @@ -138,7 +141,6 @@ public class JavaUrlHttpCommandExecutorService extends } - private InputStream bufferAndCloseStream(InputStream inputStream) throws IOException { InputStream in = null; try { @@ -161,6 +163,17 @@ public class JavaUrlHttpCommandExecutorService extends Iterable proxies = ProxySelector.getDefault().select(request.getEndpoint()); Proxy proxy = Iterables.getLast(proxies); connection = (HttpURLConnection) url.openConnection(proxy); + } else if (utils.getProxyHost() != null) { + SocketAddress addr = new InetSocketAddress(utils.getProxyHost(), utils.getProxyPort()); + Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); + Authenticator authenticator = new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + return (new PasswordAuthentication(utils.getProxyUser(), utils.getProxyPassword() + .toCharArray())); + } + }; + Authenticator.setDefault(authenticator); + connection = (HttpURLConnection) url.openConnection(proxy); } else { connection = (HttpURLConnection) url.openConnection(); }