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.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
@Singleton @Singleton
@ -107,10 +108,17 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
return this.hashCode(); return this.hashCode();
} else if (method.isAnnotationPresent(Provides.class)) { } else if (method.isAnnotationPresent(Provides.class)) {
try { try {
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent); try {
return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier)); Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
} catch (NoSuchElementException e) { return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier));
return injector.getInstance(Key.get(method.getGenericReturnType())); } 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)) { } else if (method.isAnnotationPresent(Delegate.class)) {
return delegateMap.get(new ClassMethodArgs(method.getReturnType(), method, args)); 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.io.Payloads;
import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AsyncClientFactory; import org.jclouds.rest.AsyncClientFactory;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientTest; import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.InvocationContext; import org.jclouds.rest.InvocationContext;
@ -2179,6 +2180,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Provides @Provides
Set<String> foo(); Set<String> foo();
@Named("exception")
@Provides
Set<String> exception();
@POST @POST
@Path("/") @Path("/")
void oneForm(@PathParam("bucket") String path); void oneForm(@PathParam("bucket") String path);
@ -2197,6 +2202,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(set, ImmutableSet.of("bar")); 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 @Test
public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneForm = TestClassForm.class.getMethod("oneForm", String.class); Method oneForm = TestClassForm.class.getMethod("oneForm", String.class);
@ -2252,6 +2263,13 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
URI.create("http://localhost:1111")); URI.create("http://localhost:1111"));
} }
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> exception() {
throw new AuthorizationException();
}
})); }));
injector = createContextBuilder(contextSpec).buildInjector(); injector = createContextBuilder(contextSpec).buildInjector();

View File

@ -81,10 +81,9 @@ public class Throwables2Test {
public void testReturnExceptionThatsInList() throws Exception { public void testReturnExceptionThatsInList() throws Exception {
Exception e = new TestException(); Exception e = new TestException();
assertEquals(returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class }, e), assertEquals(returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class }, e),
e); e);
assertEquals( assertEquals(returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class },
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class }, new RuntimeException(e)), e);
new RuntimeException(e)), e);
} }
@Test(expectedExceptions = TestException.class) @Test(expectedExceptions = TestException.class)
@ -116,13 +115,20 @@ public class Throwables2Test {
Exception e = new AuthorizationException(); Exception e = new AuthorizationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)); 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) @Test(expectedExceptions = InsufficientResourcesException.class)
public void testPropagateStandardExceptionInsufficientResourcesException() throws Exception { public void testPropagateStandardExceptionInsufficientResourcesException() throws Exception {
Exception e = new InsufficientResourcesException(); Exception e = new InsufficientResourcesException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)); returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e));
} }
@Test(expectedExceptions = ResourceNotFoundException.class) @Test(expectedExceptions = ResourceNotFoundException.class)
public void testPropagateStandardExceptionResourceNotFoundException() throws Exception { public void testPropagateStandardExceptionResourceNotFoundException() throws Exception {
Exception e = new ResourceNotFoundException(); Exception e = new ResourceNotFoundException();
@ -133,36 +139,36 @@ public class Throwables2Test {
public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Exception { public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Exception {
Exception e = new IllegalStateException(); Exception e = new IllegalStateException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo", returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e)); createNiceMock(HttpCommand.class), null, e));
} }
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Exception { public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Exception {
Exception e = new IllegalArgumentException(); Exception e = new IllegalArgumentException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo", returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e)); createNiceMock(HttpCommand.class), null, e));
} }
@Test(expectedExceptions = UnsupportedOperationException.class) @Test(expectedExceptions = UnsupportedOperationException.class)
public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException() public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException()
throws Exception { throws Exception {
Exception e = new UnsupportedOperationException(); Exception e = new UnsupportedOperationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo", returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e)); createNiceMock(HttpCommand.class), null, e));
} }
@Test(expectedExceptions = AuthorizationException.class) @Test(expectedExceptions = AuthorizationException.class)
public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Exception { public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Exception {
Exception e = new AuthorizationException(); Exception e = new AuthorizationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo", returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e)); createNiceMock(HttpCommand.class), null, e));
} }
@Test(expectedExceptions = ResourceNotFoundException.class) @Test(expectedExceptions = ResourceNotFoundException.class)
public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Exception { public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Exception {
Exception e = new ResourceNotFoundException(); Exception e = new ResourceNotFoundException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo", returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new HttpResponseException("goo",
createNiceMock(HttpCommand.class), null, e)); createNiceMock(HttpCommand.class), null, e));
} }
@Test(expectedExceptions = HttpResponseException.class) @Test(expectedExceptions = HttpResponseException.class)
@ -170,6 +176,7 @@ public class Throwables2Test {
Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null); Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null);
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)); returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e));
} }
static class TestException extends Exception { static class TestException extends Exception {
/** /**