From b99b6b1dc04290a29b8fa69b06ba5cc971f35665 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 4 Apr 2011 17:43:09 -0700 Subject: [PATCH] address auth exceptions on @Provides methods --- .../rest/internal/AsyncRestClientProxy.java | 16 +++++++--- .../internal/RestAnnotationProcessorTest.java | 18 +++++++++++ .../org/jclouds/util/Throwables2Test.java | 31 ++++++++++++------- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java b/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java index ad23311901..9fab6ba736 100755 --- a/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java +++ b/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java @@ -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 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)); diff --git a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java index 1738f1dd46..b2829efe83 100755 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -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 foo(); + @Named("exception") + @Provides + Set 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 exception() { + throw new AuthorizationException(); + } + })); injector = createContextBuilder(contextSpec).buildInjector(); diff --git a/core/src/test/java/org/jclouds/util/Throwables2Test.java b/core/src/test/java/org/jclouds/util/Throwables2Test.java index 8a8e2992ed..064edb639f 100644 --- a/core/src/test/java/org/jclouds/util/Throwables2Test.java +++ b/core/src/test/java/org/jclouds/util/Throwables2Test.java @@ -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) @@ -116,13 +115,20 @@ public class Throwables2Test { Exception e = new AuthorizationException(); 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(); returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)); } - + @Test(expectedExceptions = ResourceNotFoundException.class) public void testPropagateStandardExceptionResourceNotFoundException() throws Exception { Exception e = new ResourceNotFoundException(); @@ -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 { /**