mirror of https://github.com/apache/jclouds.git
decoupled ProxyConfig and introduced PROPERTY_PROXY_TYPE
This commit is contained in:
parent
21ab1ae836
commit
c5e3b24d3e
|
@ -18,6 +18,9 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds;
|
package org.jclouds;
|
||||||
|
|
||||||
|
import java.net.Proxy;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Location;
|
||||||
import org.jclouds.location.reference.LocationConstants;
|
import org.jclouds.location.reference.LocationConstants;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,24 +106,38 @@ public interface Constants {
|
||||||
* Whether or not to use the proxy setup from the underlying operating system.
|
* Whether or not to use the proxy setup from the underlying operating system.
|
||||||
*/
|
*/
|
||||||
public static final String PROPERTY_PROXY_SYSTEM = "jclouds.use-system-proxy";
|
public static final String PROPERTY_PROXY_SYSTEM = "jclouds.use-system-proxy";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String property.
|
* String property.
|
||||||
* <p/>
|
* <p/>
|
||||||
*Explicitly sets the host name of a HTTP proxy server.
|
*Explicitly sets the host name of a proxy server.
|
||||||
*/
|
*/
|
||||||
public static final String PROPERTY_PROXY_HOST = "jclouds.proxy-host";
|
public static final String PROPERTY_PROXY_HOST = "jclouds.proxy-host";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integer property.
|
* Integer property. default is 80 when {@link #PROPERTY_PROXY_TYPE} is
|
||||||
|
* {@code HTTP}, and 1080 when {@link #PROPERTY_PROXY_TYPE} is {@code SOCKS}.
|
||||||
* <p/>
|
* <p/>
|
||||||
* Explicitly sets the port number of a HTTP proxy server.
|
* Explicitly sets the port number of a proxy server.
|
||||||
*/
|
*/
|
||||||
public static final String PROPERTY_PROXY_PORT = "jclouds.proxy-port";
|
public static final String PROPERTY_PROXY_PORT = "jclouds.proxy-port";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String property. default {@code HTTP}, valid options: {@code HTTP}, {@code SOCKS}.
|
||||||
|
* <p/>
|
||||||
|
* Explicitly sets the type of a proxy server.
|
||||||
|
*
|
||||||
|
* @see Proxy.Type
|
||||||
|
*/
|
||||||
|
public static final String PROPERTY_PROXY_TYPE = "jclouds.proxy-type";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String property.
|
* String property.
|
||||||
* <p/>
|
* <p/>
|
||||||
* Explicitly sets the user name credential for proxy authentication.
|
* Explicitly sets the user name credential for proxy authentication.
|
||||||
*/
|
*/
|
||||||
public static final String PROPERTY_PROXY_USER = "jclouds.proxy-user";
|
public static final String PROPERTY_PROXY_USER = "jclouds.proxy-user";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String property.
|
* String property.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|
|
@ -79,27 +79,11 @@ public class HttpUtils {
|
||||||
@Named(Constants.PROPERTY_RELAX_HOSTNAME)
|
@Named(Constants.PROPERTY_RELAX_HOSTNAME)
|
||||||
private boolean relaxHostname = false;
|
private boolean relaxHostname = false;
|
||||||
|
|
||||||
@Inject(optional = true)
|
|
||||||
@Named(Constants.PROPERTY_PROXY_SYSTEM)
|
|
||||||
private boolean systemProxies = System.getProperty("java.net.useSystemProxies") != null ? Boolean
|
|
||||||
.parseBoolean(System.getProperty("java.net.useSystemProxies")) : false;
|
|
||||||
|
|
||||||
private final int globalMaxConnections;
|
private final int globalMaxConnections;
|
||||||
private final int globalMaxConnectionsPerHost;
|
private final int globalMaxConnectionsPerHost;
|
||||||
private final int connectionTimeout;
|
private final int connectionTimeout;
|
||||||
private final int soTimeout;
|
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(optional = true)
|
@Inject(optional = true)
|
||||||
@Named(Constants.PROPERTY_TRUST_ALL_CERTS)
|
@Named(Constants.PROPERTY_TRUST_ALL_CERTS)
|
||||||
private boolean trustAllCerts;
|
private boolean trustAllCerts;
|
||||||
|
@ -115,34 +99,6 @@ public class HttpUtils {
|
||||||
this.globalMaxConnectionsPerHost = globalMaxConnectionsPerHost;
|
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() {
|
public int getSocketOpenTimeout() {
|
||||||
return soTimeout;
|
return soTimeout;
|
||||||
}
|
}
|
||||||
|
@ -159,10 +115,6 @@ public class HttpUtils {
|
||||||
return trustAllCerts;
|
return trustAllCerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useSystemProxies() {
|
|
||||||
return systemProxies;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMaxConnections() {
|
public int getMaxConnections() {
|
||||||
return globalMaxConnections;
|
return globalMaxConnections;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ package org.jclouds.http.internal;
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static com.google.common.base.Throwables.propagate;
|
import static com.google.common.base.Throwables.propagate;
|
||||||
import static com.google.common.collect.Iterables.getLast;
|
|
||||||
import static com.google.common.io.ByteStreams.toByteArray;
|
import static com.google.common.io.ByteStreams.toByteArray;
|
||||||
import static com.google.common.io.Closeables.closeQuietly;
|
import static com.google.common.io.Closeables.closeQuietly;
|
||||||
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
|
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
|
||||||
|
@ -33,14 +32,10 @@ import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.net.Authenticator;
|
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.PasswordAuthentication;
|
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.ProxySelector;
|
import java.net.URI;
|
||||||
import java.net.SocketAddress;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -64,6 +59,7 @@ import org.jclouds.io.ContentMetadataCodec;
|
||||||
import org.jclouds.io.MutableContentMetadata;
|
import org.jclouds.io.MutableContentMetadata;
|
||||||
import org.jclouds.io.Payload;
|
import org.jclouds.io.Payload;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
import com.google.common.collect.ImmutableMultimap.Builder;
|
import com.google.common.collect.ImmutableMultimap.Builder;
|
||||||
|
@ -83,23 +79,26 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
|
||||||
.getProperty("java.version"));
|
.getProperty("java.version"));
|
||||||
|
|
||||||
private final Supplier<SSLContext> untrustedSSLContextProvider;
|
private final Supplier<SSLContext> untrustedSSLContextProvider;
|
||||||
|
private final Function<URI, Proxy> proxyForURI;
|
||||||
private final HostnameVerifier verifier;
|
private final HostnameVerifier verifier;
|
||||||
private final Field methodField;
|
private final Field methodField;
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
Supplier<SSLContext> sslContextSupplier;
|
Supplier<SSLContext> sslContextSupplier;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public JavaUrlHttpCommandExecutorService(HttpUtils utils, ContentMetadataCodec contentMetadataCodec,
|
public JavaUrlHttpCommandExecutorService(HttpUtils utils, ContentMetadataCodec contentMetadataCodec,
|
||||||
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ListeningExecutorService ioExecutor,
|
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ListeningExecutorService ioExecutor,
|
||||||
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
|
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
|
||||||
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
|
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
|
||||||
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider) throws SecurityException,
|
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider, Function<URI, Proxy> proxyForURI)
|
||||||
NoSuchFieldException {
|
throws SecurityException, NoSuchFieldException {
|
||||||
super(utils, contentMetadataCodec, ioExecutor, retryHandler, ioRetryHandler, errorHandler, wire);
|
super(utils, contentMetadataCodec, ioExecutor, retryHandler, ioRetryHandler, errorHandler, wire);
|
||||||
if (utils.getMaxConnections() > 0)
|
if (utils.getMaxConnections() > 0)
|
||||||
System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections()));
|
System.setProperty("http.maxConnections", String.valueOf(checkNotNull(utils, "utils").getMaxConnections()));
|
||||||
this.untrustedSSLContextProvider = checkNotNull(untrustedSSLContextProvider, "untrustedSSLContextProvider");
|
this.untrustedSSLContextProvider = checkNotNull(untrustedSSLContextProvider, "untrustedSSLContextProvider");
|
||||||
this.verifier = checkNotNull(verifier, "verifier");
|
this.verifier = checkNotNull(verifier, "verifier");
|
||||||
|
this.proxyForURI = checkNotNull(proxyForURI, "proxyForURI");
|
||||||
this.methodField = HttpURLConnection.class.getDeclaredField("method");
|
this.methodField = HttpURLConnection.class.getDeclaredField("method");
|
||||||
methodField.setAccessible(true);
|
methodField.setAccessible(true);
|
||||||
}
|
}
|
||||||
|
@ -159,26 +158,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
|
||||||
boolean chunked = "chunked".equals(request.getFirstHeaderOrNull("Transfer-Encoding"));
|
boolean chunked = "chunked".equals(request.getFirstHeaderOrNull("Transfer-Encoding"));
|
||||||
URL url = request.getEndpoint().toURL();
|
URL url = request.getEndpoint().toURL();
|
||||||
|
|
||||||
HttpURLConnection connection;
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxyForURI.apply(request.getEndpoint()));
|
||||||
|
|
||||||
if (utils.useSystemProxies()) {
|
|
||||||
System.setProperty("java.net.useSystemProxies", "true");
|
|
||||||
Iterable<Proxy> proxies = ProxySelector.getDefault().select(request.getEndpoint());
|
|
||||||
Proxy proxy = 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();
|
|
||||||
}
|
|
||||||
if (connection instanceof HttpsURLConnection) {
|
if (connection instanceof HttpsURLConnection) {
|
||||||
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
|
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
|
||||||
if (utils.relaxHostname())
|
if (utils.relaxHostname())
|
||||||
|
|
|
@ -18,9 +18,13 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.predicates;
|
package org.jclouds.predicates;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
@ -28,6 +32,8 @@ import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.net.HostAndPort;
|
import com.google.common.net.HostAndPort;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
@ -47,6 +53,14 @@ public class InetSocketAddressConnect implements SocketOpen {
|
||||||
@Named("org.jclouds.socket_timeout")
|
@Named("org.jclouds.socket_timeout")
|
||||||
private int timeout = 2000;
|
private int timeout = 2000;
|
||||||
|
|
||||||
|
private final Function<URI, Proxy> proxyForURI;
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
@Inject
|
||||||
|
InetSocketAddressConnect(Function<URI, Proxy> proxyForURI) {
|
||||||
|
this.proxyForURI = checkNotNull(proxyForURI, "proxyForURI");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(HostAndPort socketA) {
|
public boolean apply(HostAndPort socketA) {
|
||||||
InetSocketAddress socketAddress = new InetSocketAddress(socketA.getHostText(), socketA
|
InetSocketAddress socketAddress = new InetSocketAddress(socketA.getHostText(), socketA
|
||||||
|
@ -54,7 +68,8 @@ public class InetSocketAddressConnect implements SocketOpen {
|
||||||
Socket socket = null;
|
Socket socket = null;
|
||||||
try {
|
try {
|
||||||
logger.trace("testing socket %s", socketAddress);
|
logger.trace("testing socket %s", socketAddress);
|
||||||
socket = new Socket();
|
socket = new Socket(
|
||||||
|
proxyForURI.apply(URI.create("socket://" + socketA.getHostText() + ":" + socketA.getPort())));
|
||||||
socket.setReuseAddress(false);
|
socket.setReuseAddress(false);
|
||||||
socket.setSoLinger(false, 1);
|
socket.setSoLinger(false, 1);
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.proxy;
|
||||||
|
|
||||||
|
import java.net.Proxy.Type;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
|
import org.jclouds.proxy.internal.GuiceProxyConfig;
|
||||||
|
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.common.net.HostAndPort;
|
||||||
|
import com.google.inject.ImplementedBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parameters needed to configure {@link java.net.Proxy}. Check presence of
|
||||||
|
* {@link #getProxyHost()} to decide if proxy support should even be attempted.
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@ImplementedBy(GuiceProxyConfig.class)
|
||||||
|
public interface ProxyConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_SYSTEM
|
||||||
|
*/
|
||||||
|
boolean useSystem();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_TYPE
|
||||||
|
*/
|
||||||
|
Type getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_HOST
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_PORT
|
||||||
|
*/
|
||||||
|
Optional<HostAndPort> getProxy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_USER
|
||||||
|
* @see org.jclouds.Constants#PROPERTY_PROXY_PASSWORD
|
||||||
|
*/
|
||||||
|
Optional<Credentials> getCredentials();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.proxy;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static com.google.common.collect.Iterables.getLast;
|
||||||
|
|
||||||
|
import java.net.Authenticator;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.PasswordAuthentication;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.ProxySelector;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class ProxyForURI implements Function<URI, Proxy> {
|
||||||
|
private final ProxyConfig config;
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
@Inject
|
||||||
|
ProxyForURI(ProxyConfig config) {
|
||||||
|
this.config = checkNotNull(config, "config");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param endpoint
|
||||||
|
* <ul>
|
||||||
|
* <li>http URI for http connections</li>
|
||||||
|
* <li>https URI for https connections</li>
|
||||||
|
* <li>ftp URI for ftp connections</li>
|
||||||
|
* <li>socket://host:port for tcp client sockets connections</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Proxy apply(URI endpoint) {
|
||||||
|
if (config.useSystem()) {
|
||||||
|
System.setProperty("java.net.useSystemProxies", "true");
|
||||||
|
Iterable<Proxy> proxies = ProxySelector.getDefault().select(endpoint);
|
||||||
|
return getLast(proxies);
|
||||||
|
} else if (config.getProxy().isPresent()) {
|
||||||
|
SocketAddress addr = new InetSocketAddress(config.getProxy().get().getHostText(), config.getProxy().get()
|
||||||
|
.getPort());
|
||||||
|
Proxy proxy = new Proxy(config.getType(), addr);
|
||||||
|
|
||||||
|
final Optional<Credentials> creds = config.getCredentials();
|
||||||
|
if (creds.isPresent()) {
|
||||||
|
Authenticator authenticator = new Authenticator() {
|
||||||
|
public PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(creds.get().identity, creds.get().credential.toCharArray());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Authenticator.setDefault(authenticator);
|
||||||
|
}
|
||||||
|
return proxy;
|
||||||
|
} else {
|
||||||
|
return Proxy.NO_PROXY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,123 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.jclouds.proxy.internal;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
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_TYPE;
|
||||||
|
import static org.jclouds.Constants.PROPERTY_PROXY_USER;
|
||||||
|
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Proxy.Type;
|
||||||
|
|
||||||
|
import javax.inject.Named;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.domain.Credentials;
|
||||||
|
import org.jclouds.proxy.ProxyConfig;
|
||||||
|
|
||||||
|
import com.google.common.base.Objects;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.net.HostAndPort;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration derived from Guice properties.
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class GuiceProxyConfig implements ProxyConfig {
|
||||||
|
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_SYSTEM)
|
||||||
|
private boolean systemProxies = Boolean.parseBoolean(System.getProperty("java.net.useSystemProxies", "false"));
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_HOST)
|
||||||
|
private String host;
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_PORT)
|
||||||
|
private Integer port;
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_USER)
|
||||||
|
private String user;
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_PASSWORD)
|
||||||
|
private String password;
|
||||||
|
@Inject(optional = true)
|
||||||
|
@Named(PROPERTY_PROXY_TYPE)
|
||||||
|
private Proxy.Type type = Proxy.Type.HTTP;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<HostAndPort> getProxy() {
|
||||||
|
if (host == null)
|
||||||
|
return Optional.absent();
|
||||||
|
Integer port = this.port;
|
||||||
|
if (port == null) {
|
||||||
|
switch (type) {
|
||||||
|
case HTTP:
|
||||||
|
port = 80;
|
||||||
|
break;
|
||||||
|
case SOCKS:
|
||||||
|
port = 1080;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException(type + " not supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.of(HostAndPort.fromParts(host, port));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Credentials> getCredentials() {
|
||||||
|
if (user == null)
|
||||||
|
return Optional.absent();
|
||||||
|
return Optional.of(new Credentials(user, checkNotEmpty(password, "set property %s for user %s",
|
||||||
|
PROPERTY_PROXY_PASSWORD, user)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String checkNotEmpty(String nullableString, String message, Object... args) {
|
||||||
|
checkArgument(Strings.emptyToNull(nullableString) != null, message, args);
|
||||||
|
return nullableString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean useSystem() {
|
||||||
|
return systemProxies;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Objects.toStringHelper(this).omitNullValues().add("systemProxies", systemProxies ? "true" : null)
|
||||||
|
.add("proxy", getProxy().orNull()).add("user", user).add("type", host != null ? type : null).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -33,6 +33,7 @@ import static org.jclouds.util.Maps2.transformKeys;
|
||||||
import static org.jclouds.util.Predicates2.startsWith;
|
import static org.jclouds.util.Predicates2.startsWith;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.Proxy;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -46,6 +47,8 @@ import org.jclouds.http.functions.config.SaxParserModule;
|
||||||
import org.jclouds.internal.FilterStringsBoundToInjectorByName;
|
import org.jclouds.internal.FilterStringsBoundToInjectorByName;
|
||||||
import org.jclouds.json.config.GsonModule;
|
import org.jclouds.json.config.GsonModule;
|
||||||
import org.jclouds.location.config.LocationModule;
|
import org.jclouds.location.config.LocationModule;
|
||||||
|
import com.google.common.reflect.Invokable;
|
||||||
|
import org.jclouds.proxy.ProxyForURI;
|
||||||
import org.jclouds.rest.AuthorizationException;
|
import org.jclouds.rest.AuthorizationException;
|
||||||
import org.jclouds.rest.HttpAsyncClient;
|
import org.jclouds.rest.HttpAsyncClient;
|
||||||
import org.jclouds.rest.HttpClient;
|
import org.jclouds.rest.HttpClient;
|
||||||
|
@ -59,7 +62,6 @@ import com.google.common.cache.Cache;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.reflect.Invokable;
|
|
||||||
import com.google.common.reflect.Parameter;
|
import com.google.common.reflect.Parameter;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
|
@ -147,8 +149,8 @@ public class RestModule extends AbstractModule {
|
||||||
}).toInstance(authException);
|
}).toInstance(authException);
|
||||||
bind(new TypeLiteral<Function<Predicate<String>, Map<String, String>>>() {
|
bind(new TypeLiteral<Function<Predicate<String>, Map<String, String>>>() {
|
||||||
}).to(FilterStringsBoundToInjectorByName.class);
|
}).to(FilterStringsBoundToInjectorByName.class);
|
||||||
bind(new TypeLiteral<Function<Predicate<String>, Map<String, String>>>() {
|
bind(new TypeLiteral<Function<URI, Proxy>>() {
|
||||||
}).to(FilterStringsBoundToInjectorByName.class);
|
}).to(ProxyForURI.class);
|
||||||
installLocations();
|
installLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.http.internal;
|
package org.jclouds.http.internal;
|
||||||
|
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -37,6 +39,7 @@ import org.jclouds.http.handlers.DelegatingRetryHandler;
|
||||||
import org.jclouds.io.ContentMetadataCodec;
|
import org.jclouds.io.ContentMetadataCodec;
|
||||||
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.reflect.Invokable;
|
import com.google.common.reflect.Invokable;
|
||||||
|
@ -91,10 +94,10 @@ public class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpComman
|
||||||
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ListeningExecutorService ioExecutor,
|
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ListeningExecutorService ioExecutor,
|
||||||
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
|
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
|
||||||
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
|
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
|
||||||
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider, List<HttpCommand> commandsInvoked)
|
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider, Function<URI, Proxy> proxyForURI,
|
||||||
throws SecurityException, NoSuchFieldException {
|
List<HttpCommand> commandsInvoked) throws SecurityException, NoSuchFieldException {
|
||||||
super(utils, contentMetadataCodec, ioExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier,
|
super(utils, contentMetadataCodec, ioExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier,
|
||||||
untrustedSSLContextProvider);
|
untrustedSSLContextProvider, proxyForURI);
|
||||||
this.commandsInvoked = commandsInvoked;
|
this.commandsInvoked = commandsInvoked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ import org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService;
|
||||||
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
|
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
|
||||||
import org.jclouds.http.config.SSLModule;
|
import org.jclouds.http.config.SSLModule;
|
||||||
import org.jclouds.lifecycle.Closer;
|
import org.jclouds.lifecycle.Closer;
|
||||||
|
import org.jclouds.proxy.ProxyConfig;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
|
@ -155,9 +156,9 @@ public class ApacheHCHttpCommandExecutorServiceModule extends AbstractModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
HttpClient newDefaultHttpClient(HttpUtils utils, BasicHttpParams params, ClientConnectionManager cm) {
|
HttpClient newDefaultHttpClient(ProxyConfig config, BasicHttpParams params, ClientConnectionManager cm) {
|
||||||
DefaultHttpClient client = new DefaultHttpClient(cm, params);
|
DefaultHttpClient client = new DefaultHttpClient(cm, params);
|
||||||
if (utils.useSystemProxies()) {
|
if (config.useSystem()) {
|
||||||
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(client.getConnectionManager()
|
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(client.getConnectionManager()
|
||||||
.getSchemeRegistry(), ProxySelector.getDefault());
|
.getSchemeRegistry(), ProxySelector.getDefault());
|
||||||
client.setRoutePlanner(routePlanner);
|
client.setRoutePlanner(routePlanner);
|
||||||
|
|
Loading…
Reference in New Issue