Issue 122: add proxy config to default http provider

This commit is contained in:
Adrian Cole 2010-03-17 09:27:39 -07:00
parent 785ce56d3f
commit d670ecfa6d
4 changed files with 119 additions and 5 deletions

View File

@ -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.
* <p/>
*Explicitly sets the host name of a HTTP proxy server.
*/
public static final String PROPERTY_PROXY_HOST = "jclouds.proxy_host";
/**
* Integer property.
* <p/>
* Explicitly sets the port number of a HTTP proxy server.
*/
public static final String PROPERTY_PROXY_PORT = "jclouds.proxy_port";
/**
* String property.
* <p/>
* Explicitly sets the user name credential for proxy authentication.
*/
public static final String PROPERTY_PROXY_USER = "jclouds.proxy_user";
/**
* String property.
* <p/>
* Explicitly sets the password credential for proxy authentication.
*/
public static final String PROPERTY_PROXY_PASSWORD = "jclouds.proxy_password";
/**
* Integer property.
* <p/>
@ -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";
}

View File

@ -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
*/

View File

@ -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;
}

View File

@ -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;
@ -106,7 +110,6 @@ public class JavaUrlHttpCommandExecutorService extends
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<Proxy> 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();
}