diff --git a/core/src/main/java/org/jclouds/http/config/JavaUrlHttpCommandExecutorServiceModule.java b/core/src/main/java/org/jclouds/http/config/JavaUrlHttpCommandExecutorServiceModule.java index ff1facf787..9925585cf3 100644 --- a/core/src/main/java/org/jclouds/http/config/JavaUrlHttpCommandExecutorServiceModule.java +++ b/core/src/main/java/org/jclouds/http/config/JavaUrlHttpCommandExecutorServiceModule.java @@ -19,14 +19,12 @@ package org.jclouds.http.config; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.Map; import javax.annotation.Resource; -import javax.inject.Named; +import javax.inject.Inject; import javax.inject.Singleton; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; @@ -40,10 +38,13 @@ import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl; import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService; import org.jclouds.logging.Logger; +import com.google.common.base.Supplier; +import com.google.common.base.Throwables; import com.google.common.collect.Maps; import com.google.inject.AbstractModule; -import com.google.inject.Provides; import com.google.inject.Scopes; +import com.google.inject.TypeLiteral; +import com.google.inject.name.Names; /** * Configures {@link JavaUrlHttpCommandExecutorService}. @@ -65,6 +66,9 @@ public class JavaUrlHttpCommandExecutorServiceModule extends AbstractModule { bind(HostnameVerifier.class).to(LogToMapHostnameVerifier.class); bind(TransformingHttpCommandExecutorService.class).to(TransformingHttpCommandExecutorServiceImpl.class).in( Scopes.SINGLETON); + bind(new TypeLiteral>() { + }).annotatedWith(Names.named("untrusted")).to(new TypeLiteral() { + }); } /** @@ -86,14 +90,28 @@ public class JavaUrlHttpCommandExecutorServiceModule extends AbstractModule { } } - @Provides @Singleton - @Named("untrusted") - SSLContext provideUntrustedSSLContext(TrustAllCerts trustAllCerts) throws NoSuchAlgorithmException, - KeyManagementException { - SSLContext sc = SSLContext.getInstance("SSL"); - sc.init(null, new TrustManager[] { trustAllCerts }, new SecureRandom()); - return sc; + public static class UntrustedSSLContextSupplier implements Supplier { + private final TrustAllCerts trustAllCerts; + + @Inject + UntrustedSSLContextSupplier(TrustAllCerts trustAllCerts) { + this.trustAllCerts = trustAllCerts; + } + + @Override + public SSLContext get() { + try { + SSLContext sc; + sc = SSLContext.getInstance("SSL"); + sc.init(null, new TrustManager[] { trustAllCerts }, new SecureRandom()); + return sc; + } catch (Exception e) { + Throwables.propagate(e); + return null; + } + + } } /** 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 4eb68d199c..6b1f228a48 100644 --- a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java +++ b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java @@ -44,7 +44,6 @@ import java.util.concurrent.ExecutorService; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; -import javax.inject.Provider; import javax.inject.Singleton; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -64,6 +63,7 @@ import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.logging.Logger; +import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; @@ -79,7 +79,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe public static final String USER_AGENT = "jclouds/1.0 java/" + System.getProperty("java.version"); @Resource protected Logger logger = Logger.NULL; - private final Provider untrustedSSLContextProvider; + private final Supplier untrustedSSLContextProvider; private final HostnameVerifier verifier; private final Field methodField; @@ -88,7 +88,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler, DelegatingErrorHandler errorHandler, HttpWire wire, HostnameVerifier verifier, - @Named("untrusted") Provider untrustedSSLContextProvider) throws SecurityException, + @Named("untrusted") Supplier untrustedSSLContextProvider) throws SecurityException, NoSuchFieldException { super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire); if (utils.getMaxConnections() > 0) diff --git a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java index 9dd25c8f92..26d9186bbd 100644 --- a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java +++ b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java @@ -51,6 +51,7 @@ import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; import com.google.inject.Key; import com.google.inject.TypeLiteral; @@ -119,7 +120,7 @@ public class BackoffLimitedRetryHandlerTest { public boolean verify(String hostname, SSLSession session) { return false; } - }, new Provider() { + }, new Supplier() { @Override public SSLContext get() {