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

This commit is contained in:
Ignasi Barrera 2016-06-17 00:30:40 +02:00
parent 112c64e92a
commit 040df11d3f
2 changed files with 62 additions and 7 deletions

View File

@ -198,14 +198,21 @@ public class RestAnnotationProcessor implements Function<Invocation, HttpRequest
endpoint = Optional.fromNullable(r.getEndpoint()); endpoint = Optional.fromNullable(r.getEndpoint());
if (endpoint.isPresent()) if (endpoint.isPresent())
logger.trace("using endpoint %s from invocation.getArgs() for %s", endpoint, invocation); 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 { } 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()) if (!endpoint.isPresent())

View File

@ -20,6 +20,7 @@ import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull; 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_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_XML; 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.newInputStreamPayload;
import static org.jclouds.io.Payloads.newStringPayload; import static org.jclouds.io.Payloads.newStringPayload;
import static org.jclouds.providers.AnonymousProviderMetadata.forApiOnEndpoint; import static org.jclouds.providers.AnonymousProviderMetadata.forApiOnEndpoint;
@ -185,6 +186,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
@Produces(APPLICATION_XML) @Produces(APPLICATION_XML)
@Consumes(APPLICATION_XML) @Consumes(APPLICATION_XML)
void testProducesAndConsumesOnMethod(); void testProducesAndConsumesOnMethod();
@GET
void testWithEndpointParam(@EndpointParam URI endpoint);
@GET
@Endpoint(Localhost2.class)
void testWithEndpoint();
} }
@Path("/client/{jclouds.api-version}") @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, public void testDelegateIsLazyLoadedAndRequestIncludesEndpointVersionAndPath() throws InterruptedException,
ExecutionException { ExecutionException {
Injector child = injectorForCaller(new HttpCommandExecutorService() { Injector child = injectorForCaller(new HttpCommandExecutorService() {