diff --git a/common/openstack/src/main/java/org/jclouds/openstack/config/OpenStackAuthenticationModule.java b/common/openstack/src/main/java/org/jclouds/openstack/config/OpenStackAuthenticationModule.java index 0d260b205b..d2c8949c26 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/config/OpenStackAuthenticationModule.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/config/OpenStackAuthenticationModule.java @@ -18,6 +18,9 @@ */ package org.jclouds.openstack.config; +import static com.google.common.base.Suppliers.memoizeWithExpiration; +import static com.google.common.base.Throwables.propagate; + import java.util.Date; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -41,12 +44,9 @@ import org.jclouds.rest.AsyncClientFactory; import com.google.common.base.Function; import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.base.Throwables; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import com.google.common.util.concurrent.UncheckedExecutionException; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.TypeLiteral; @@ -102,9 +102,14 @@ public class OpenStackAuthenticationModule extends AbstractModule { .authenticate(input.identity, input.credential); return response.get(30, TimeUnit.SECONDS); } catch (Exception e) { - throw Throwables.propagate(e); + throw propagate(e); } } + + @Override + public String toString() { + return "authenticate()"; + } }); } @@ -113,13 +118,9 @@ public class OpenStackAuthenticationModule extends AbstractModule { @Provides @Singleton public LoadingCache provideAuthenticationResponseCache2( - final Function getAuthenticationResponse, - @Provider final Credentials creds) { - - LoadingCache cache = CacheBuilder.newBuilder().expireAfterWrite(23, - TimeUnit.HOURS).build(CacheLoader.from(getAuthenticationResponse)); - - return cache; + Function getAuthenticationResponse) { + return CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS).build( + CacheLoader.from(getAuthenticationResponse)); } @Provides @@ -131,10 +132,8 @@ public class OpenStackAuthenticationModule extends AbstractModule { public AuthenticationResponse get() { try { return cache.get(creds); - } catch (UncheckedExecutionException e) { - throw Throwables.propagate(e.getCause()); } catch (ExecutionException e) { - throw Throwables.propagate(e.getCause()); + throw propagate(e.getCause()); } } }; @@ -144,7 +143,7 @@ public class OpenStackAuthenticationModule extends AbstractModule { @Singleton @TimeStamp protected Supplier provideCacheBusterDate() { - return Suppliers.memoizeWithExpiration(new Supplier() { + return memoizeWithExpiration(new Supplier() { public Date get() { return new Date(); } diff --git a/common/openstack/src/main/java/org/jclouds/openstack/handlers/RetryOnRenew.java b/common/openstack/src/main/java/org/jclouds/openstack/handlers/RetryOnRenew.java index dc0cb7e6da..87d073979d 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/handlers/RetryOnRenew.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/handlers/RetryOnRenew.java @@ -18,10 +18,9 @@ */ package org.jclouds.openstack.handlers; +import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream; import static org.jclouds.http.HttpUtils.releasePayload; -import java.io.IOException; - import javax.annotation.Resource; import org.jclouds.domain.Credentials; @@ -31,7 +30,6 @@ import org.jclouds.http.HttpRetryHandler; import org.jclouds.logging.Logger; import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; import org.jclouds.openstack.reference.AuthHeaders; -import org.jclouds.util.Strings2; import com.google.common.cache.LoadingCache; import com.google.common.collect.Multimap; @@ -49,8 +47,12 @@ public class RetryOnRenew implements HttpRetryHandler { @Resource protected Logger logger = Logger.NULL; + private final LoadingCache authenticationResponseCache; + @Inject - LoadingCache authenticationResponseCache; + protected RetryOnRenew(LoadingCache authenticationResponseCache) { + this.authenticationResponseCache = authenticationResponseCache; + } @Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { @@ -64,9 +66,9 @@ public class RetryOnRenew implements HttpRetryHandler { && headers.containsKey(AuthHeaders.AUTH_KEY) && !headers.containsKey(AuthHeaders.AUTH_TOKEN)) { retry = false; } else { - String content = parsePayloadOrNull(response); - if (content != null && content.contains("lease renew")) { - // Otherwise invalidate the token cache, to force reauthentication + byte[] content = closeClientButKeepContentStream(response); + if (content != null && new String(content).contains("lease renew")) { + logger.debug("invalidating authentication token"); authenticationResponseCache.invalidateAll(); retry = true; } else { @@ -82,14 +84,4 @@ public class RetryOnRenew implements HttpRetryHandler { } } - String parsePayloadOrNull(HttpResponse response) { - if (response.getPayload() != null) { - try { - return Strings2.toStringAndClose(response.getPayload().getInput()); - } catch (IOException e) { - logger.warn(e, "exception reading error from response", response); - } - } - return null; - } } diff --git a/common/openstack/src/test/java/org/jclouds/openstack/handlers/RetryOnRenewTest.java b/common/openstack/src/test/java/org/jclouds/openstack/handlers/RetryOnRenewTest.java index c59629cfd2..36a6c3a7d4 100644 --- a/common/openstack/src/test/java/org/jclouds/openstack/handlers/RetryOnRenewTest.java +++ b/common/openstack/src/test/java/org/jclouds/openstack/handlers/RetryOnRenewTest.java @@ -62,8 +62,7 @@ public class RetryOnRenewTest { replay(response); replay(cache); - RetryOnRenew retry = new RetryOnRenew(); - retry.authenticationResponseCache = cache; + RetryOnRenew retry = new RetryOnRenew(cache); assertTrue(retry.shouldRetryRequest(command, response));