Repository: jclouds
Updated Branches:
  refs/heads/master 112c64e92 -> 040df11d3


JCLOUDS-1116: Delegate endpoint should be preferred to the caller's one


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/040df11d
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/040df11d
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/040df11d

Branch: refs/heads/master
Commit: 040df11d3f791659ff36722fb99d91c501bfa4a7
Parents: 112c64e
Author: Ignasi Barrera <[email protected]>
Authored: Fri Jun 17 00:30:40 2016 +0200
Committer: Ignasi Barrera <[email protected]>
Committed: Tue Jun 21 09:40:53 2016 +0200

----------------------------------------------------------------------
 .../rest/internal/RestAnnotationProcessor.java  | 21 ++++++---
 .../internal/RestAnnotationProcessorTest.java   | 48 ++++++++++++++++++++
 2 files changed, 62 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/040df11d/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java 
b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java
index efb7753..010f3b6 100644
--- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java
+++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java
@@ -198,14 +198,21 @@ public class RestAnnotationProcessor implements 
Function<Invocation, HttpRequest
          endpoint = Optional.fromNullable(r.getEndpoint());
          if (endpoint.isPresent())
             logger.trace("using endpoint %s from invocation.getArgs() for %s", 
endpoint, invocation);
-      } else if (caller != null) {
-         endpoint = getEndpointFor(caller);
-         if (endpoint.isPresent())
-            logger.trace("using endpoint %s from caller %s for %s", endpoint, 
caller, invocation);
-         else
-            endpoint = findEndpoint(invocation);
       } else {
-         endpoint = findEndpoint(invocation);
+         // If there is no explicit HttpRequest parameter, try to find the 
endpoint. When using
+         // delegate apis, the endpoint defined in the callee takes precedence
+         endpoint = getEndpointFor(invocation);
+         if (!endpoint.isPresent()) {
+            if (caller != null) {
+               endpoint = getEndpointFor(caller);
+               if (endpoint.isPresent())
+                  logger.trace("using endpoint %s from caller %s for %s", 
endpoint, caller, invocation);
+               else
+                  endpoint = findEndpoint(invocation);
+            } else {
+               endpoint = findEndpoint(invocation);
+            }
+         }
       }
 
       if (!endpoint.isPresent())

http://git-wip-us.apache.org/repos/asf/jclouds/blob/040df11d/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java
----------------------------------------------------------------------
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 b917d5b..1869f6a 100644
--- 
a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java
+++ 
b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java
@@ -20,6 +20,7 @@ import static com.google.common.base.Charsets.UTF_8;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 import static javax.ws.rs.core.MediaType.APPLICATION_XML;
+import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
 import static org.jclouds.io.Payloads.newInputStreamPayload;
 import static org.jclouds.io.Payloads.newStringPayload;
 import static org.jclouds.providers.AnonymousProviderMetadata.forApiOnEndpoint;
@@ -185,6 +186,13 @@ public class RestAnnotationProcessorTest extends 
BaseRestApiTest {
       @Produces(APPLICATION_XML)
       @Consumes(APPLICATION_XML)
       void testProducesAndConsumesOnMethod();
+
+      @GET
+      void testWithEndpointParam(@EndpointParam URI endpoint);
+
+      @GET
+      @Endpoint(Localhost2.class)
+      void testWithEndpoint();
    }
 
    @Path("/client/{jclouds.api-version}")
@@ -411,6 +419,46 @@ public class RestAnnotationProcessorTest extends 
BaseRestApiTest {
 
    }
 
+   public void 
testDelegateIsLazyLoadedAndRequestIncludesEndpointParamFromCallee()
+         throws InterruptedException, ExecutionException {
+      Injector child = injectorForCaller(new HttpCommandExecutorService() {
+         @Override
+         public HttpResponse invoke(HttpCommand command) {
+            assertEquals(command.getCurrentRequest().getRequestLine(), "GET 
http://foo/bar/client/1 HTTP/1.1");
+            return HttpResponse.builder().build();
+         }
+      });
+
+      try {
+         child.getInstance(Callee.class);
+         failBecauseExceptionWasNotThrown(ConfigurationException.class);
+      } catch (ConfigurationException e) {
+
+      }
+
+      
child.getInstance(Caller.class).getCallee(URI.create("http://howdyboys";)).testWithEndpointParam(URI.create("http://foo/bar";));
+   }
+
+   public void testDelegateIsLazyLoadedAndRequestIncludesEndpointFromCallee()
+         throws InterruptedException, ExecutionException {
+      Injector child = injectorForCaller(new HttpCommandExecutorService() {
+         @Override
+         public HttpResponse invoke(HttpCommand command) {
+            assertEquals(command.getCurrentRequest().getRequestLine(), "GET 
http://localhost:1111/client/1 HTTP/1.1");
+            return HttpResponse.builder().build();
+         }
+      });
+
+      try {
+         child.getInstance(Callee.class);
+         failBecauseExceptionWasNotThrown(ConfigurationException.class);
+      } catch (ConfigurationException e) {
+
+      }
+
+      
child.getInstance(Caller.class).getCallee(URI.create("http://howdyboys";)).testWithEndpoint();
+   }
+
    public void 
testDelegateIsLazyLoadedAndRequestIncludesEndpointVersionAndPath() throws 
InterruptedException,
          ExecutionException {
       Injector child = injectorForCaller(new HttpCommandExecutorService() {

Reply via email to