address auth exceptions on @Provides methods

This commit is contained in:
Adrian Cole 2011-04-04 17:43:09 -07:00
parent ab8a7e452c
commit b99b6b1dc0
3 changed files with 49 additions and 16 deletions

View File

@ -58,6 +58,7 @@ import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import com.google.inject.TypeLiteral;
@Singleton
@ -107,10 +108,17 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
return this.hashCode();
} else if (method.isAnnotationPresent(Provides.class)) {
try {
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier));
} catch (NoSuchElementException e) {
return injector.getInstance(Key.get(method.getGenericReturnType()));
try {
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier));
} catch (NoSuchElementException e) {
return injector.getInstance(Key.get(method.getGenericReturnType()));
}
} catch (ProvisionException e) {
AuthorizationException aex = Throwables2.getFirstThrowableOfType(e, AuthorizationException.class);
if (aex != null)
throw aex;
throw e;
}
} else if (method.isAnnotationPresent(Delegate.class)) {
return delegateMap.get(new ClassMethodArgs(method.getReturnType(), method, args));

View File

@ -108,6 +108,7 @@ import org.jclouds.io.PayloadEnclosing;
import org.jclouds.io.Payloads;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AsyncClientFactory;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.InvocationContext;
@ -2179,6 +2180,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Provides
Set<String> foo();
@Named("exception")
@Provides
Set<String> exception();
@POST
@Path("/")
void oneForm(@PathParam("bucket") String path);
@ -2197,6 +2202,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(set, ImmutableSet.of("bar"));
}
@Test(expectedExceptions = AuthorizationException.class)
public void testProvidesWithGenericQualifiedAuthorizationException() throws SecurityException,
NoSuchMethodException, UnsupportedEncodingException {
injector.getInstance(AsyncClientFactory.class).create(TestClassForm.class).exception();
}
@Test
public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneForm = TestClassForm.class.getMethod("oneForm", String.class);
@ -2252,6 +2263,13 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
URI.create("http://localhost:1111"));
}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> exception() {
throw new AuthorizationException();
}
}));
injector = createContextBuilder(contextSpec).buildInjector();

View File

@ -81,10 +81,9 @@ public class Throwables2Test {
public void testReturnExceptionThatsInList() throws Exception {
Exception e = new TestException();
assertEquals(returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class }, e),
e);
assertEquals(
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class },
new RuntimeException(e)), e);
e);
assertEquals(returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class },
new RuntimeException(e)), e);
}
@Test(expectedExceptions = TestException.class)
@ -117,6 +116,13 @@ public class Throwables2Test {
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e));
}
@Test(expectedExceptions = AuthorizationException.class)
public void testPropagateProvisionExceptionAuthorizationException() throws Exception {
Exception e = new AuthorizationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new ProvisionException(ImmutableSet.of(new Message(
ImmutableList.of(), "Error in custom provider",e))));
}
@Test(expectedExceptions = InsufficientResourcesException.class)
public void testPropagateStandardExceptionInsufficientResourcesException() throws Exception {
Exception e = new InsufficientResourcesException();
@ -133,36 +139,36 @@ public class Throwables2Test {
public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Exception {
Exception e = new IllegalStateException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e));
createNiceMock(HttpCommand.class), null, e));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Exception {
Exception e = new IllegalArgumentException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e));
createNiceMock(HttpCommand.class), null, e));
}
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException()
throws Exception {
throws Exception {
Exception e = new UnsupportedOperationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e));
createNiceMock(HttpCommand.class), null, e));
}
@Test(expectedExceptions = AuthorizationException.class)
public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Exception {
Exception e = new AuthorizationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e));
createNiceMock(HttpCommand.class), null, e));
}
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Exception {
Exception e = new ResourceNotFoundException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e));
createNiceMock(HttpCommand.class), null, e));
}
@Test(expectedExceptions = HttpResponseException.class)
@ -170,6 +176,7 @@ public class Throwables2Test {
Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null);
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e));
}
static class TestException extends Exception {
/**