Re-authenticate on CloudStack session expiration

This commit is contained in:
Andrei Savu 2012-02-05 13:15:00 +02:00 committed by Adrian Cole
parent 0134c3569b
commit 214c4630bc
2 changed files with 19 additions and 26 deletions

View File

@ -18,12 +18,9 @@
*/
package org.jclouds.cloudstack.handlers;
import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream;
import static org.jclouds.http.HttpUtils.releasePayload;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import com.google.common.cache.LoadingCache;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.features.SessionClient;
import org.jclouds.domain.Credentials;
@ -32,9 +29,10 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpRetryHandler;
import org.jclouds.logging.Logger;
import com.google.common.cache.LoadingCache;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import static org.jclouds.http.HttpUtils.releasePayload;
/**
* This will parse and set an appropriate exception on the command object.
@ -59,18 +57,14 @@ public class RetryOnRenewAndLogoutOnClose implements HttpRetryHandler {
@Override
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
boolean retry = false; // default
try {
switch (response.getStatusCode()) {
case 401:
byte[] content = closeClientButKeepContentStream(response);
if (new String(content).equals("TODO: What state can we retry?")) {
logger.debug("invalidating session");
authenticationResponseCache.invalidateAll();
retry = true;
}
authenticationResponseCache.invalidateAll();
return true;
}
return retry;
return false;
} finally {
releasePayload(response);
}

View File

@ -18,12 +18,7 @@
*/
package org.jclouds.cloudstack.handlers;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.testng.Assert.assertTrue;
import com.google.common.cache.LoadingCache;
import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.features.SessionClient;
import org.jclouds.domain.Credentials;
@ -32,11 +27,15 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.io.Payloads;
import org.testng.annotations.Test;
import com.google.common.cache.LoadingCache;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.testng.Assert.assertTrue;
/**
* Tests behavior of {@code RetryOnRenewAndLogoutOnClose} handler
*
*
* @author grkvlt@apache.org
*/
@Test(groups = "unit", testName = "RetryOnRenewAndLogoutOnCloseTest")
@ -54,7 +53,7 @@ public class RetryOnRenewAndLogoutOnCloseTest {
replay(cache, command);
HttpResponse response = HttpResponse.builder().payload(
Payloads.newStringPayload("TODO: What state can we retry?")).statusCode(401).build();
Payloads.newStringPayload("Not relevant")).statusCode(401).build();
RetryOnRenewAndLogoutOnClose retry = new RetryOnRenewAndLogoutOnClose(cache, sessionClient);