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
@ -106,12 +107,19 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
} else if (method.getName().equals("hashCode")) { } else if (method.getName().equals("hashCode")) {
return this.hashCode(); return this.hashCode();
} else if (method.isAnnotationPresent(Provides.class)) { } else if (method.isAnnotationPresent(Provides.class)) {
try {
try { try {
Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent); Annotation qualifier = Iterables.find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent);
return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier)); return injector.getInstance(Key.get(method.getGenericReturnType(), qualifier));
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
return injector.getInstance(Key.get(method.getGenericReturnType())); 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));
} else if (annotationProcessor.getDelegateOrNull(method) != null } else if (annotationProcessor.getDelegateOrNull(method) != null

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

@ -82,8 +82,7 @@ public class Throwables2Test {
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);
} }
@ -117,6 +116,13 @@ public class Throwables2Test {
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();
@ -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 {
/** /**