Issue 122: added proxy support to default java engine

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2649 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2010-01-13 21:05:23 +00:00
parent ec6636145a
commit 7559ba165e
8 changed files with 83 additions and 5 deletions

View File

@ -24,6 +24,9 @@ package org.jclouds.http;
* @author Adrian Cole
*/
public interface HttpConstants {
public static final String PROPERTY_HTTP_PROXY_ADDRESS = "jclouds.http.proxy.address";
public static final String PROPERTY_HTTP_PROXY_SYSTEM = "jclouds.http.proxy.system";
public static final String PROPERTY_HTTP_PROXY_PORT = "jclouds.http.proxy.port";
public static final String PROPERTY_HTTP_MAX_RETRIES = "jclouds.http.max-retries";
public static final String PROPERTY_HTTP_MAX_REDIRECTS = "jclouds.http.max-redirects";
public static final String HTTP_HEADERS_LOGGER = "jclouds.http.headers";

View File

@ -20,6 +20,9 @@ package org.jclouds.http;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_MAX_REDIRECTS;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_MAX_RETRIES;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_PROXY_ADDRESS;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_PROXY_PORT;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_PROXY_SYSTEM;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_RELAX_HOSTNAME;
import static org.jclouds.http.pool.PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS;
import static org.jclouds.http.pool.PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS;
@ -27,6 +30,7 @@ import static org.jclouds.http.pool.PoolConstants.PROPERTY_POOL_MAX_CONNECTION_R
import static org.jclouds.http.pool.PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES;
import static org.jclouds.http.pool.PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS;
import java.net.InetAddress;
import java.util.Properties;
import com.google.common.annotations.VisibleForTesting;
@ -46,6 +50,21 @@ public class HttpPropertiesBuilder {
return this;
}
public HttpPropertiesBuilder withProxyAddress(InetAddress proxyAddress) {
properties.setProperty(PROPERTY_HTTP_PROXY_ADDRESS, proxyAddress.getHostAddress());
return this;
}
public HttpPropertiesBuilder useSystemProxies(boolean useSystemProxies) {
properties.setProperty(PROPERTY_HTTP_PROXY_SYSTEM, useSystemProxies+"");
return this;
}
public HttpPropertiesBuilder withProxyPort(int proxyPort) {
properties.setProperty(PROPERTY_HTTP_PROXY_PORT, proxyPort+"");
return this;
}
public HttpPropertiesBuilder withHttpMaxRetries(int httpMaxRetries) {
properties.setProperty(PROPERTY_HTTP_MAX_RETRIES, Integer.toString(httpMaxRetries));
return this;

View File

@ -22,10 +22,16 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.net.ssl.HostnameVerifier;
@ -40,6 +46,7 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import com.google.inject.Inject;
@ -59,7 +66,22 @@ public class JavaUrlHttpCommandExecutorService extends
@Named(HttpConstants.PROPERTY_HTTP_RELAX_HOSTNAME)
private boolean relaxHostname = false;
private final Map<String, String> sslMap;
@Inject(optional = true)
@Named(HttpConstants.PROPERTY_HTTP_PROXY_ADDRESS)
@Nullable
private String proxyAddress = System.getProperty("http.proxyHost");
@Inject(optional = true)
@Named(HttpConstants.PROPERTY_HTTP_PROXY_ADDRESS)
private boolean systemProxies = System.getProperty("java.net.useSystemProxies") != null ?
Boolean.parseBoolean(System.getProperty("java.net.useSystemProxies")) : false;
@Inject(optional = true)
@Named(HttpConstants.PROPERTY_HTTP_PROXY_PORT)
private int proxyPort = System.getProperty("http.proxyPort") != null ?
Integer.parseInt(System.getProperty("http.proxyPort")) : 80;
@Inject
public JavaUrlHttpCommandExecutorService(ExecutorService executorService,
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler, HttpWire wire) {
@ -105,7 +127,19 @@ public class JavaUrlHttpCommandExecutorService extends
@Override
protected HttpURLConnection convert(HttpRequest request) throws IOException {
URL url = request.getEndpoint().toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
HttpURLConnection connection;
if (systemProxies) {
System.setProperty("java.net.useSystemProxies", "true");
Iterable<Proxy> proxies = ProxySelector.getDefault().select(request.getEndpoint());
Proxy proxy = Iterables.getLast(proxies);
connection = (HttpURLConnection) url.openConnection(proxy);
} else if (proxyAddress != null) {
SocketAddress proxySocketAddress = new InetSocketAddress(InetAddress.getByName(proxyAddress), proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxySocketAddress);
connection = (HttpURLConnection) url.openConnection(proxy);
} else {
connection = (HttpURLConnection) url.openConnection();
}
if (relaxHostname && connection instanceof HttpsURLConnection) {
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
sslCon.setHostnameVerifier(new LogToMapHostnameVerifier());

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.http;
import java.net.URI;
import java.util.Map;
import javax.ws.rs.GET;
@ -32,6 +33,7 @@ import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Endpoint;
import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.MapPayloadParam;
@ -137,6 +139,9 @@ public interface IntegrationTestAsyncClient {
@Path("objects/{id}")
ListenableFuture<String> download(@PathParam("id") String id, @HeaderParam("test") String header);
@GET
ListenableFuture<String> download(@EndpointParam URI endpoint);
@GET
@Path("objects/{id}")
@XMLResponseParser(BarHandler.class)

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.http;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -60,4 +61,5 @@ public interface IntegrationTestClient {
StringBuffer newStringBuffer();
String download(URI endpoint);
}

View File

@ -18,11 +18,13 @@
*/
package org.jclouds.http;
import com.google.inject.Module;
import java.util.Properties;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService;
import org.testng.annotations.Test;
import java.util.Properties;
import com.google.inject.Module;
/**
* Tests the functionality of the {@link JavaUrlHttpCommandExecutorService}
@ -39,4 +41,5 @@ public class JavaUrlHttpCommandExecutorServiceTest extends BaseHttpCommandExecut
protected void addConnectionProperties(Properties props) {
// NONE
}
}

View File

@ -119,7 +119,7 @@ class ShrinkBlob < Task
print "connecting to service ",destination.getHost(),"/",@container,"\n"
# array thing is a weird hack since jruby doesn't understand varargs
context = BlobStoreContextFactory.new().createContext(destination, java.lang.reflect.Array.newInstance(com.google.inject.Module.java_class,0))
context = BlobStoreContextFactory.new().createContext(destination)
context.getBlobStore().createContainer(@container)
print "uploading to ",destination.getHost(),"/",@container,"/",@zip,"\n"

View File

@ -428,6 +428,18 @@ pageTracker._trackPageview();
<include>**/*LiveTest.java</include>
</includes>
<systemProperties>
<!--
If you're behind a proxy, set this here
<property>
<name>http.proxyHost</name>
<value>host</value>
</property>
<property>
<name>http.proxyPort</name>
<value>port</value>
</property>
-->
<property>
<name>file.encoding</name>
<value>${sourceEncoding}</value>