diff --git a/apis/atmos/src/main/java/org/jclouds/atmos/blobstore/AtmosBlobRequestSigner.java b/apis/atmos/src/main/java/org/jclouds/atmos/blobstore/AtmosBlobRequestSigner.java index 711b859842..057957d8e6 100644 --- a/apis/atmos/src/main/java/org/jclouds/atmos/blobstore/AtmosBlobRequestSigner.java +++ b/apis/atmos/src/main/java/org/jclouds/atmos/blobstore/AtmosBlobRequestSigner.java @@ -33,6 +33,7 @@ import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.functions.BlobToHttpGetOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.options.GetOptions; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.RestAnnotationProcessor; import com.google.common.collect.ImmutableList; @@ -53,9 +54,9 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner { private final Invokable createMethod; @Inject - public AtmosBlobRequestSigner(RestAnnotationProcessor.Factory processor, BlobToObject blobToObject, + public AtmosBlobRequestSigner(RestAnnotationProcessor processor, BlobToObject blobToObject, BlobToHttpGetOptions blob2ObjectGetOptions) throws SecurityException, NoSuchMethodException { - this.processor = checkNotNull(processor, "processor").declaring(AtmosAsyncClient.class); + this.processor = checkNotNull(processor, "processor"); this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blob2ObjectGetOptions = checkNotNull(blob2ObjectGetOptions, "blob2ObjectGetOptions"); this.getMethod = Invokable.from(AtmosAsyncClient.class.getMethod("readFile", String.class, GetOptions[].class)); @@ -68,7 +69,8 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner { public HttpRequest signGetBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList. of(getPath(container, name)))); + return cleanRequest(processor.apply(Invocation.create(getMethod, + ImmutableList. of(getPath(container, name))))); } @Override @@ -80,8 +82,8 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner { public HttpRequest signPutBlob(String container, Blob blob) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - return cleanRequest(processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob)))); + return cleanRequest(processor.apply(Invocation.create(createMethod, + ImmutableList. of(container, blobToObject.apply(blob))))); } @Override @@ -93,7 +95,8 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner { public HttpRequest signRemoveBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(deleteMethod, ImmutableList. of(getPath(container, name)))); + return cleanRequest(processor.apply(Invocation.create(deleteMethod, + ImmutableList. of(getPath(container, name))))); } private String getPath(String container, String name) { @@ -104,8 +107,8 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner { public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, - ImmutableList.of(getPath(container, name), blob2ObjectGetOptions.apply(checkNotNull(options, "options"))))); + return cleanRequest(processor.apply(Invocation.create(getMethod, + ImmutableList.of(getPath(container, name), blob2ObjectGetOptions.apply(checkNotNull(options, "options")))))); } } diff --git a/apis/atmos/src/main/java/org/jclouds/atmos/functions/ParseObjectFromHeadersAndHttpContent.java b/apis/atmos/src/main/java/org/jclouds/atmos/functions/ParseObjectFromHeadersAndHttpContent.java index 61fa2608ab..07625faa93 100644 --- a/apis/atmos/src/main/java/org/jclouds/atmos/functions/ParseObjectFromHeadersAndHttpContent.java +++ b/apis/atmos/src/main/java/org/jclouds/atmos/functions/ParseObjectFromHeadersAndHttpContent.java @@ -79,7 +79,7 @@ public class ParseObjectFromHeadersAndHttpContent implements Functionof("name", "newdrive"), ImmutableList. of(), - "name newdrive"); + String expected = "name newdrive"; + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); + + Map map = ImmutableMap. of("name", "newdrive"); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testWithSize() throws IOException { - assertInputAndArgsCreatesPayload(ImmutableMap.of("name", "newdrive"), - ImmutableList. of(new CloneDriveOptions().size(1024)), "name newdrive\nsize 1024"); + String expected = "name newdrive\nsize 1024"; + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(new CloneDriveOptions().size(1024))); + + Map map = ImmutableMap. of("name", "newdrive"); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } - protected void assertInputAndArgsCreatesPayload(ImmutableMap inputMap, List args, - String expected) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(args).atLeastOnce(); - request.setPayload(expected); - Payload payload = createMock(Payload.class); - expect(request.getPayload()).andReturn(payload); - MutableContentMetadata md = createMock(MutableContentMetadata.class); - expect(payload.getContentMetadata()).andReturn(md); - md.setContentType("text/plain"); - - replay(request); - replay(payload); - replay(md); - - binder.bindToRequest(request, inputMap); - - verify(request); - verify(payload); - verify(md); + protected GeneratedHttpRequest requestForArgs(List args) { + try { + Invocation invocation = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), args); + return GeneratedHttpRequest.builder().method("POST").endpoint(URI.create("http://localhost/key")) + .invocation(invocation).build(); + } catch (SecurityException e) { + throw Throwables.propagate(e); + } catch (NoSuchMethodException e) { + throw Throwables.propagate(e); + } } - } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java index 12eac1c0bd..8b99db8ed9 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java @@ -187,7 +187,7 @@ public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedReq } public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest gRequest, Set zones) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof String) { String zone = (String) arg; if (zones.contains(zone)) diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/AttachmentHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/AttachmentHandlerTest.java index 74a292de84..9f708516c1 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/AttachmentHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/AttachmentHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,11 +25,8 @@ import java.io.InputStream; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.Attachment; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; - /** * Tests behavior of {@code AttachmentHandler} * @@ -56,9 +50,6 @@ public class AttachmentHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/BundleTaskHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/BundleTaskHandlerTest.java index 0acd5bd465..e68053ea1b 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/BundleTaskHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/BundleTaskHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,11 +25,8 @@ import java.io.InputStream; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.BundleTask; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; - /** * Tests behavior of {@code BundleTaskHandler} * @@ -71,9 +65,6 @@ public class BundleTaskHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/CreateVolumeResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/CreateVolumeResponseHandlerTest.java index 7fa28abc1d..0d2831d758 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/CreateVolumeResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/CreateVolumeResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -30,10 +27,8 @@ import org.jclouds.date.DateService; import org.jclouds.ec2.domain.Attachment; import org.jclouds.ec2.domain.Volume; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; /** @@ -62,9 +57,6 @@ public class CreateVolumeResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandlerTest.java index c246eeddb9..1ba5909ecb 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -29,7 +26,6 @@ import java.util.Set; import org.jclouds.ec2.domain.PublicIpInstanceIdPair; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; @@ -58,9 +54,6 @@ public class DescribeAddressesResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeBundleTasksResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeBundleTasksResponseHandlerTest.java index 956b805ef4..924b1ccd58 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeBundleTasksResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeBundleTasksResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,10 +25,8 @@ import java.io.InputStream; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.BundleTask; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; /** @@ -58,9 +53,6 @@ public class DescribeBundleTasksResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandlerTest.java index cbf0dd15b3..191c567a99 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandlerTest.java @@ -17,10 +17,6 @@ * under the License. */ package org.jclouds.ec2.xml; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,10 +24,8 @@ import java.util.Set; import org.jclouds.ec2.domain.KeyPair; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; /** @@ -56,9 +50,6 @@ public class DescribeKeyPairsResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandlerTest.java index b7bd44f62c..819de7c94c 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -30,10 +27,8 @@ import org.jclouds.ec2.domain.IpPermission; import org.jclouds.ec2.domain.IpProtocol; import org.jclouds.ec2.domain.SecurityGroup; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.LinkedHashMultimap; @@ -91,9 +86,6 @@ public class DescribeSecurityGroupsResponseHandlerTest extends BaseEC2HandlerTes } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSnapshotsResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSnapshotsResponseHandlerTest.java index 4965a4d77c..57ba37b273 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSnapshotsResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeSnapshotsResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -29,10 +26,8 @@ import java.util.Set; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.Snapshot; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; /** @@ -61,9 +56,6 @@ public class DescribeSnapshotsResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeVolumesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeVolumesResponseHandlerTest.java index e5bebdc791..b000b8dd73 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeVolumesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeVolumesResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -30,10 +27,8 @@ import org.jclouds.date.DateService; import org.jclouds.ec2.domain.Attachment; import org.jclouds.ec2.domain.Volume; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; /** @@ -69,9 +64,6 @@ public class DescribeVolumesResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/InstanceStateChangeHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/InstanceStateChangeHandlerTest.java index 08da2ad697..f90c1ca1ca 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/InstanceStateChangeHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/InstanceStateChangeHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -30,11 +27,9 @@ import org.jclouds.date.DateService; import org.jclouds.ec2.domain.InstanceState; import org.jclouds.ec2.domain.InstanceStateChange; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; /** @@ -95,9 +90,6 @@ public class InstanceStateChangeHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/KeyPairResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/KeyPairResponseHandlerTest.java index 53946b1e71..7ef77d2af9 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/KeyPairResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/KeyPairResponseHandlerTest.java @@ -17,10 +17,6 @@ * under the License. */ package org.jclouds.ec2.xml; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.IOException; @@ -29,11 +25,8 @@ import java.io.InputStream; import org.jclouds.crypto.SshKeys; import org.jclouds.ec2.domain.KeyPair; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; - /** * Tests behavior of {@code KeyPairResponseHandler} * @@ -84,9 +77,6 @@ public class KeyPairResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/RunInstancesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/RunInstancesResponseHandlerTest.java index 9b34e36d46..80bd009399 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/RunInstancesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/RunInstancesResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -32,11 +29,9 @@ import org.jclouds.ec2.domain.Reservation; import org.jclouds.ec2.domain.RootDeviceType; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; /** @@ -112,9 +107,6 @@ public class RunInstancesResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/SnapshotHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/SnapshotHandlerTest.java index 465c919216..6c3ae04924 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/SnapshotHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/SnapshotHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,11 +25,8 @@ import java.io.InputStream; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.Snapshot; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; - /** * Tests behavior of {@code SnapshotHandler} * @@ -56,9 +50,6 @@ public class SnapshotHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java index f8f162269d..6797eb4de2 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/binders/BindAuthToJsonPayload.java @@ -20,7 +20,6 @@ package org.jclouds.openstack.keystone.v2_0.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.util.Map; @@ -37,8 +36,8 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import com.google.common.base.Predicates; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.collect.Iterables; /** * @@ -58,7 +57,7 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde } protected void addCredentialsInArgsOrNull(GeneratedHttpRequest gRequest, Builder builder) { - for (Object arg : Iterables.filter(gRequest.getArgs(), Predicates.notNull())) { + for (Object arg : Iterables.filter(gRequest.getInvocation().getArgs(), Predicates.notNull())) { if (arg.getClass().isAnnotationPresent(CredentialType.class)) { builder.put(arg.getClass().getAnnotation(CredentialType.class).value(), arg); } @@ -70,7 +69,6 @@ public class BindAuthToJsonPayload extends BindToJsonPayload implements MapBinde checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); Builder builder = ImmutableMap.builder(); addCredentialsInArgsOrNull(gRequest, builder); diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSet.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSet.java index 163929b9a0..1520266368 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSet.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSet.java @@ -19,26 +19,28 @@ package org.jclouds.openstack.v2_0.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.Iterables.any; +import static org.jclouds.openstack.v2_0.predicates.ExtensionPredicates.namespaceOrAliasEquals; +import static org.jclouds.util.Optionals2.unwrapIfOptional; import java.net.URI; +import java.util.List; import java.util.Set; import javax.inject.Inject; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; import org.jclouds.openstack.v2_0.domain.Extension; -import org.jclouds.openstack.v2_0.predicates.ExtensionPredicates; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.functions.ImplicitOptionalConverter; import com.google.common.base.Optional; import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; /** - * We use the annotation {@link org.jclouds.openstack.services.Extension} to - * bind a class that implements an extension API to an {@link Extension}. + * We use the annotation {@link org.jclouds.openstack.services.Extension} to bind a class that implements an extension + * API to an {@link Extension}. * * @author Adrian Cole * @@ -47,36 +49,36 @@ public class PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensio ImplicitOptionalConverter { private final LoadingCache> extensions; private final Multimap aliases; - + @Inject public PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSet( - LoadingCache> extensions, - Multimap aliases) { + LoadingCache> extensions, Multimap aliases) { this.extensions = checkNotNull(extensions, "extensions"); - this.aliases = aliases == null ? ImmutableMultimap.of() : ImmutableMultimap.copyOf(aliases); + this.aliases = aliases == null ? ImmutableMultimap. of() : ImmutableMultimap.copyOf(aliases); } @Override - public Optional apply(ClassInvokerArgsAndReturnVal input) { - Optional ext = Optional.fromNullable(input.getClazz().getAnnotation( - org.jclouds.openstack.v2_0.services.Extension.class)); + public Optional apply(InvocationSuccess input) { + Class target = unwrapIfOptional(input.getInvocation().getInvokable().getReturnType()); + Optional ext = Optional.fromNullable(target + .getAnnotation(org.jclouds.openstack.v2_0.services.Extension.class)); if (ext.isPresent()) { URI namespace = URI.create(ext.get().namespace()); - if (input.getArgs().isEmpty()) { - if (Iterables.any(extensions.getUnchecked(""), - ExtensionPredicates.namespaceOrAliasEquals(namespace, aliases.get(namespace)))) - return Optional.of(input.getReturnVal()); - } else if (input.getArgs().size() == 1) { - if (Iterables.any(extensions.getUnchecked(checkNotNull(input.getArgs().get(0), "arg[0] in %s", input).toString()), - ExtensionPredicates.namespaceOrAliasEquals(namespace, aliases.get(namespace)))) - return Optional.of(input.getReturnVal()); + List args = input.getInvocation().getArgs(); + if (args.isEmpty()) { + if (any(extensions.getUnchecked(""), namespaceOrAliasEquals(namespace, aliases.get(namespace)))) + return input.getResult(); + } else if (args.size() == 1) { + String arg0 = checkNotNull(args.get(0), "arg[0] in %s", input).toString(); + if (any(extensions.getUnchecked(arg0), namespaceOrAliasEquals(namespace, aliases.get(namespace)))) + return input.getResult(); } else { throw new RuntimeException(String.format("expecting zero or one args %s", input)); } return Optional.absent(); } else { // No extension annotation, should check whether to return absent - return Optional.of(input.getReturnVal()); + return input.getResult(); } } diff --git a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSetTest.java b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSetTest.java index b2d5776d83..4fbead7325 100644 --- a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSetTest.java +++ b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/v2_0/functions/PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensionsSetTest.java @@ -3,12 +3,14 @@ package org.jclouds.openstack.v2_0.functions; import static org.testng.Assert.assertEquals; import java.net.URI; +import java.util.List; import java.util.Set; import org.jclouds.date.internal.SimpleDateFormatDateService; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; import org.jclouds.openstack.v2_0.ServiceType; import org.jclouds.openstack.v2_0.domain.Extension; +import org.jclouds.reflect.Invocation; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.annotations.Delegate; import org.testng.annotations.Test; @@ -63,34 +65,31 @@ public class PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensio } - ClassInvokerArgsAndReturnVal getFloatingIPExtension() throws SecurityException, NoSuchMethodException { - return ClassInvokerArgsAndReturnVal - .builder() - .clazz(FloatingIPAsyncApi.class) - .invoker( - Invokable.from(NovaAsyncApi.class.getDeclaredMethod("getFloatingIPExtensionForZone", String.class))) - .args(ImmutableList. of("zone")).returnVal("foo").build(); + InvocationSuccess getFloatingIPExtension(List args) throws SecurityException, NoSuchMethodException { + return InvocationSuccess.create(Invocation.create( + Invokable.from(NovaAsyncApi.class.getDeclaredMethod("getFloatingIPExtensionForZone", String.class)), + args), "foo"); } - ClassInvokerArgsAndReturnVal getKeyPairExtension() throws SecurityException, NoSuchMethodException { - return ClassInvokerArgsAndReturnVal.builder().clazz(KeyPairAsyncApi.class) - .invoker(Invokable.from(NovaAsyncApi.class.getDeclaredMethod("getKeyPairExtensionForZone", String.class))) - .args(ImmutableList. of("zone")).returnVal("foo").build(); + InvocationSuccess getKeyPairExtension(List args) throws SecurityException, NoSuchMethodException { + return InvocationSuccess.create(Invocation.create( + Invokable.from(NovaAsyncApi.class.getDeclaredMethod("getKeyPairExtensionForZone", String.class)), + args), "foo"); } public void testPresentWhenExtensionsIncludeNamespaceFromAnnotationAbsentWhenNot() throws SecurityException, NoSuchMethodException { - assertEquals(whenExtensionsInZoneInclude("zone", keypairs, floatingIps).apply(getFloatingIPExtension()), Optional.of("foo")); - assertEquals(whenExtensionsInZoneInclude("zone", keypairs, floatingIps).apply(getKeyPairExtension()), Optional.of("foo")); - assertEquals(whenExtensionsInZoneInclude("zone", keypairs).apply(getFloatingIPExtension()), Optional.absent()); - assertEquals(whenExtensionsInZoneInclude("zone", floatingIps).apply(getKeyPairExtension()), Optional.absent()); + assertEquals(whenExtensionsInZoneInclude("zone", keypairs, floatingIps).apply(getFloatingIPExtension(ImmutableList. of("zone"))), Optional.of("foo")); + assertEquals(whenExtensionsInZoneInclude("zone", keypairs, floatingIps).apply(getKeyPairExtension(ImmutableList. of("zone"))), Optional.of("foo")); + assertEquals(whenExtensionsInZoneInclude("zone", keypairs).apply(getFloatingIPExtension(ImmutableList. of("zone"))), Optional.absent()); + assertEquals(whenExtensionsInZoneInclude("zone", floatingIps).apply(getKeyPairExtension(ImmutableList. of("zone"))), Optional.absent()); } public void testZoneWithoutExtensionsReturnsAbsent() throws SecurityException, NoSuchMethodException { assertEquals(whenExtensionsInZoneInclude("zone", floatingIps).apply( - getFloatingIPExtension().toBuilder().args(ImmutableList. of("differentzone")).build()), Optional.absent()); + getFloatingIPExtension(ImmutableList. of("differentzone"))), Optional.absent()); assertEquals(whenExtensionsInZoneInclude("zone", keypairs).apply( - getKeyPairExtension().toBuilder().args(ImmutableList. of("differentzone")).build()), Optional.absent()); + getKeyPairExtension(ImmutableList. of("differentzone"))), Optional.absent()); } /** @@ -107,9 +106,9 @@ public class PresentWhenExtensionAnnotationNamespaceEqualsAnyNamespaceInExtensio .getNamespace()); assertEquals(whenExtensionsAndAliasesInZoneInclude("zone", ImmutableSet.of(keypairsWithDifferentNamespace), aliases).apply( - getKeyPairExtension()), Optional.of("foo")); + getKeyPairExtension(ImmutableList. of("zone"))), Optional.of("foo")); assertEquals(whenExtensionsAndAliasesInZoneInclude("zone", ImmutableSet.of(keypairsWithDifferentNamespace), aliases).apply( - getFloatingIPExtension()), Optional.absent()); + getFloatingIPExtension(ImmutableList. of("zone"))), Optional.absent()); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindSecurityGroupRuleToJsonPayload.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindSecurityGroupRuleToJsonPayload.java index 06559b8016..161506341c 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindSecurityGroupRuleToJsonPayload.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindSecurityGroupRuleToJsonPayload.java @@ -20,6 +20,8 @@ package org.jclouds.openstack.nova.v2_0.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Predicates.instanceOf; +import static com.google.common.collect.Iterables.find; import java.util.Map; @@ -33,9 +35,7 @@ import org.jclouds.rest.MapBinder; import org.jclouds.rest.binders.BindToJsonPayload; import org.jclouds.rest.internal.GeneratedHttpRequest; -import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; import com.google.common.collect.ImmutableMap.Builder; /** @@ -63,7 +63,7 @@ public class BindSecurityGroupRuleToJsonPayload extends BindToJsonPayload implem "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - Ingress ingress = Ingress.class.cast(Iterables.find(gRequest.getArgs(), Predicates.instanceOf(Ingress.class))); + Ingress ingress = Ingress.class.cast(find(gRequest.getInvocation().getArgs(), instanceOf(Ingress.class))); payload.put("ip_protocol", ingress.getIpProtocol().toString()); payload.put("from_port", ingress.getFromPort() + ""); payload.put("to_port", ingress.getToPort() + ""); diff --git a/apis/s3/src/main/java/org/jclouds/s3/blobstore/S3BlobRequestSigner.java b/apis/s3/src/main/java/org/jclouds/s3/blobstore/S3BlobRequestSigner.java index 58f6ee496e..6c1757f304 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/blobstore/S3BlobRequestSigner.java +++ b/apis/s3/src/main/java/org/jclouds/s3/blobstore/S3BlobRequestSigner.java @@ -29,6 +29,7 @@ import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.functions.BlobToHttpGetOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.options.GetOptions; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.s3.S3AsyncClient; import org.jclouds.s3.blobstore.functions.BlobToObject; @@ -37,13 +38,14 @@ import org.jclouds.s3.options.PutObjectOptions; import com.google.common.collect.ImmutableList; import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeToken; /** * * @author Adrian Cole */ @Singleton -public class S3BlobRequestSigner implements BlobRequestSigner { +public class S3BlobRequestSigner implements BlobRequestSigner { private final RestAnnotationProcessor processor; private final BlobToObject blobToObject; private final BlobToHttpGetOptions blob2HttpGetOptions; @@ -51,26 +53,30 @@ public class S3BlobRequestSigner implements BlobRequestSigner { private final Invokable getMethod; private final Invokable deleteMethod; private final Invokable createMethod; + private final Class interfaceType; @Inject - public S3BlobRequestSigner(RestAnnotationProcessor.Factory processor, BlobToObject blobToObject, - BlobToHttpGetOptions blob2HttpGetOptions) throws SecurityException, NoSuchMethodException { - this.processor = checkNotNull(processor, "processor").declaring(S3AsyncClient.class); + public S3BlobRequestSigner(RestAnnotationProcessor processor, BlobToObject blobToObject, + BlobToHttpGetOptions blob2HttpGetOptions, Class interfaceType) throws SecurityException, + NoSuchMethodException { + this.processor = checkNotNull(processor, "processor"); + this.interfaceType = checkNotNull(interfaceType, "interfaceType"); this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); - this.getMethod = Invokable.from(S3AsyncClient.class.getMethod("getObject", String.class, String.class, - GetOptions[].class)); - this.deleteMethod = Invokable.from(S3AsyncClient.class.getMethod("deleteObject", String.class, String.class)); - this.createMethod = Invokable.from(S3AsyncClient.class.getMethod("putObject", String.class, S3Object.class, - PutObjectOptions[].class)); - + this.getMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("getObject", String.class, String.class, GetOptions[].class)); + this.deleteMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("deleteObject", String.class, String.class)); + this.createMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("putObject", String.class, S3Object.class, PutObjectOptions[].class)); } @Override public HttpRequest signGetBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, getMethod, + ImmutableList. of(container, name)))); } @Override @@ -82,8 +88,8 @@ public class S3BlobRequestSigner implements BlobRequestSigner { public HttpRequest signPutBlob(String container, Blob blob) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - return cleanRequest(processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob)))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, createMethod, + ImmutableList. of(container, blobToObject.apply(blob))))); } @Override @@ -95,14 +101,15 @@ public class S3BlobRequestSigner implements BlobRequestSigner { public HttpRequest signRemoveBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(deleteMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, deleteMethod, + ImmutableList. of(container, name)))); } @Override public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, - ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options"))))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, getMethod, + ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options")))))); } } diff --git a/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java b/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java index 75f8b75608..85e5b98543 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java +++ b/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java @@ -28,10 +28,12 @@ import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.attr.ConsistencyModel; import org.jclouds.blobstore.config.BlobStoreMapModule; import org.jclouds.domain.Location; +import org.jclouds.s3.S3AsyncClient; import org.jclouds.s3.S3Client; import org.jclouds.s3.blobstore.S3AsyncBlobStore; import org.jclouds.s3.blobstore.S3BlobRequestSigner; import org.jclouds.s3.blobstore.S3BlobStore; +import org.jclouds.s3.blobstore.S3BlobStoreContext; import org.jclouds.s3.blobstore.functions.LocationFromBucketName; import org.jclouds.s3.domain.AccessControlList; @@ -57,11 +59,16 @@ public class S3BlobStoreContextModule extends AbstractModule { bind(ConsistencyModel.class).toInstance(ConsistencyModel.EVENTUAL); bind(AsyncBlobStore.class).to(S3AsyncBlobStore.class).in(Scopes.SINGLETON); bind(BlobStore.class).to(S3BlobStore.class).in(Scopes.SINGLETON); - bind(BlobRequestSigner.class).to(S3BlobRequestSigner.class); bind(new TypeLiteral>() { }).to(LocationFromBucketName.class); + bindRequestSigner(); } + protected void bindRequestSigner() { + bind(BlobRequestSigner.class).to(new TypeLiteral>() { + }); + } + @Provides @Singleton protected LoadingCache bucketAcls(final S3Client client) { diff --git a/apis/s3/src/main/java/org/jclouds/s3/blobstore/functions/BlobToObjectMetadata.java b/apis/s3/src/main/java/org/jclouds/s3/blobstore/functions/BlobToObjectMetadata.java index 6da6661e4b..ca93dd9270 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/blobstore/functions/BlobToObjectMetadata.java +++ b/apis/s3/src/main/java/org/jclouds/s3/blobstore/functions/BlobToObjectMetadata.java @@ -59,7 +59,7 @@ public class BlobToObjectMetadata implements Function 2 && bucketName.length() < 256, - "bucketName name must be between 3 and 255 characters long"); - checkArgument(!IP_PATTERN.matcher(bucketName).matches(), - "bucketName name cannot be ip address style"); + "bucketName name must be between 3 and 255 characters long"); + checkArgument(!IP_PATTERN.matcher(bucketName).matches(), "bucketName name cannot be ip address style"); return bucketName; } /** - * This implementation invokes {@link S3Client#deleteBucketIfEmpty} followed by - * {@link S3Client#bucketExists} until it is true. + * This implementation invokes {@link S3Client#deleteBucketIfEmpty} followed by {@link S3Client#bucketExists} until + * it is true. */ public static boolean deleteAndVerifyContainerGone(S3Client sync, String container) { sync.deleteBucketIfEmpty(container); return !sync.bucketExists(container); } - + private static final Predicate ANNOTATIONTYPE_BUCKET = new Predicate() { public boolean apply(Annotation input) { return input.annotationType().equals(Bucket.class); @@ -78,9 +77,10 @@ public class S3Utils { String bucketName = null; - for (int i = 0; i < request.getInvoker().getParameters().size(); i++) { - if (any(Arrays.asList(request.getInvoker().getParameters().get(i).getAnnotations()), ANNOTATIONTYPE_BUCKET)) { - bucketName = (String) request.getArgs().get(i); + for (int i = 0; i < request.getInvocation().getInvokable().getParameters().size(); i++) { + if (any(Arrays.asList(request.getInvocation().getInvokable().getParameters().get(i).getAnnotations()), + ANNOTATIONTYPE_BUCKET)) { + bucketName = (String) request.getInvocation().getArgs().get(i); break; } } diff --git a/apis/s3/src/main/java/org/jclouds/s3/xml/LocationConstraintHandler.java b/apis/s3/src/main/java/org/jclouds/s3/xml/LocationConstraintHandler.java index 49d106ac5e..82cfb6944f 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/xml/LocationConstraintHandler.java +++ b/apis/s3/src/main/java/org/jclouds/s3/xml/LocationConstraintHandler.java @@ -64,7 +64,7 @@ public class LocationConstraintHandler extends ParseSax.HandlerWithResult newArrayList(null, "bucket"))).build(); } @Test diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java index 6e5754c669..037b7c2633 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java @@ -40,7 +40,6 @@ import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix; import org.jclouds.blobstore.domain.PageSet; import org.jclouds.http.functions.ParseETagHeader; import org.jclouds.http.options.GetOptions; -import org.jclouds.openstack.filters.AuthenticateRequest; import org.jclouds.openstack.swift.SwiftFallbacks.TrueOn404FalseOn409; import org.jclouds.openstack.swift.binders.BindIterableToHeadersWithContainerDeleteMetadataPrefix; import org.jclouds.openstack.swift.binders.BindMapToHeadersWithContainerMetadataPrefix; @@ -60,12 +59,10 @@ import org.jclouds.openstack.swift.options.CreateContainerOptions; import org.jclouds.openstack.swift.options.ListContainerOptions; import org.jclouds.openstack.swift.reference.SwiftHeaders; import org.jclouds.rest.annotations.BinderParam; -import org.jclouds.rest.annotations.Endpoint; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.Headers; import org.jclouds.rest.annotations.ParamParser; import org.jclouds.rest.annotations.QueryParams; -import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; import com.google.common.annotations.Beta; @@ -79,8 +76,6 @@ import com.google.inject.Provides; * @see * @author Adrian Cole */ -@RequestFilters(AuthenticateRequest.class) -@Endpoint(Storage.class) public interface CommonSwiftAsyncClient { @Provides SwiftObject newSwiftObject(); diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSigner.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSigner.java index ea34c3effa..0306f74e5c 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSigner.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSigner.java @@ -45,6 +45,7 @@ import org.jclouds.openstack.swift.CommonSwiftAsyncClient; import org.jclouds.openstack.swift.TemporaryUrlKey; import org.jclouds.openstack.swift.blobstore.functions.BlobToObject; import org.jclouds.openstack.swift.domain.SwiftObject; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.RestAnnotationProcessor; import com.google.common.base.Supplier; @@ -53,6 +54,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteProcessor; import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeToken; import com.google.inject.Provider; /** @@ -73,6 +75,7 @@ public class SwiftBlobSigner implements BlobRe private final Invokable getMethod; private final Invokable deleteMethod; private final Invokable createMethod; + private final Class interfaceType; /** * create a signer for this subtype of swift @@ -83,9 +86,10 @@ public class SwiftBlobSigner implements BlobRe @Inject protected SwiftBlobSigner(BlobToObject blobToObject, BlobToHttpGetOptions blob2HttpGetOptions, Crypto crypto, @TimeStamp Provider unixEpochTimestampProvider, - @TemporaryUrlKey Supplier temporaryUrlKeySupplier, RestAnnotationProcessor.Factory processor, - Class clazz) throws SecurityException, NoSuchMethodException { - this.processor = checkNotNull(processor, "processor").declaring(clazz); + @TemporaryUrlKey Supplier temporaryUrlKeySupplier, RestAnnotationProcessor processor, + Class interfaceType) throws SecurityException, NoSuchMethodException { + this.processor = checkNotNull(processor, "processor"); + this.interfaceType = checkNotNull(interfaceType, "interfaceType"); this.crypto = checkNotNull(crypto, "crypto"); this.unixEpochTimestampProvider = checkNotNull(unixEpochTimestampProvider, "unixEpochTimestampProvider"); @@ -94,23 +98,28 @@ public class SwiftBlobSigner implements BlobRe this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); - this.getMethod = Invokable.from(clazz.getMethod("getObject", String.class, String.class, GetOptions[].class)); - this.deleteMethod = Invokable.from(clazz.getMethod("removeObject", String.class, String.class)); - this.createMethod = Invokable.from(clazz.getMethod("putObject", String.class, SwiftObject.class)); + this.getMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("getObject", String.class, String.class, GetOptions[].class)); + this.deleteMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("removeObject", String.class, String.class)); + this.createMethod = TypeToken.of(interfaceType).method( + interfaceType.getMethod("putObject", String.class, SwiftObject.class)); } @Override public HttpRequest signGetBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, getMethod, + ImmutableList. of(container, name)))); } @Override public HttpRequest signGetBlob(String container, String name, long timeInSeconds) { checkNotNull(container, "container"); checkNotNull(name, "name"); - HttpRequest request = processor.createRequest(getMethod, ImmutableList. of(container, name)); + HttpRequest request = processor.apply(Invocation.create(interfaceType, getMethod, + ImmutableList. of(container, name))); return cleanRequest(signForTemporaryAccess(request, timeInSeconds)); } @@ -118,24 +127,24 @@ public class SwiftBlobSigner implements BlobRe public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, - ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options"))))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, getMethod, + ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options")))))); } @Override public HttpRequest signPutBlob(String container, Blob blob) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - return cleanRequest(processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob)))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, createMethod, + ImmutableList. of(container, blobToObject.apply(blob))))); } @Override public HttpRequest signPutBlob(String container, Blob blob, long timeInSeconds) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - HttpRequest request = processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob))); + HttpRequest request = processor.apply(Invocation.create(interfaceType, createMethod, + ImmutableList. of(container, blobToObject.apply(blob)))); return cleanRequest(signForTemporaryAccess(request, timeInSeconds)); } @@ -143,7 +152,8 @@ public class SwiftBlobSigner implements BlobRe public HttpRequest signRemoveBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(deleteMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(interfaceType, deleteMethod, + ImmutableList. of(container, name)))); } private HttpRequest signForTemporaryAccess(HttpRequest request, long timeInSeconds) { diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeaders.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeaders.java index 889e2d54ff..9103379f08 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeaders.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeaders.java @@ -45,7 +45,7 @@ public class ParseContainerMetadataFromHeaders implements Function> implements - InvocationContext { + InvocationContext { - private GeneratedHttpRequest request; + private List args; private String container; + private GeneratedHttpRequest request; + private ListContainerOptions options; @Inject public ParseObjectInfoListFromJsonResponse(Json json) { @@ -64,26 +67,20 @@ public class ParseObjectInfoListFromJsonResponse extends ParseJson apply(InputStream stream) { - checkState(request != null, "request should be initialized at this point"); - checkState(request.getArgs() != null, "request.getArgs() should be initialized at this point"); - checkArgument(request.getArgs().get(0) instanceof String, "arg[0] must be a container name"); - checkArgument(request.getArgs().get(1) instanceof ListContainerOptions[], - "arg[1] must be an array of ListContainerOptions"); - ListContainerOptions[] optionsList = (ListContainerOptions[]) request.getArgs().get(1); - ListContainerOptions options = optionsList.length > 0 ? optionsList[0] : ListContainerOptions.NONE; + checkState(args != null, "request should be initialized at this point"); Type listType = new TypeToken>() { }.getType(); try { SortedSet list = apply(stream, listType); SortedSet returnVal = Sets.newTreeSet(Iterables.transform(list, - new Function() { - public ObjectInfo apply(ObjectInfoImpl from) { + new Function() { + public ObjectInfo apply(ObjectInfoImpl from) { return from.toBuilder().container(container) .uri(uriBuilder(request.getEndpoint()).clearQuery().appendPath(from.getName()).build()) .build(); - } - })); + } + })); boolean truncated = options.getMaxResults() == returnVal.size(); String marker = truncated ? returnVal.last().getName() : null; return new PageSetImpl(returnVal, marker); @@ -95,12 +92,13 @@ public class ParseObjectInfoListFromJsonResponse extends ParseJson 0 ? optionsList[0] : ListContainerOptions.NONE; return this; } } diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/domain/internal/ParseObjectInfoListFromJsonResponseTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/domain/internal/ParseObjectInfoListFromJsonResponseTest.java index 9fb0e7d9d7..c271f95787 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/domain/internal/ParseObjectInfoListFromJsonResponseTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/domain/internal/ParseObjectInfoListFromJsonResponseTest.java @@ -19,9 +19,6 @@ package org.jclouds.openstack.swift.domain.internal; import static com.google.common.io.BaseEncoding.base16; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -29,20 +26,15 @@ import java.net.URI; import java.util.Set; import org.jclouds.date.internal.SimpleDateFormatDateService; -import org.jclouds.json.config.GsonModule; -import org.jclouds.json.config.GsonModule.DateAdapter; -import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; import org.jclouds.openstack.swift.domain.ObjectInfo; import org.jclouds.openstack.swift.functions.ParseObjectInfoListFromJsonResponse; +import org.jclouds.openstack.swift.internal.BasePayloadTest; import org.jclouds.openstack.swift.options.ListContainerOptions; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code ParseObjectInfoListFromJsonResponse} @@ -50,35 +42,26 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit") -public class ParseObjectInfoListFromJsonResponseTest { - - Injector i = Guice.createInjector(new AbstractModule() { - - @Override - protected void configure() { - bind(DateAdapter.class).to(Iso8601DateAdapter.class); - } - - }, new GsonModule()); +public class ParseObjectInfoListFromJsonResponseTest extends BasePayloadTest { public void testApplyInputStream() { InputStream is = getClass().getResourceAsStream("/test_list_container.json"); - Set expects = ImmutableSet. of(ObjectInfoImpl.builder().container("container").name( - "test_obj_1").uri(URI.create("http://localhost/foo/test_obj_1")).hash( - base16().lowerCase().decode("4281c348eaf83e70ddce0e07221c3d28")).bytes(14l) - .contentType("application/octet-stream").lastModified( - new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")).build(), - ObjectInfoImpl.builder().container("container").name("test_obj_2").uri( - URI.create("http://localhost/foo/test_obj_2")).hash( - base16().lowerCase().decode("b039efe731ad111bc1b0ef221c3849d0")).bytes(64l).contentType( - "application/octet-stream").lastModified( - new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")).build()); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - ListContainerOptions options = new ListContainerOptions(); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/foo")).atLeastOnce(); - expect(request.getArgs()).andReturn( - ImmutableList. of("container", new ListContainerOptions[] { options })).atLeastOnce(); - replay(request); + Set expects = ImmutableSet + . of( + ObjectInfoImpl.builder().container("container").name("test_obj_1") + .uri(URI.create("http://localhost/key/test_obj_1")) + .hash(base16().lowerCase().decode("4281c348eaf83e70ddce0e07221c3d28")).bytes(14l) + .contentType("application/octet-stream") + .lastModified(new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")) + .build(), + ObjectInfoImpl.builder().container("container").name("test_obj_2") + .uri(URI.create("http://localhost/key/test_obj_2")) + .hash(base16().lowerCase().decode("b039efe731ad111bc1b0ef221c3849d0")).bytes(64l) + .contentType("application/octet-stream") + .lastModified(new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")) + .build()); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of("container", + new ListContainerOptions[] { new ListContainerOptions() })); ParseObjectInfoListFromJsonResponse parser = i.getInstance(ParseObjectInfoListFromJsonResponse.class); parser.setContext(request); assertEquals(parser.apply(is).toString(), expects.toString()); diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerListFromJsonResponseTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerListFromJsonResponseTest.java index 6b187d5d64..9ce7d1eab7 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerListFromJsonResponseTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerListFromJsonResponseTest.java @@ -26,15 +26,13 @@ import java.util.Map; import org.jclouds.http.HttpResponse; import org.jclouds.http.functions.ParseJson; -import org.jclouds.json.config.GsonModule; import org.jclouds.openstack.swift.domain.ContainerMetadata; +import org.jclouds.openstack.swift.internal.BasePayloadTest; import org.jclouds.util.Strings2; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; -import com.google.inject.Guice; -import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.TypeLiteral; @@ -44,8 +42,7 @@ import com.google.inject.TypeLiteral; * @author Adrian Cole */ @Test(groups = "unit") -public class ParseContainerListFromJsonResponseTest { - Injector i = Guice.createInjector(new GsonModule()); +public class ParseContainerListFromJsonResponseTest extends BasePayloadTest { @Test public void testApplyInputStream() { diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeadersTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeadersTest.java index b0b4cc021b..8af697e447 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeadersTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseContainerMetadataFromHeadersTest.java @@ -18,24 +18,15 @@ */ package org.jclouds.openstack.swift.functions; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; -import java.net.URI; - import org.jclouds.http.HttpResponse; import org.jclouds.openstack.swift.domain.ContainerMetadata; -import org.jclouds.openstack.swift.functions.ParseContainerMetadataFromHeaders; +import org.jclouds.openstack.swift.internal.BasePayloadTest; import org.jclouds.openstack.swift.reference.SwiftHeaders; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code ParseContainerMetadataFromHeaders} @@ -43,22 +34,12 @@ import com.google.inject.Injector; * @author Everett Toews */ @Test(groups = "unit") -public class ParseContainerMetadataFromHeadersTest { - - Injector i = Guice.createInjector(new AbstractModule() { - - @Override - protected void configure() { - } - }); +public class ParseContainerMetadataFromHeadersTest extends BasePayloadTest { public void testParseContainerMetadataHeaders() { ParseContainerMetadataFromHeaders parser = i.getInstance(ParseContainerMetadataFromHeaders.class); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of("container", "key")).atLeastOnce(); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/test")).atLeastOnce(); - replay(request); - parser.setContext(request); + + parser.setContext(requestForArgs(ImmutableList. of("container", "key"))); HttpResponse response = HttpResponse.builder().statusCode(204).message("No Content").payload("") .addHeader(SwiftHeaders.CONTAINER_BYTES_USED, "42") diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseObjectInfoFromHeadersTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseObjectInfoFromHeadersTest.java index 31d78bc371..719a345d22 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseObjectInfoFromHeadersTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/functions/ParseObjectInfoFromHeadersTest.java @@ -18,25 +18,14 @@ */ package org.jclouds.openstack.swift.functions; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertNotNull; -import java.net.URI; - -import org.jclouds.Constants; -import org.jclouds.blobstore.reference.BlobStoreConstants; import org.jclouds.http.HttpResponse; import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata; -import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.jclouds.openstack.swift.internal.BasePayloadTest; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.name.Names; /** * Tests behavior of {@code ParseContainerListFromJsonResponse} @@ -44,26 +33,13 @@ import com.google.inject.name.Names; * @author Adrian Cole */ @Test(groups = "unit") -public class ParseObjectInfoFromHeadersTest { - - Injector i = Guice.createInjector(new AbstractModule() { - - @Override - protected void configure() { - bindConstant().annotatedWith(Names.named(BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX)).to("sdf"); - bindConstant().annotatedWith(Names.named(Constants.PROPERTY_API_VERSION)).to("1"); - } - - }); +public class ParseObjectInfoFromHeadersTest extends BasePayloadTest { public void testEtagCaseIssue() { ParseObjectInfoFromHeaders parser = i.getInstance(ParseObjectInfoFromHeaders.class); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of("container", "key")).atLeastOnce(); + + parser.setContext(requestForArgs(ImmutableList. of("container", "key"))); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/test")).atLeastOnce(); - replay(request); - parser.setContext(request); HttpResponse response = HttpResponse.builder().statusCode(200).message("ok").payload("") .addHeader("Last-Modified", "Fri, 12 Jun 2007 13:40:18 GMT") .addHeader("Content-Length", "0") diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/BasePayloadTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/BasePayloadTest.java new file mode 100644 index 0000000000..9503cbac6b --- /dev/null +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/BasePayloadTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.openstack.swift.internal; + +import java.net.URI; +import java.util.List; + +import org.jclouds.json.config.GsonModule; +import org.jclouds.json.config.GsonModule.DateAdapter; +import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; +import org.jclouds.openstack.swift.SwiftApiMetadata; +import org.jclouds.reflect.Invocation; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.nnsoft.guice.rocoto.Rocoto; +import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; + +import com.google.common.base.Throwables; +import com.google.common.reflect.Invokable; +import com.google.inject.Guice; +import com.google.inject.Injector; + +public class BasePayloadTest { + protected Injector i = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { + protected void bindConfigurations() { + bindProperties(new SwiftApiMetadata().getDefaultProperties()); + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + } + }), new GsonModule()); + + protected GeneratedHttpRequest requestForArgs(List args) { + try { + Invocation invocation = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), args); + return GeneratedHttpRequest.builder().method("POST").endpoint(URI.create("http://localhost/key")) + .invocation(invocation).build(); + } catch (SecurityException e) { + throw Throwables.propagate(e); + } catch (NoSuchMethodException e) { + throw Throwables.propagate(e); + } + } +} \ No newline at end of file diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCaptureVAppParamsToXmlPayload.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCaptureVAppParamsToXmlPayload.java index 102a88ba52..9c2c922604 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCaptureVAppParamsToXmlPayload.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCaptureVAppParamsToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.vcloud.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; @@ -68,7 +67,6 @@ public class BindCaptureVAppParamsToXmlPayload implements MapBinder { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String templateName = checkNotNull(postParams.remove("templateName"), "templateName").toString(); String vApp = checkNotNull(postParams.remove("vApp"), "vApp").toString(); @@ -106,7 +104,7 @@ public class BindCaptureVAppParamsToXmlPayload implements MapBinder { } protected CaptureVAppOptions findOptionsInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof CaptureVAppOptions) { return (CaptureVAppOptions) arg; } else if (arg instanceof CaptureVAppOptions[]) { diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayload.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayload.java index 6830c4b365..30c133346c 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayload.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.vcloud.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; @@ -69,7 +68,6 @@ public class BindCatalogItemToXmlPayload implements MapBinder { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String name = checkNotNull(postParams.get("name"), "name").toString(); URI entity = URI.create(checkNotNull(postParams.get("Entity"), "Entity").toString()); @@ -107,7 +105,7 @@ public class BindCatalogItemToXmlPayload implements MapBinder { } protected CatalogItemOptions findOptionsInArgsOrNew(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof CatalogItemOptions) { return CatalogItemOptions.class.cast(arg); } else if (arg.getClass().isArray()) { diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCloneParamsToXmlPayload.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCloneParamsToXmlPayload.java index 55581f39a4..31bd713e5c 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCloneParamsToXmlPayload.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindCloneParamsToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.vcloud.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; @@ -69,7 +68,6 @@ public abstract class BindCloneParamsToXmlPayload implem checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String name = checkNotNull(postParams.get("name"), "name").toString(); String source = checkNotNull(postParams.get("Source"), "Source").toString(); boolean isSourceDelete = Boolean.parseBoolean((String) postParams.get("IsSourceDelete")); @@ -110,7 +108,7 @@ public abstract class BindCloneParamsToXmlPayload implem @SuppressWarnings("unchecked") protected O findOptionsInArgsOrNew(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (getOptionClass().isInstance(arg)) { return (O) arg; } else if (arg.getClass().isArray()) { diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java index ab83ce71e4..a1398d29d7 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.vcloud.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.transform; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; @@ -92,7 +91,6 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String name = checkNotNull(postParams.remove("name"), "name").toString(); URI template = URI.create(checkNotNull(postParams.remove("template"), "template").toString()); @@ -203,7 +201,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder } protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof InstantiateVAppTemplateOptions) { return (InstantiateVAppTemplateOptions) arg; } else if (arg instanceof InstantiateVAppTemplateOptions[]) { diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayloadTest.java index b80389404a..1c0222cc1b 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCatalogItemToXmlPayloadTest.java @@ -18,26 +18,18 @@ */ package org.jclouds.vcloud.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; import java.io.IOException; -import java.net.URI; import java.util.Map; import org.jclouds.rest.internal.GeneratedHttpRequest; -import org.jclouds.vcloud.VCloudApiMetadata; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.jclouds.vcloud.options.CatalogItemOptions; -import org.nnsoft.guice.rocoto.Rocoto; -import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code BindCatalogItemToXmlPayload} @@ -45,31 +37,19 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit") -public class BindCatalogItemToXmlPayloadTest { - Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { - - @Override - protected void bindConfigurations() { - bindProperties(new VCloudApiMetadata().getDefaultProperties()); - } - })); - +public class BindCatalogItemToXmlPayloadTest extends BasePayloadTest { + public void testDefault() throws IOException { String expected = "mydescriptionbar"; - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn( - ImmutableList. of(CatalogItemOptions.Builder.description("mydescription").properties( - ImmutableMap.of("foo", "bar")))).anyTimes(); - request.setPayload(expected); - replay(request); + CatalogItemOptions options = CatalogItemOptions.Builder.description("mydescription").properties( + ImmutableMap.of("foo", "bar")); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindCatalogItemToXmlPayload binder = injector.getInstance(BindCatalogItemToXmlPayload.class); - Map map = ImmutableMap.of("name", "myname", "Entity", "http://fooentity"); + Map map = ImmutableMap. of("name", "myname", "Entity", "http://fooentity"); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppParamsToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppParamsToXmlPayloadTest.java index c4791b4d54..52dee7f0e0 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppParamsToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppParamsToXmlPayloadTest.java @@ -20,21 +20,15 @@ package org.jclouds.vcloud.binders; import static org.testng.Assert.assertEquals; -import java.util.Properties; - import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.util.Strings2; -import org.jclouds.vcloud.VCloudApiMetadata; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.jclouds.vcloud.options.CloneVAppOptions; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; -import com.google.common.reflect.Invokable; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.name.Names; /** * Tests behavior of {@code BindCloneVAppParamsToXmlPayload} @@ -42,26 +36,15 @@ import com.google.inject.name.Names; * @author Adrian Cole */ @Test(groups = "unit") -public class BindCloneVAppParamsToXmlPayloadTest { - Injector injector = Guice.createInjector(new AbstractModule() { - - @Override - protected void configure() { - Properties props = new VCloudApiMetadata().getDefaultProperties(); - props.setProperty("jclouds.vcloud.xml.ns", "http://www.vmware.com/vcloud/v1"); - props.setProperty("jclouds.vcloud.xml.schema", "http://vcloud.safesecureweb.com/ns/vcloud.xsd"); - Names.bindProperties(binder(), props); - } - }); - +public class BindCloneVAppParamsToXmlPayloadTest extends BasePayloadTest { + public void testWithDescriptionDeployOn() throws Exception { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/copyVApp.xml")); CloneVAppOptions options = new CloneVAppOptions().deploy().powerOn().description( "The description of the new vApp"); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") - .declaring(String.class).invoker(Invokable.from(String.class.getDeclaredMethod("toString"))).arg(options).build(); - + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); + BindCloneVAppParamsToXmlPayload binder = injector.getInstance(BindCloneVAppParamsToXmlPayload.class); Builder map = ImmutableMap.builder(); @@ -75,9 +58,7 @@ public class BindCloneVAppParamsToXmlPayloadTest { CloneVAppOptions options = new CloneVAppOptions().deploy().powerOn().description( "The description of the new vApp"); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") - .declaring(String.class).invoker(Invokable.from(String.class.getDeclaredMethod("toString"))).arg(options).build(); - + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindCloneVAppParamsToXmlPayload binder = injector.getInstance(BindCloneVAppParamsToXmlPayload.class); @@ -90,10 +71,7 @@ public class BindCloneVAppParamsToXmlPayloadTest { public void testDefault() throws Exception { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/copyVApp-default.xml")); - - GeneratedHttpRequest request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") - .declaring(String.class).invoker(Invokable.from(String.class.getDeclaredMethod("toString"))).build(); - + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindCloneVAppParamsToXmlPayload binder = injector.getInstance(BindCloneVAppParamsToXmlPayload.class); diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppTemplateParamsToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppTemplateParamsToXmlPayloadTest.java index 9fe01ebd85..5e8077c699 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppTemplateParamsToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindCloneVAppTemplateParamsToXmlPayloadTest.java @@ -18,28 +18,19 @@ */ package org.jclouds.vcloud.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; +import static org.jclouds.util.Strings2.toStringAndClose; +import static org.testng.Assert.assertEquals; import java.io.IOException; -import java.net.URI; -import java.util.Properties; import org.jclouds.rest.internal.GeneratedHttpRequest; -import org.jclouds.util.Strings2; -import org.jclouds.vcloud.VCloudApiMetadata; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.jclouds.vcloud.options.CloneVAppTemplateOptions; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.name.Names; /** * Tests behavior of {@code BindCloneVAppTemplateParamsToXmlPayload} @@ -47,77 +38,54 @@ import com.google.inject.name.Names; * @author Adrian Cole */ @Test(groups = "unit") -public class BindCloneVAppTemplateParamsToXmlPayloadTest { - Injector injector = Guice.createInjector(new AbstractModule() { - - @Override - protected void configure() { - Properties props = new Properties(); - props.setProperty("jclouds.vcloud.xml.ns", "http://www.vmware.com/vcloud/v1"); - props.setProperty("jclouds.vcloud.xml.schema", "http://vcloud.safesecureweb.com/ns/vcloud.xsd"); - Names.bindProperties(binder(), new VCloudApiMetadata().getDefaultProperties()); - } - }); - +public class BindCloneVAppTemplateParamsToXmlPayloadTest extends BasePayloadTest { public void testWithDescription() throws IOException { - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/copyVAppTemplate.xml")); - + String expected = toStringAndClose(getClass().getResourceAsStream("/copyVAppTemplate.xml")); + CloneVAppTemplateOptions options = new CloneVAppTemplateOptions() - .description("The description of the new vAppTemplate"); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); - request.setPayload(expected); - replay(request); + .description("The description of the new vAppTemplate"); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindCloneVAppTemplateParamsToXmlPayload binder = injector - .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); + .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); Builder map = ImmutableMap.builder(); map.put("name", "new-linux-server"); map.put("Source", "https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/201"); - binder.bindToRequest(request, map.build()); - verify(request); + + assertEquals(binder.bindToRequest(request, map.build()).getPayload().getRawContent(), expected); } public void testWithDescriptionSourceDelete() throws IOException { - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/moveVAppTemplate.xml")); + String expected = toStringAndClose(getClass().getResourceAsStream("/moveVAppTemplate.xml")); CloneVAppTemplateOptions options = new CloneVAppTemplateOptions() - .description("The description of the new vAppTemplate"); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); - request.setPayload(expected); - replay(request); + .description("The description of the new vAppTemplate"); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindCloneVAppTemplateParamsToXmlPayload binder = injector - .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); + .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); Builder map = ImmutableMap.builder(); map.put("name", "new-linux-server"); map.put("Source", "https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/201"); map.put("IsSourceDelete", "true"); - binder.bindToRequest(request, map.build()); - verify(request); + + assertEquals(binder.bindToRequest(request, map.build()).getPayload().getRawContent(), expected); } public void testDefault() throws IOException { - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/copyVAppTemplate-default.xml")); + String expected = toStringAndClose(getClass().getResourceAsStream("/copyVAppTemplate-default.xml")); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindCloneVAppTemplateParamsToXmlPayload binder = injector - .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); + .getInstance(BindCloneVAppTemplateParamsToXmlPayload.class); Builder map = ImmutableMap.builder(); map.put("name", "my-vapptemplate"); map.put("Source", "https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/4181"); - binder.bindToRequest(request, map.build()); - verify(request); + + assertEquals(binder.bindToRequest(request, map.build()).getPayload().getRawContent(), expected); } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindDeployVAppParamsToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindDeployVAppParamsToXmlPayloadTest.java index c022679cca..a7162b675c 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindDeployVAppParamsToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindDeployVAppParamsToXmlPayloadTest.java @@ -18,24 +18,17 @@ */ package org.jclouds.vcloud.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; import java.io.IOException; -import java.net.URI; import java.util.Map; import org.jclouds.rest.internal.GeneratedHttpRequest; -import org.jclouds.vcloud.VCloudApiMetadata; -import org.nnsoft.guice.rocoto.Rocoto; -import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code BindDeployVAppParamsToXmlPayload} @@ -43,44 +36,28 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit") -public class BindDeployVAppParamsToXmlPayloadTest { - Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { - - @Override - protected void bindConfigurations() { - bindProperties(new VCloudApiMetadata().getDefaultProperties()); - } - })); +public class BindDeployVAppParamsToXmlPayloadTest extends BasePayloadTest { public void testPowerOnTrue() throws IOException { String expected = ""; - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindDeployVAppParamsToXmlPayload binder = injector.getInstance(BindDeployVAppParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("powerOn", "true"); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testDefault() throws IOException { String expected = ""; - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindDeployVAppParamsToXmlPayload binder = injector.getInstance(BindDeployVAppParamsToXmlPayload.class); Map map = Maps.newHashMap(); - - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java index ee625e30dd..40a2e83834 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java @@ -19,11 +19,10 @@ package org.jclouds.vcloud.binders; import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.jclouds.vcloud.options.InstantiateVAppTemplateOptions.Builder.addNetworkConfig; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_FENCEMODE; +import static org.testng.Assert.assertEquals; import java.io.IOException; import java.net.URI; @@ -41,6 +40,7 @@ import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl; import org.jclouds.vcloud.domain.network.FenceMode; import org.jclouds.vcloud.domain.network.NetworkConfig; import org.jclouds.vcloud.endpoints.Network; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; import org.nnsoft.guice.rocoto.Rocoto; import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; @@ -66,7 +66,7 @@ import com.google.inject.Provides; * @author Adrian Cole */ @Test(groups = "unit") -public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { +public class BindInstantiateVAppTemplateParamsToXmlPayloadTest extends BasePayloadTest { Injector createInjector(final URI vAppTemplate, final VAppTemplate value) { return Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { @@ -109,15 +109,10 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { public void testDefault() throws IOException { URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); VAppTemplate template = createMock(VAppTemplate.class); + replay(template); String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams.xml")); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(new InstantiateVAppTemplateOptions())) - .atLeastOnce(); - request.setPayload(expected); - - replay(request, template); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(new InstantiateVAppTemplateOptions())); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( BindInstantiateVAppTemplateParamsToXmlPayload.class); @@ -125,24 +120,17 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { Map map = Maps.newHashMap(); map.put("name", "my-vapp"); map.put("template", templateUri.toASCIIString()); - binder.bindToRequest(request, map); - - verify(request, template); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testDescription() throws IOException { URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); VAppTemplate template = createMock(VAppTemplate.class); + replay(template); String expected = Strings2.toStringAndClose(getClass() .getResourceAsStream("/instantiationparams-description.xml")); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn( - ImmutableList. of(new InstantiateVAppTemplateOptions().description("my foo"))).atLeastOnce(); - request.setPayload(expected); - - replay(request, template); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(new InstantiateVAppTemplateOptions().description("my foo"))); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( BindInstantiateVAppTemplateParamsToXmlPayload.class); @@ -150,22 +138,16 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { Map map = Maps.newHashMap(); map.put("name", "my-vapp"); map.put("template", templateUri.toASCIIString()); - binder.bindToRequest(request, map); - - verify(request, template); - + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testWhenTemplateDoesntExist() throws IOException { URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); VAppTemplate template = createMock(VAppTemplate.class); + replay(template); String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams.xml")); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - request.setPayload(expected); - replay(request, template); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( BindInstantiateVAppTemplateParamsToXmlPayload.class); @@ -173,52 +155,19 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { Map map = Maps.newHashMap(); map.put("name", "my-vapp"); map.put("template", templateUri.toASCIIString()); - binder.bindToRequest(request, map); - verify(request, template); - - } - - // TODO!!! figure out how to get this to work - @Test(enabled = false) - public void testWithProcessorMemoryDisk() throws IOException { - URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); - VAppTemplate template = null; - - InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions(); - - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams.xml")); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); - request.setPayload(expected); - replay(request); - - BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); - - Map map = Maps.newHashMap(); - map.put("name", "my-vapp"); - map.put("template", "https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); - - binder.bindToRequest(request, map); - verify(request); - + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testWithNetworkNameFenceMode() throws IOException { URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); VAppTemplate template = createMock(VAppTemplate.class); + replay(template); InstantiateVAppTemplateOptions options = addNetworkConfig(new NetworkConfig("aloha", URI .create("https://vcenterprise.bluelock.com/api/v1.0/network/1991"), FenceMode.NAT_ROUTED)); String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams-network.xml")); - - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); - request.setPayload(expected); - replay(request, template); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( BindInstantiateVAppTemplateParamsToXmlPayload.class); @@ -227,7 +176,6 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { map.put("name", "my-vapp"); map.put("template", "https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); - binder.bindToRequest(request, map); - verify(request, template); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindNetworkConnectionSectionToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindNetworkConnectionSectionToXmlPayloadTest.java index f94645a31e..9354f6b4fe 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindNetworkConnectionSectionToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindNetworkConnectionSectionToXmlPayloadTest.java @@ -24,17 +24,13 @@ import java.io.IOException; import java.net.URI; import org.jclouds.http.HttpRequest; -import org.jclouds.vcloud.VCloudApiMetadata; import org.jclouds.vcloud.domain.NetworkConnection; import org.jclouds.vcloud.domain.NetworkConnectionSection; import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; -import org.nnsoft.guice.rocoto.Rocoto; -import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code BindNetworkConnectionSectionToXmlPayload} @@ -42,14 +38,7 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit", testName = "BindNetworkConnectionSectionToXmlPayloadTest") -public class BindNetworkConnectionSectionToXmlPayloadTest { - Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { - - @Override - protected void bindConfigurations() { - bindProperties(new VCloudApiMetadata().getDefaultProperties()); - } - })); +public class BindNetworkConnectionSectionToXmlPayloadTest extends BasePayloadTest { public void testWithIpAllocationModeNONE() throws IOException { diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindUndeployVAppParamsToXmlPayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindUndeployVAppParamsToXmlPayloadTest.java index 0485dd2af3..a1830b5a2f 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindUndeployVAppParamsToXmlPayloadTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/binders/BindUndeployVAppParamsToXmlPayloadTest.java @@ -18,24 +18,17 @@ */ package org.jclouds.vcloud.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; import java.io.IOException; -import java.net.URI; import java.util.Map; import org.jclouds.rest.internal.GeneratedHttpRequest; -import org.jclouds.vcloud.VCloudApiMetadata; -import org.nnsoft.guice.rocoto.Rocoto; -import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; +import org.jclouds.vcloud.internal.BasePayloadTest; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; -import com.google.inject.Guice; -import com.google.inject.Injector; /** * Tests behavior of {@code BindUndeployVAppParamsToXmlPayload} @@ -43,44 +36,28 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit") -public class BindUndeployVAppParamsToXmlPayloadTest { - Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { - - @Override - protected void bindConfigurations() { - bindProperties(new VCloudApiMetadata().getDefaultProperties()); - } - })); +public class BindUndeployVAppParamsToXmlPayloadTest extends BasePayloadTest { public void testSaveStateTrue() throws IOException { String expected = ""; - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindUndeployVAppParamsToXmlPayload binder = injector.getInstance(BindUndeployVAppParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("saveState", "true"); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testDefault() throws IOException { String expected = ""; - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindUndeployVAppParamsToXmlPayload binder = injector.getInstance(BindUndeployVAppParamsToXmlPayload.class); Map map = Maps.newHashMap(); - - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/internal/BasePayloadTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/internal/BasePayloadTest.java new file mode 100644 index 0000000000..cdde41b161 --- /dev/null +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/internal/BasePayloadTest.java @@ -0,0 +1,54 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.vcloud.internal; + +import java.net.URI; +import java.util.List; + +import org.jclouds.reflect.Invocation; +import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.jclouds.vcloud.VCloudApiMetadata; +import org.nnsoft.guice.rocoto.Rocoto; +import org.nnsoft.guice.rocoto.configuration.ConfigurationModule; + +import com.google.common.base.Throwables; +import com.google.common.reflect.Invokable; +import com.google.inject.Guice; +import com.google.inject.Injector; + +public class BasePayloadTest { + + protected Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { + protected void bindConfigurations() { + bindProperties(new VCloudApiMetadata().getDefaultProperties()); + } + })); + + protected GeneratedHttpRequest requestForArgs(List args) { + try { + Invocation invocation = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), args); + return GeneratedHttpRequest.builder().method("POST").endpoint(URI.create("http://localhost/key")) + .invocation(invocation).build(); + } catch (SecurityException e) { + throw Throwables.propagate(e); + } catch (NoSuchMethodException e) { + throw Throwables.propagate(e); + } + } +} \ No newline at end of file diff --git a/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java b/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java index 2801af0122..b5a2b3979f 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java @@ -20,6 +20,7 @@ package org.jclouds.blobstore.util; import static com.google.common.base.Preconditions.checkNotNull; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -60,12 +61,13 @@ public class BlobStoreUtils { public static String getNameFor(GeneratedHttpRequest request) { checkNotNull(request, "request"); + List args = request.getInvocation().getArgs(); // assume first params are container and key - if (request.getArgs().size() >= 2 && request.getArgs().get(0) instanceof String - && request.getArgs().get(1) instanceof String) { - return request.getArgs().get(1).toString(); - } else if (request.getArgs().size() >= 1 && request.getArgs().get(0) instanceof String) { - Matcher matcher = keyFromContainer.matcher(request.getArgs().get(0).toString()); + if (args.size() >= 2 && args.get(0) instanceof String + && args.get(1) instanceof String) { + return args.get(1).toString(); + } else if (args.size() >= 1 && args.get(0) instanceof String) { + Matcher matcher = keyFromContainer.matcher(args.get(0).toString()); if (matcher.find()) return matcher.group(1); } diff --git a/blobstore/src/test/java/org/jclouds/blobstore/util/BlobStoreUtilsTest.java b/blobstore/src/test/java/org/jclouds/blobstore/util/BlobStoreUtilsTest.java index 295a884441..23fb4aabdd 100644 --- a/blobstore/src/test/java/org/jclouds/blobstore/util/BlobStoreUtilsTest.java +++ b/blobstore/src/test/java/org/jclouds/blobstore/util/BlobStoreUtilsTest.java @@ -27,16 +27,20 @@ import static org.jclouds.blobstore.util.BlobStoreUtils.getNameFor; import static org.testng.Assert.assertEquals; import java.net.URI; +import java.util.List; import org.jclouds.blobstore.AsyncBlobStore; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.MutableBlobMetadata; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.Providers; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import com.google.common.reflect.Invokable; /** * Tests behavior of {@code BlobStoreUtils} @@ -121,30 +125,28 @@ public class BlobStoreUtilsTest { } public void testGetKeyForAzureS3AndRackspace() { - - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - - expect(request.getEndpoint()).andReturn( - URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore0/five")); - expect(request.getArgs()).andReturn(ImmutableList. of("adriancole-blobstore0", "five")).atLeastOnce(); - - replay(request); - + GeneratedHttpRequest request = requestForEndpointAndArgs( + "https://jclouds.blob.core.windows.net/adriancole-blobstore0/five", + ImmutableList. of("adriancole-blobstore0", "five")); assertEquals(getNameFor(request), "five"); } public void testGetKeyForAtmos() { - - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - - expect(request.getEndpoint()) - .andReturn( - URI.create("https://storage4.clouddrive.com/v1/MossoCloudFS_dc1f419c-5059-4c87-a389-3f2e33a77b22/adriancole-blobstore0/four")); - expect(request.getArgs()).andReturn(ImmutableList. of("adriancole-blobstore0/four")).atLeastOnce(); - - replay(request); - + GeneratedHttpRequest request = requestForEndpointAndArgs( + "https://storage4.clouddrive.com/v1/MossoCloudFS_dc1f419c-5059-4c87-a389-3f2e33a77b22/adriancole-blobstore0/four", + ImmutableList. of("adriancole-blobstore0/four")); assertEquals(getNameFor(request), "four"); } + GeneratedHttpRequest requestForEndpointAndArgs(String endpoint, List args) { + try { + Invocation invocation = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), args); + return GeneratedHttpRequest.builder().method("POST").endpoint(URI.create(endpoint)).invocation(invocation) + .build(); + } catch (SecurityException e) { + throw Throwables.propagate(e); + } catch (NoSuchMethodException e) { + throw Throwables.propagate(e); + } + } } diff --git a/common/aws/src/main/java/org/jclouds/aws/util/AWSUtils.java b/common/aws/src/main/java/org/jclouds/aws/util/AWSUtils.java index 2c80e22a51..e792e7671a 100644 --- a/common/aws/src/main/java/org/jclouds/aws/util/AWSUtils.java +++ b/common/aws/src/main/java/org/jclouds/aws/util/AWSUtils.java @@ -213,7 +213,7 @@ public class AWSUtils { } public static String findRegionInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof String) { String regionName = (String) arg; // TODO regions may not be amazon regions! diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayload.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayload.java index 5257da74d1..d5c9fad7b0 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayload.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayload.java @@ -68,7 +68,6 @@ public class BindCloneVAppParamsToXmlPayload implements MapBinder { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String newName = checkNotNull(postParams.remove("newName"), "newName").toString(); String vApp = checkNotNull(postParams.remove("vApp"), "vApp").toString(); @@ -108,7 +107,7 @@ public class BindCloneVAppParamsToXmlPayload implements MapBinder { } protected CloneVAppOptions findOptionsInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof CloneVAppOptions) { return (CloneVAppOptions) arg; } else if (arg instanceof CloneVAppOptions[]) { diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java index 0e93a51666..6779d88111 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java @@ -87,7 +87,6 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String name = checkNotNull(postParams.remove("name"), "name").toString(); String template = checkNotNull(postParams.remove("template"), "template").toString(); @@ -174,7 +173,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest gRequest) { InstantiateVAppTemplateOptions options = null; - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof InstantiateVAppTemplateOptions) { options = (InstantiateVAppTemplateOptions) arg; } else if (arg instanceof InstantiateVAppTemplateOptions[]) { diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayload.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayload.java index f551a1ec7a..9a843773d0 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayload.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayload.java @@ -82,8 +82,6 @@ public class BindVAppConfigurationToXmlPayload implements MapBinder, Function() { @@ -59,7 +59,8 @@ public class KeyPairByNameHandler extends ParseSax.HandlerForGeneratedRequestWit }); } catch (NoSuchElementException e) { - logger.debug("keypair %s/%s not found in %s", getRequest().getArgs().get(0), name, handler.getResult()); + logger.debug("keypair %s/%s not found in %s", getRequest().getInvocation().getArgs().get(0), name, + handler.getResult()); return null; } } diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindAddNodeServiceToXmlPayloadTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindAddNodeServiceToXmlPayloadTest.java index 62f6167713..8e5971e76d 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindAddNodeServiceToXmlPayloadTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindAddNodeServiceToXmlPayloadTest.java @@ -51,7 +51,7 @@ public class BindAddNodeServiceToXmlPayloadTest { @Override protected void configure() { bindConstant().annotatedWith(Names.named(PROPERTY_TERREMARK_EXTENSION_NS)).to( - "urn:tmrk:vCloudExpressExtensions-1.6"); + "urn:tmrk:vCloudExpressExtensions-1.6"); } @Singleton @@ -64,11 +64,9 @@ public class BindAddNodeServiceToXmlPayloadTest { }); public void testApplyInputStream() throws IOException { - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream( - "/CreateNodeService-test.xml")); + String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/CreateNodeService-test.xml")); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://test").build(); - BindAddNodeServiceToXmlPayload binder = injector - .getInstance(BindAddNodeServiceToXmlPayload.class); + BindAddNodeServiceToXmlPayload binder = injector.getInstance(BindAddNodeServiceToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "Node for Jim"); @@ -76,8 +74,6 @@ public class BindAddNodeServiceToXmlPayloadTest { map.put("port", "80"); map.put("enabled", "false"); map.put("description", "Some test node"); - binder.bindToRequest(request, map); - assertEquals(request.getPayload().getRawContent(), expected); - + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayloadTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayloadTest.java index 3a942ef2b2..003a5309fb 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayloadTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindCloneVAppParamsToXmlPayloadTest.java @@ -24,13 +24,14 @@ import java.util.Map; import java.util.Properties; import org.jclouds.rest.internal.GeneratedHttpRequest; +import org.jclouds.trmk.vcloud_0_8.internal.BasePayloadTest; import org.jclouds.trmk.vcloud_0_8.internal.TerremarkVCloudApiMetadata; import org.jclouds.trmk.vcloud_0_8.options.CloneVAppOptions; import org.jclouds.util.Strings2; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; -import com.google.common.reflect.Invokable; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; @@ -42,7 +43,7 @@ import com.google.inject.name.Names; * @author Adrian Cole */ @Test(groups = "unit") -public class BindCloneVAppParamsToXmlPayloadTest { +public class BindCloneVAppParamsToXmlPayloadTest extends BasePayloadTest { Injector injector = Guice.createInjector(new AbstractModule() { @Override @@ -53,15 +54,13 @@ public class BindCloneVAppParamsToXmlPayloadTest { Names.bindProperties(binder(), props); } }); - + public void testWithDescriptionDeployOn() throws Exception { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/cloneVApp.xml")); CloneVAppOptions options = new CloneVAppOptions().deploy().powerOn().withDescription( "The description of the new vApp"); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") - .declaring(String.class).invoker(Invokable.from(String.class.getDeclaredMethod("toString"))).arg(options).build(); - + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(options)); BindCloneVAppParamsToXmlPayload binder = injector.getInstance(BindCloneVAppParamsToXmlPayload.class); @@ -74,8 +73,7 @@ public class BindCloneVAppParamsToXmlPayloadTest { public void testDefault() throws Exception { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/cloneVApp-default.xml")); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") - .declaring(String.class).invoker(Invokable.from(String.class.getDeclaredMethod("toString"))).build(); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of()); BindCloneVAppParamsToXmlPayload binder = injector.getInstance(BindCloneVAppParamsToXmlPayload.class); diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java index c950b86c0f..8e1a7501fb 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindInstantiateVAppTemplateParamsToXmlPayloadTest.java @@ -18,9 +18,7 @@ */ package org.jclouds.trmk.vcloud_0_8.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; +import static org.testng.Assert.assertEquals; import java.io.IOException; import java.net.URI; @@ -32,6 +30,7 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; import org.jclouds.trmk.vcloud_0_8.domain.internal.ReferenceTypeImpl; import org.jclouds.trmk.vcloud_0_8.endpoints.Network; +import org.jclouds.trmk.vcloud_0_8.internal.BasePayloadTest; import org.jclouds.trmk.vcloud_0_8.internal.TerremarkVCloudApiMetadata; import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions; import org.jclouds.trmk.vcloud_0_8.options.InstantiateVAppTemplateOptions.NetworkConfig; @@ -42,11 +41,8 @@ import org.testng.annotations.Test; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; -import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.Multimaps; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Provides; @@ -57,7 +53,7 @@ import com.google.inject.Provides; * @author Adrian Cole */ @Test(groups = "unit") -public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { +public class BindInstantiateVAppTemplateParamsToXmlPayloadTest extends BasePayloadTest { Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { @Override @@ -69,7 +65,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { @Provides @Singleton Supplier provideNetwork() { - return Suppliers.ofInstance(new ReferenceTypeImpl(null, null, URI + return Suppliers. ofInstance(new ReferenceTypeImpl(null, null, URI .create("https://vcloud.safesecureweb.com/network/1990"))); } })); @@ -78,25 +74,18 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream( "/InstantiateVAppTemplateParams-options-test.xml")); - Multimap headers = Multimaps.synchronizedMultimap(HashMultimap. create()); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn( - ImmutableList. of(InstantiateVAppTemplateOptions.Builder.processorCount(2).memory(512) - .inGroup("group").withPassword("password").inRow("row") - .addNetworkConfig(new NetworkConfig(URI.create("http://network"))))).atLeastOnce(); - expect(request.getFirstHeaderOrNull("Content-Type")).andReturn("application/unknown").atLeastOnce(); - expect(request.getHeaders()).andReturn(headers).atLeastOnce(); - request.setPayload(expected); - replay(request); - BindInstantiateVAppTemplateParamsToXmlPayload binder = injector + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(InstantiateVAppTemplateOptions.Builder + .processorCount(2).memory(512).inGroup("group").withPassword("password").inRow("row") + .addNetworkConfig(new NetworkConfig(URI.create("http://network"))))); + + BindInstantiateVAppTemplateParamsToXmlPayload binder = injector .getInstance(BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "name"); map.put("template", "https://vcloud/vAppTemplate/3"); - binder.bindToRequest(request, map); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayloadTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayloadTest.java index d71129d260..e93cf2cdaf 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayloadTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/binders/BindVAppConfigurationToXmlPayloadTest.java @@ -18,11 +18,8 @@ */ package org.jclouds.trmk.vcloud_0_8.binders; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.jclouds.trmk.vcloud_0_8.domain.VAppConfiguration.Builder.changeNameTo; +import static org.testng.Assert.assertEquals; import java.io.IOException; import java.net.URI; @@ -35,6 +32,7 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.trmk.vcloud_0_8.domain.Status; import org.jclouds.trmk.vcloud_0_8.domain.VAppConfiguration; import org.jclouds.trmk.vcloud_0_8.domain.internal.VAppImpl; +import org.jclouds.trmk.vcloud_0_8.internal.BasePayloadTest; import org.jclouds.trmk.vcloud_0_8.internal.TerremarkVCloudApiMetadata; import org.jclouds.util.Strings2; import org.nnsoft.guice.rocoto.Rocoto; @@ -54,7 +52,7 @@ import com.google.inject.Injector; * @author Adrian Cole */ @Test(groups = "unit") -public class BindVAppConfigurationToXmlPayloadTest { +public class BindVAppConfigurationToXmlPayloadTest extends BasePayloadTest { Injector injector = Guice.createInjector(Rocoto.expandVariables(new ConfigurationModule() { @Override @@ -80,17 +78,11 @@ public class BindVAppConfigurationToXmlPayloadTest { VAppConfiguration config = new VAppConfiguration().changeNameTo("roberto"); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(vApp, config)).atLeastOnce(); - request.setPayload(expected); - replay(request); - + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(vApp, config)); BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class); Map map = Maps.newHashMap(); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testRemoveDisk() throws IOException { @@ -115,18 +107,13 @@ public class BindVAppConfigurationToXmlPayloadTest { .replace("eduardo", "MyAppServer6"); VAppConfiguration config = new VAppConfiguration().deleteDiskWithAddressOnParent(1); - - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(vApp, config)).atLeastOnce(); - request.setPayload(expected); - replay(request); + + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(vApp, config)); BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class); Map map = Maps.newHashMap(); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testChangeAll() throws IOException { @@ -145,17 +132,12 @@ public class BindVAppConfigurationToXmlPayloadTest { VAppConfiguration config = changeNameTo("eduardo").changeMemoryTo(1536).changeProcessorCountTo(1).addDisk( 25 * 1048576).addDisk(25 * 1048576); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(vApp, config)).atLeastOnce(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(vApp, config)); BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class); Map map = Maps.newHashMap(); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testChangeCPUCountTo4() throws IOException { @@ -172,17 +154,12 @@ public class BindVAppConfigurationToXmlPayloadTest { VAppConfiguration config = new VAppConfiguration().changeProcessorCountTo(4); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(vApp, config)).atLeastOnce(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(vApp, config)); BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class); Map map = Maps.newHashMap(); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } public void testChangeMemoryTo1536() throws IOException { @@ -201,16 +178,11 @@ public class BindVAppConfigurationToXmlPayloadTest { VAppConfiguration config = new VAppConfiguration().changeMemoryTo(1536); - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList. of(vApp, config)).atLeastOnce(); - request.setPayload(expected); - replay(request); + GeneratedHttpRequest request = requestForArgs(ImmutableList. of(vApp, config)); BindVAppConfigurationToXmlPayload binder = injector.getInstance(BindVAppConfigurationToXmlPayload.class); Map map = Maps.newHashMap(); - binder.bindToRequest(request, map); - verify(request); + assertEquals(binder.bindToRequest(request, map).getPayload().getRawContent(), expected); } } diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/internal/BasePayloadTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/internal/BasePayloadTest.java new file mode 100644 index 0000000000..82708e4cbe --- /dev/null +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/internal/BasePayloadTest.java @@ -0,0 +1,43 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.trmk.vcloud_0_8.internal; + +import java.net.URI; +import java.util.List; + +import org.jclouds.reflect.Invocation; +import org.jclouds.rest.internal.GeneratedHttpRequest; + +import com.google.common.base.Throwables; +import com.google.common.reflect.Invokable; + +public class BasePayloadTest { + + protected GeneratedHttpRequest requestForArgs(List args) { + try { + Invocation invocation = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), args); + return GeneratedHttpRequest.builder().method("POST").endpoint(URI.create("http://localhost/key")) + .invocation(invocation).build(); + } catch (SecurityException e) { + throw Throwables.propagate(e); + } catch (NoSuchMethodException e) { + throw Throwables.propagate(e); + } + } +} \ No newline at end of file diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/xml/KeyPairByNameHandlerTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/xml/KeyPairByNameHandlerTest.java index c62e0baa69..cee9929534 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/xml/KeyPairByNameHandlerTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/xml/KeyPairByNameHandlerTest.java @@ -18,17 +18,14 @@ */ package org.jclouds.trmk.vcloud_0_8.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; import java.io.InputStream; import java.net.URI; import java.net.UnknownHostException; import org.jclouds.http.functions.BaseHandlerTest; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.trmk.vcloud_0_8.domain.KeyPair; import org.testng.annotations.Test; @@ -59,15 +56,11 @@ public class KeyPairByNameHandlerTest extends BaseHandlerTest { KeyPair result = factory.create( addOrgAndNameToHandler(injector.getInstance(KeyPairByNameHandler.class), "org", "monster")).parse(is); - assertEquals(result, null); - + assertNull(result); } - private static KeyPairByNameHandler addOrgAndNameToHandler(KeyPairByNameHandler handler, String org, String name) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of(org, name)).anyTimes(); - replay(request); - handler.setContext(request); + private KeyPairByNameHandler addOrgAndNameToHandler(KeyPairByNameHandler handler, String org, String name) { + handler.setContext(requestForArgs(ImmutableList. of(org, name))); return handler; } } diff --git a/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java b/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java index b32961f0df..d13f62fbd7 100644 --- a/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java +++ b/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java @@ -24,34 +24,33 @@ import static com.google.common.base.Preconditions.checkState; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; -import org.jclouds.internal.ClassInvokerArgs; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; +import org.jclouds.internal.ForwardInvocationToInterface; import org.jclouds.logging.Logger; -import org.jclouds.reflect.AbstractInvocationHandler; +import org.jclouds.reflect.FunctionalReflection; +import org.jclouds.reflect.Invocation; +import org.jclouds.reflect.Invocation.Result; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.annotations.Delegate; +import org.jclouds.rest.internal.AsyncRestClientProxy; import org.jclouds.util.Optionals2; -import org.jclouds.util.Throwables2; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Optional; -import com.google.common.base.Throwables; -import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.Invokable; import com.google.common.util.concurrent.ListenableFuture; -import com.google.inject.ProvisionException; import com.google.inject.assistedinject.Assisted; /** @@ -59,137 +58,181 @@ import com.google.inject.assistedinject.Assisted; * * @author Adrian Cole */ -public final class SyncProxy extends AbstractInvocationHandler { +public final class SyncProxy implements Function { public static interface Factory { /** * @param declaring - * type of the interface where all methods match those of {@code async} except the return values are + * type of the interface where all invokeds match those of {@code async} except the return values are * dereferenced * @param async - * object whose interface matched {@code declaring} except all methods return {@link ListenableFuture} + * object whose interface matched {@code declaring} except all invokeds return {@link ListenableFuture} * @return blocking invocation handler */ SyncProxy create(Class declaring, Object async); } + /** + * CreateClientForCaller is parameterized, so clients it creates aren't singletons. For example, + * CreateClientForCaller satisfies a call like this: + * {@code context.getProviderSpecificContext().getApi().getOrgClientForName(name)} + * + * @author Adrian Cole + */ + public static class CreateClientForCaller implements Function { + private final SyncProxy.Factory factory; + private final AsyncRestClientProxy.Caller.Factory asyncFactory; + + @Inject + private CreateClientForCaller(SyncProxy.Factory factory, AsyncRestClientProxy.Caller.Factory asyncFactory) { + this.factory = factory; + this.asyncFactory = asyncFactory; + } + + @Override + public Object apply(ForwardInvocationToInterface from) { + Object asyncClient = FunctionalReflection.newProxy(from.getInterfaceType(), + asyncFactory.caller(from.getInvocation(), from.getInterfaceType())); + checkState(asyncClient != null, "configuration error, sync client for " + from + " not found"); + Class type = Optionals2.unwrapIfOptional(from.getInvocation().getInvokable().getReturnType()); + return FunctionalReflection.newProxy(type, factory.create(type, asyncClient)); + } + } + @Resource private Logger logger = Logger.NULL; - - private final Function> optionalConverter; + + private final Function> optionalConverter; private final Object delegate; private final Class declaring; - private final Map, Invokable>> methodMap; + private final Map, Invokable>> invokedMap; private final Map, Invokable> syncMethodMap; private final Map, Optional> timeoutMap; - private final LoadingCache delegateMap; + private final Function createClientForCaller; private final Map, Class> sync2Async; private static final Set objectMethods = ImmutableSet.copyOf(Object.class.getMethods()); @SuppressWarnings("unchecked") @Inject @VisibleForTesting - SyncProxy(Function> optionalConverter, - @Named("sync") LoadingCache delegateMap, Map, Class> sync2Async, + SyncProxy(Function> optionalConverter, + Function createClientForCaller, Map, Class> sync2Async, @Named("TIMEOUTS") Map timeouts, @Assisted Class declaring, @Assisted Object async) throws SecurityException, NoSuchMethodException { this.optionalConverter = optionalConverter; - this.delegateMap = delegateMap; + this.createClientForCaller = createClientForCaller; this.delegate = async; this.declaring = declaring; this.sync2Async = ImmutableMap.copyOf(sync2Async); - ImmutableMap.Builder, Invokable>> methodMapBuilder = ImmutableMap.builder(); + ImmutableMap.Builder, Invokable>> invokedMapBuilder = ImmutableMap + .builder(); ImmutableMap.Builder, Invokable> syncMethodMapBuilder = ImmutableMap.builder(); - for (Method method : declaring.getMethods()) { - if (!objectMethods.contains(method)) { - Method delegatedMethod = delegate.getClass().getMethod(method.getName(), method.getParameterTypes()); - if (!Arrays.equals(delegatedMethod.getExceptionTypes(), method.getExceptionTypes())) + for (Method invoked : declaring.getMethods()) { + if (!objectMethods.contains(invoked)) { + Method delegatedMethod = delegate.getClass().getMethod(invoked.getName(), invoked.getParameterTypes()); + if (!Arrays.equals(delegatedMethod.getExceptionTypes(), invoked.getExceptionTypes())) throw new IllegalArgumentException(String.format( - "method %s has different typed exceptions than delegated method %s", method, delegatedMethod)); + "invoked %s has different typed exceptions than delegated invoked %s", invoked, delegatedMethod)); if (delegatedMethod.getReturnType().isAssignableFrom(ListenableFuture.class)) { - methodMapBuilder.put(Invokable.from(method), Invokable.class.cast(Invokable.from(delegatedMethod))); + invokedMapBuilder.put(Invokable.from(invoked), Invokable.class.cast(Invokable.from(delegatedMethod))); } else { - syncMethodMapBuilder.put(Invokable.from(method), Invokable.class.cast(Invokable.from(delegatedMethod))); + syncMethodMapBuilder.put(Invokable.from(invoked), Invokable.class.cast(Invokable.from(delegatedMethod))); } } } - methodMap = methodMapBuilder.build(); + invokedMap = invokedMapBuilder.build(); syncMethodMap = syncMethodMapBuilder.build(); ImmutableMap.Builder, Optional> timeoutMapBuilder = ImmutableMap.builder(); - for (Invokable method : methodMap.keySet()) { - timeoutMapBuilder.put(method, timeoutInNanos(method, timeouts)); + for (Invokable invoked : invokedMap.keySet()) { + timeoutMapBuilder.put(invoked, timeoutInNanos(invoked, timeouts)); } timeoutMap = timeoutMapBuilder.build(); } - public Class getDeclaring() { - return declaring; - } - @Override - protected Object handleInvocation(Object proxy, Invokable method, List args) throws Throwable { - if (method.isAnnotationPresent(Delegate.class)) { - Class syncClass = Optionals2.returnTypeOrTypeOfOptional(method); - // get the return type of the asynchronous class associated with this client - // ex. FloatingIPClient is associated with FloatingIPAsyncClient - Class asyncClass = sync2Async.get(syncClass); - checkState(asyncClass != null, "please configure corresponding async class for " + syncClass - + " in your RestClientModule"); - // pass any parameters necessary to get a relevant instance of that async class - // ex. getClientForRegion("north") might return an instance whose endpoint is - // different that "south" - ClassInvokerArgs cma = ClassInvokerArgs.builder().clazz(asyncClass).invoker(method).args(args).build(); - Object returnVal = delegateMap.get(cma); - if (Optionals2.isReturnTypeOptional(method)){ - ClassInvokerArgsAndReturnVal cmar = ClassInvokerArgsAndReturnVal.builder().fromClassInvokerArgs(cma) - .returnVal(returnVal).build(); - return optionalConverter.apply(cmar); - } - return returnVal; - } else if (syncMethodMap.containsKey(method)) { - try { - return syncMethodMap.get(method).invoke(delegate, args.toArray()); - } catch (InvocationTargetException e) { - throw Throwables.propagate(e.getCause()); - } - } else { - try { - Optional timeoutNanos = timeoutMap.get(method); - Invokable> asyncMethod = methodMap.get(method); - String name = asyncMethod.getDeclaringClass().getSimpleName() + "." + asyncMethod.getName(); - ListenableFuture future = asyncMethod.invoke(delegate, args.toArray()); - if (timeoutNanos.isPresent()) { - logger.debug(">> blocking on %s for %s", name, timeoutNanos); - return future.get(timeoutNanos.get(), TimeUnit.NANOSECONDS); - } - logger.debug(">> blocking on %s", name); - return future.get(); - } catch (ProvisionException e) { - throw Throwables2.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); - } catch (ExecutionException e) { - throw Throwables2.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); - } catch (Exception e) { - throw Throwables2.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); + public Result apply(Invocation invocation) { + if (invocation.getInvokable().isAnnotationPresent(Delegate.class)) + return forwardToDelegate(invocation); + if (syncMethodMap.containsKey(invocation.getInvokable())) + return invokeOnDelegate(invocation); + return invokeFutureAndBlock(invocation); + } + + private Result forwardToDelegate(Invocation invocation) { + Class returnType = Optionals2.unwrapIfOptional(invocation.getInvokable().getReturnType()); + // get the return type of the asynchronous class associated with this client + // ex. FloatingIPClient is associated with FloatingIPAsyncClient + Class asyncClass = sync2Async.get(returnType); + checkState(asyncClass != null, "please configure corresponding async class for %s in your RestClientModule", + returnType); + // pass any parameters necessary to get a relevant instance of that async class + // ex. getClientForRegion("north") might return an instance whose endpoint is + // different that "south" + ForwardInvocationToInterface cma = ForwardInvocationToInterface.create(invocation, asyncClass); + Object result = createClientForCaller.apply(cma); + if (Optionals2.isReturnTypeOptional(invocation.getInvokable())) { + result = optionalConverter.apply(InvocationSuccess.create(invocation, result)); + } + return Result.success(result); + } + + private Result invokeFutureAndBlock(Invocation invocation) { + try { + Invokable> asyncMethod = invokedMap.get(invocation.getInvokable()); + ListenableFuture future = asyncMethod.invoke(delegate, invocation.getArgs().toArray()); + Optional timeoutNanos = timeoutMap.get(invocation.getInvokable()); + return block(future, timeoutNanos); + } catch (InvocationTargetException e) { + return Result.fail(e); + } catch (IllegalAccessException e) { + return Result.fail(e); + } + } + + private Result block(ListenableFuture future, Optional timeoutNanos) { + try { + if (timeoutNanos.isPresent()) { + logger.debug(">> blocking on %s for %s", future, timeoutNanos); + return Result.success(future.get(timeoutNanos.get(), TimeUnit.NANOSECONDS)); + } else { + logger.debug(">> blocking on %s", future); + return Result.success(future.get()); } + } catch (ExecutionException e) { + return Result.fail(e.getCause()); + } catch (InterruptedException e) { + return Result.fail(e); // TODO: should we kill the future? + } catch (TimeoutException e) { + return Result.fail(e); + } + } + + private Result invokeOnDelegate(Invocation invocation) { + Invokable toInvoke = syncMethodMap.get(invocation.getInvokable()); + try { + return Result.success(toInvoke.invoke(delegate, invocation.getArgs().toArray())); + } catch (InvocationTargetException e) { + return Result.fail(e); + } catch (IllegalAccessException e) { + return Result.fail(e); } } // override timeout by values configured in properties(in ms) - private Optional timeoutInNanos(Invokable method, Map timeouts) { + private Optional timeoutInNanos(Invokable invoked, Map timeouts) { String className = declaring.getSimpleName(); - Optional timeoutMillis = fromNullable(timeouts.get(className + "." + method.getName())) - .or(fromNullable(timeouts.get(className))) - .or(fromNullable(timeouts.get("default"))); + Optional timeoutMillis = fromNullable(timeouts.get(className + "." + invoked.getName())).or( + fromNullable(timeouts.get(className))).or(fromNullable(timeouts.get("default"))); if (timeoutMillis.isPresent()) return Optional.of(TimeUnit.MILLISECONDS.toNanos(timeoutMillis.get())); return Optional.absent(); } - + @Override public String toString() { - return "blocking invocation handler for: " + delegate.getClass().getSimpleName(); + return String.format("%s->%s", declaring.getClass().getSimpleName(), delegate.getClass().getSimpleName()); } } diff --git a/core/src/main/java/org/jclouds/http/HttpCommand.java b/core/src/main/java/org/jclouds/http/HttpCommand.java index b433d24173..d0eb4bad76 100644 --- a/core/src/main/java/org/jclouds/http/HttpCommand.java +++ b/core/src/main/java/org/jclouds/http/HttpCommand.java @@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import org.jclouds.rest.internal.GeneratedHttpRequest; import com.google.common.base.Objects; +import com.google.common.reflect.Invokable; /** * Command whose endpoint is an http service. @@ -131,12 +132,13 @@ public class HttpCommand { @Override public String toString() { - if (request instanceof GeneratedHttpRequest) - return String.format("[method=%s.%s, request=%s]", GeneratedHttpRequest.class.cast(request).getDeclaring() - .getSimpleName(), GeneratedHttpRequest.class.cast(request).getInvoker().getName(), request - .getRequestLine()); - else - return "[request=" + request.getRequestLine() + "]"; + if (request instanceof GeneratedHttpRequest) { + GeneratedHttpRequest gRequest = GeneratedHttpRequest.class.cast(request); + return String.format("[method=%s.%s, request=%s]", + gRequest.getInvocation().getInterfaceType().getSimpleName(), gRequest.getInvocation().getInvokable() + .getName(), gRequest.getRequestLine()); + } + return "[request=" + request.getRequestLine() + "]"; } } diff --git a/core/src/main/java/org/jclouds/http/functions/ParseSax.java b/core/src/main/java/org/jclouds/http/functions/ParseSax.java index a1c0f9d9d2..d28f701187 100644 --- a/core/src/main/java/org/jclouds/http/functions/ParseSax.java +++ b/core/src/main/java/org/jclouds/http/functions/ParseSax.java @@ -212,7 +212,7 @@ public class ParseSax implements Function, InvocationContext @Override public HandlerForGeneratedRequestWithResult setContext(HttpRequest request) { - checkArgument(request instanceof GeneratedHttpRequest, "note this handler requires a GeneratedHttpRequest"); + checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "note this handler requires a GeneratedHttpRequest"); super.setContext(request); return this; } diff --git a/core/src/main/java/org/jclouds/internal/ClassInvokerArgs.java b/core/src/main/java/org/jclouds/internal/ClassInvokerArgs.java deleted file mode 100644 index 427bb13e6b..0000000000 --- a/core/src/main/java/org/jclouds/internal/ClassInvokerArgs.java +++ /dev/null @@ -1,149 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.internal; - -import static com.google.common.base.Objects.equal; -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.List; - -import com.google.common.base.Objects; -import com.google.common.base.Objects.ToStringHelper; -import com.google.common.collect.ImmutableList; -import com.google.common.reflect.Invokable; - -/** - * - * @author Adrian Cole - */ -public class ClassInvokerArgs { - public static Builder builder() { - return new ConcreteBuilder(); - } - - public Builder toBuilder() { - return builder().fromClassInvokerArgs(this); - } - - private static class ConcreteBuilder extends Builder { - } - - public abstract static class Builder> { - private Class clazz; - private Invokable invoker; - private List args = ImmutableList.of(); - - @SuppressWarnings("unchecked") - protected B self() { - return (B) this; - } - - /** - * @see ClassInvokerArgs#getClazz() - */ - public B clazz(Class clazz) { - this.clazz = clazz; - return self(); - } - - /** - * @see ClassInvokerArgs#getInvoker() - */ - public B invoker(Invokable invoker) { - this.invoker = invoker; - return self(); - } - - /** - * @see ClassInvokerArgs#getArgs() - */ - public B args(List args) { - this.args = args; - return self(); - } - - public ClassInvokerArgs build() { - return new ClassInvokerArgs(this); - } - - public B fromClassInvokerArgs(ClassInvokerArgs in) { - return clazz(in.getClazz()).invoker(in.getInvoker()).args(in.getArgs()); - } - } - - private final Class clazz; - private final Invokable invoker; - private final List args; - - public ClassInvokerArgs(Builder builder) { - this(builder.clazz, builder.invoker, builder.args); - } - - /** - * @param args as these represent parameters, can contain nulls - */ - public ClassInvokerArgs(Class clazz, Invokable invoker, List args) { - this.clazz = checkNotNull(clazz, "clazz"); - this.invoker = checkNotNull(invoker, "invoker"); - this.args = checkNotNull(args, "args"); - } - - /** - * not necessarily the declaring class of the invoker. - */ - public Class getClazz() { - return clazz; - } - - public Invokable getInvoker() { - return invoker; - } - - /** - * @param args as these represent parameters, can contain nulls - */ - public List getArgs() { - return args; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - ClassInvokerArgs that = ClassInvokerArgs.class.cast(o); - return equal(this.clazz, that.clazz) && equal(this.invoker, that.invoker) && equal(this.args, that.args); - } - - @Override - public int hashCode() { - return Objects.hashCode(clazz, invoker, args); - } - - @Override - public String toString() { - return string().toString(); - } - - protected ToStringHelper string() { - return Objects.toStringHelper("").omitNullValues().add("clazz", clazz).add("invoker", invoker) - .add("args", args.size() != 0 ? args : null); - } -} diff --git a/core/src/main/java/org/jclouds/internal/ClassInvokerArgsAndReturnVal.java b/core/src/main/java/org/jclouds/internal/ClassInvokerArgsAndReturnVal.java deleted file mode 100644 index d4dda2524e..0000000000 --- a/core/src/main/java/org/jclouds/internal/ClassInvokerArgsAndReturnVal.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.internal; - -import static com.google.common.base.Objects.equal; - -import java.util.List; - -import com.google.common.base.Objects; -import com.google.common.base.Objects.ToStringHelper; -import com.google.common.reflect.Invokable; - -/** - * - * @author Adrian Cole - */ -public final class ClassInvokerArgsAndReturnVal extends ClassInvokerArgs { - - public static Builder builder() { - return new Builder(); - } - - public Builder toBuilder() { - return builder().fromClassInvokerArgsAndReturnVal(this); - } - - public final static class Builder extends ClassInvokerArgs.Builder { - - private Object returnVal; - - /** - * @see ClassInvokerArgsAndReturnVal#getReturnVal() - */ - public Builder returnVal(Object returnVal) { - this.returnVal = returnVal; - return this; - } - - @Override - public ClassInvokerArgsAndReturnVal build() { - return new ClassInvokerArgsAndReturnVal(this); - } - - public Builder fromClassInvokerArgsAndReturnVal(ClassInvokerArgsAndReturnVal in) { - return fromClassInvokerArgs(in).returnVal(in.getReturnVal()); - } - } - - private final Object returnVal; - - private ClassInvokerArgsAndReturnVal(Class clazz, Invokable invoker, List args, Object returnVal) { - super(clazz, invoker, args); - this.returnVal = returnVal; - } - - private ClassInvokerArgsAndReturnVal(Builder builder) { - super(builder); - this.returnVal = builder.returnVal; - } - - public Object getReturnVal() { - return returnVal; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - ClassInvokerArgsAndReturnVal that = ClassInvokerArgsAndReturnVal.class.cast(o); - return super.equals(that) && equal(this.returnVal, that.returnVal); - } - - @Override - public int hashCode() { - return Objects.hashCode(super.hashCode(), returnVal); - } - - @Override - public ToStringHelper string() { - return super.string().add("returnVal", returnVal); - } - -} diff --git a/core/src/main/java/org/jclouds/internal/ForwardInvocationToInterface.java b/core/src/main/java/org/jclouds/internal/ForwardInvocationToInterface.java new file mode 100644 index 0000000000..60087d6183 --- /dev/null +++ b/core/src/main/java/org/jclouds/internal/ForwardInvocationToInterface.java @@ -0,0 +1,84 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.internal; + +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Preconditions.checkNotNull; + +import org.jclouds.reflect.Invocation; + +import com.google.common.annotations.Beta; +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; + +/** + * internal type to {@link SyncProxy} which is likely to be removed + * + * @author Adrian Cole + */ +@Beta +public final class ForwardInvocationToInterface { + /** + * @param interfaceType + * {@link #getInterfaceType()} + */ + public static ForwardInvocationToInterface create(Invocation invocation, Class interfaceType) { + return new ForwardInvocationToInterface(invocation, interfaceType); + } + + private final Invocation invocation; + private final Class interfaceType; + + private ForwardInvocationToInterface(Invocation invocation, Class interfaceType) { + this.invocation = checkNotNull(invocation, "invocation"); + this.interfaceType = checkNotNull(interfaceType, "interfaceType"); + } + + public Invocation getInvocation() { + return invocation; + } + + public Class getInterfaceType() { + return interfaceType; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + ForwardInvocationToInterface that = ForwardInvocationToInterface.class.cast(o); + return equal(this.invocation, that.invocation) && equal(this.interfaceType, that.interfaceType); + } + + @Override + public int hashCode() { + return Objects.hashCode(invocation, interfaceType); + } + + @Override + public String toString() { + return string().toString(); + } + + protected ToStringHelper string() { + return Objects.toStringHelper("").omitNullValues().add("invocation", invocation).add("interfaceType", interfaceType); + } +} diff --git a/core/src/main/java/org/jclouds/location/config/LocationModule.java b/core/src/main/java/org/jclouds/location/config/LocationModule.java index 3312bc8fc1..a8b17ba21c 100644 --- a/core/src/main/java/org/jclouds/location/config/LocationModule.java +++ b/core/src/main/java/org/jclouds/location/config/LocationModule.java @@ -32,7 +32,6 @@ import javax.inject.Singleton; import org.jclouds.collect.Memoized; import org.jclouds.domain.Location; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; import org.jclouds.location.Iso3166; import org.jclouds.location.Provider; import org.jclouds.location.Region; @@ -49,6 +48,7 @@ import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; import org.jclouds.location.suppliers.RegionIdsSupplier; import org.jclouds.location.suppliers.ZoneIdToURISupplier; import org.jclouds.location.suppliers.ZoneIdsSupplier; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.functions.ImplicitOptionalConverter; import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; @@ -73,7 +73,7 @@ public class LocationModule extends AbstractModule { @Override protected void configure() { - bind(new TypeLiteral>>(){}).to(ImplicitOptionalConverter.class); + bind(new TypeLiteral>>(){}).to(ImplicitOptionalConverter.class); } @Provides diff --git a/core/src/main/java/org/jclouds/reflect/AbstractInvocationHandler.java b/core/src/main/java/org/jclouds/reflect/AbstractInvocationHandler.java deleted file mode 100644 index c3e677882f..0000000000 --- a/core/src/main/java/org/jclouds/reflect/AbstractInvocationHandler.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.reflect; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import com.google.common.annotations.Beta; -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.reflect.Invokable; - -/** - * Like {@link com.google.common.reflect.AbstractInvocationHandler}, except you process {@link Invokable} and - * {@link List} as opposed to {@link Method} and arg arrays. - * - * @author Adrian Cole - * @since 1.6 - */ -@Beta -public abstract class AbstractInvocationHandler extends com.google.common.reflect.AbstractInvocationHandler { - - /** - * @param args - * note that this can contain nulls, as method arguments can be null. - * @see com.google.common.reflect.AbstractInvocationHandler#invoke(Object, Method, Object[]) - */ - protected abstract Object handleInvocation(Object proxy, Invokable method, List args) throws Throwable; - - @Override - protected Object handleInvocation(Object proxy, Method method, Object[] argv) throws Throwable { - List args = Arrays.asList(argv); - if (Iterables.all(args, Predicates.notNull())) - args = ImmutableList.copyOf(args); - else - args = Collections.unmodifiableList(args); - return handleInvocation(proxy, Invokable.from(method), args); - } -} \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/rest/InputParamValidator.java b/core/src/main/java/org/jclouds/rest/InputParamValidator.java index de10b7d1d8..f33e4b82ad 100644 --- a/core/src/main/java/org/jclouds/rest/InputParamValidator.java +++ b/core/src/main/java/org/jclouds/rest/InputParamValidator.java @@ -26,12 +26,12 @@ import java.util.List; import javax.inject.Inject; import org.jclouds.predicates.Validator; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.annotations.ParamValidators; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; -import com.google.common.reflect.Invokable; import com.google.common.reflect.Parameter; import com.google.inject.Injector; @@ -70,14 +70,13 @@ public class InputParamValidator { * @throws IllegalStateException * if validation failed */ - public void validateMethodParametersOrThrow(Invokable method, List args) { + public void validateMethodParametersOrThrow(Invocation invocation) { try { - performMethodValidation(checkNotNull(method, "method"), args); - performParameterValidation(method.getParameters(), args); + performMethodValidation(checkNotNull(invocation, "invocation")); + performParameterValidation(invocation); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(String.format("Validation on '%s#%s' didn't pass for arguments: " - + "%s. %n Reason: %s.", method.getDeclaringClass().getName(), method.getName(), args, - e.getMessage())); + throw new IllegalArgumentException(String.format("Validation on '%s' didn't pass:%n Reason: %s.", invocation, + e.getMessage()), e); } } @@ -90,14 +89,14 @@ public class InputParamValidator { * @param args * method's parameters */ - private void performMethodValidation(Invokable method, List args) { - ParamValidators paramValidatorsAnnotation = method.getAnnotation(ParamValidators.class); + private void performMethodValidation(Invocation invocation) { + ParamValidators paramValidatorsAnnotation = invocation.getInvokable().getAnnotation(ParamValidators.class); if (paramValidatorsAnnotation == null) return; // by contract List> methodValidators = getValidatorsFromAnnotation(paramValidatorsAnnotation); - runPredicatesAgainstArgs(methodValidators, args); + runPredicatesAgainstArgs(methodValidators, invocation.getArgs()); } /** @@ -109,8 +108,8 @@ public class InputParamValidator { * @param args * arguments that correspond to the array of annotations */ - private void performParameterValidation(List parameters, List args) { - for (Parameter param : filter(parameters, new Predicate() { + private void performParameterValidation(Invocation invocation) { + for (Parameter param : filter(invocation.getInvokable().getParameters(), new Predicate() { public boolean apply(Parameter in) { return in.isAnnotationPresent(ParamValidators.class); } @@ -119,7 +118,8 @@ public class InputParamValidator { if (annotation == null) continue; List> parameterValidators = getValidatorsFromAnnotation(annotation); - runPredicatesAgainstArg(parameterValidators, args.get(param.hashCode()));// TODO position guava issue 1243 + // TODO position guava issue 1243 + runPredicatesAgainstArg(parameterValidators, invocation.getArgs().get(param.hashCode())); } } @@ -130,7 +130,7 @@ public class InputParamValidator { } return validators; } - + @SuppressWarnings("unchecked") private void runPredicatesAgainstArg(List> predicates, Object arg) { for (@SuppressWarnings("rawtypes") diff --git a/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java b/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java index d43c3e8e84..cc3332db2d 100644 --- a/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java +++ b/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java @@ -32,21 +32,24 @@ import org.jclouds.rest.MapBinder; import org.jclouds.rest.annotations.Payload; import org.jclouds.rest.internal.GeneratedHttpRequest; +import com.google.common.reflect.Invokable; + /** * * @author Adrian Cole */ @Singleton public class BindMapToStringPayload implements MapBinder { - + @SuppressWarnings("unchecked") @Override public R bindToRequest(R request, Map postParams) { checkNotNull(postParams, "postParams"); GeneratedHttpRequest r = GeneratedHttpRequest.class.cast(checkNotNull(request, "request")); - checkArgument(r.getInvoker().isAnnotationPresent(Payload.class), - "method %s must have @Payload annotation to use this binder", r.getInvoker()); - String payload = r.getInvoker().getAnnotation(Payload.class).value(); + Invokable invoked = r.getInvocation().getInvokable(); + checkArgument(invoked.isAnnotationPresent(Payload.class), + "method %s must have @Payload annotation to use this binder", invoked); + String payload = invoked.getAnnotation(Payload.class).value(); if (postParams.size() > 0) { payload = urlDecode(expand(payload, postParams)); } diff --git a/core/src/main/java/org/jclouds/rest/config/AsyncClientProvider.java b/core/src/main/java/org/jclouds/rest/config/AsyncClientProvider.java index 145821b0bc..4cb7237e14 100644 --- a/core/src/main/java/org/jclouds/rest/config/AsyncClientProvider.java +++ b/core/src/main/java/org/jclouds/rest/config/AsyncClientProvider.java @@ -18,13 +18,11 @@ */ package org.jclouds.rest.config; -import static com.google.common.reflect.Reflection.newProxy; - import javax.inject.Inject; import javax.inject.Singleton; +import org.jclouds.reflect.FunctionalReflection; import org.jclouds.rest.internal.AsyncRestClientProxy; -import org.jclouds.rest.internal.AsyncRestClientProxy.Factory; import com.google.inject.Provider; import com.google.inject.TypeLiteral; @@ -36,11 +34,11 @@ import com.google.inject.TypeLiteral; @Singleton public class AsyncClientProvider implements Provider { private final Class asyncClientType; - private final Factory factory; + private final AsyncRestClientProxy proxy; @Inject - private AsyncClientProvider(AsyncRestClientProxy.Factory factory, TypeLiteral asyncClientType) { - this.factory = factory; + private AsyncClientProvider(AsyncRestClientProxy proxy, TypeLiteral asyncClientType) { + this.proxy = proxy; this.asyncClientType = asyncClientType.getRawType(); } @@ -48,7 +46,7 @@ public class AsyncClientProvider implements Provider { @Override @Singleton public A get() { - return (A) newProxy(asyncClientType, factory.declaring(asyncClientType)); + return (A) FunctionalReflection.newProxy(asyncClientType, proxy); } } diff --git a/core/src/main/java/org/jclouds/rest/config/ClientProvider.java b/core/src/main/java/org/jclouds/rest/config/ClientProvider.java index 31aef4a214..9d047e2ef9 100644 --- a/core/src/main/java/org/jclouds/rest/config/ClientProvider.java +++ b/core/src/main/java/org/jclouds/rest/config/ClientProvider.java @@ -18,12 +18,11 @@ */ package org.jclouds.rest.config; -import static com.google.common.reflect.Reflection.newProxy; - import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.concurrent.internal.SyncProxy; +import org.jclouds.reflect.FunctionalReflection; import com.google.inject.Provider; @@ -50,6 +49,6 @@ public class ClientProvider implements Provider { @Override @Singleton public S get() { - return newProxy(syncClientType, factory.create(syncClientType, asyncClient)); + return FunctionalReflection.newProxy(syncClientType, factory.create(syncClientType, asyncClient)); } } diff --git a/core/src/main/java/org/jclouds/rest/config/RestModule.java b/core/src/main/java/org/jclouds/rest/config/RestModule.java index 319a694123..4693161eb8 100644 --- a/core/src/main/java/org/jclouds/rest/config/RestModule.java +++ b/core/src/main/java/org/jclouds/rest/config/RestModule.java @@ -29,10 +29,11 @@ import javax.inject.Named; import javax.inject.Singleton; import org.jclouds.concurrent.internal.SyncProxy; +import org.jclouds.concurrent.internal.SyncProxy.CreateClientForCaller; import org.jclouds.functions.IdentityFunction; import org.jclouds.http.functions.config.SaxParserModule; -import org.jclouds.internal.ClassInvokerArgs; import org.jclouds.internal.FilterStringsBoundToInjectorByName; +import org.jclouds.internal.ForwardInvocationToInterface; import org.jclouds.json.config.GsonModule; import org.jclouds.location.config.LocationModule; import org.jclouds.rest.AuthorizationException; @@ -40,8 +41,6 @@ import org.jclouds.rest.HttpAsyncClient; import org.jclouds.rest.HttpClient; import org.jclouds.rest.binders.BindToJsonPayloadWrappedWith; import org.jclouds.rest.internal.AsyncRestClientProxy; -import org.jclouds.rest.internal.CreateAsyncClientForCaller; -import org.jclouds.rest.internal.CreateClientForCaller; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.util.Maps2; import org.jclouds.util.Predicates2; @@ -49,8 +48,6 @@ import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Supplier; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.util.concurrent.Atomics; @@ -84,8 +81,8 @@ public class RestModule extends AbstractModule { install(new SaxParserModule()); install(new GsonModule()); install(new FactoryModuleBuilder().build(BindToJsonPayloadWrappedWith.Factory.class)); - install(new FactoryModuleBuilder().build(RestAnnotationProcessor.Factory.class)); - install(new FactoryModuleBuilder().build(AsyncRestClientProxy.Factory.class)); + install(new FactoryModuleBuilder().build(RestAnnotationProcessor.Caller.Factory.class)); + install(new FactoryModuleBuilder().build(AsyncRestClientProxy.Caller.Factory.class)); bind(IdentityFunction.class).toInstance(IdentityFunction.INSTANCE); install(new FactoryModuleBuilder().build(SyncProxy.Factory.class)); bindClientAndAsyncClient(binder(), HttpClient.class, HttpAsyncClient.class); @@ -94,6 +91,10 @@ public class RestModule extends AbstractModule { }).toInstance(authException); bind(new TypeLiteral, Map>>() { }).to(FilterStringsBoundToInjectorByName.class); + bind(new TypeLiteral, Map>>() { + }).to(FilterStringsBoundToInjectorByName.class); + bind(new TypeLiteral>() { + }).to(CreateClientForCaller.class); installLocations(); } @@ -120,19 +121,4 @@ public class RestModule extends AbstractModule { }); } - - @Provides - @Singleton - @Named("async") - LoadingCache provideAsyncDelegateMap(CreateAsyncClientForCaller createAsyncClientForCaller) { - return CacheBuilder.newBuilder().build(createAsyncClientForCaller); - } - - @Provides - @Singleton - @Named("sync") - LoadingCache provideSyncDelegateMap(CreateClientForCaller createClientForCaller) { - return CacheBuilder.newBuilder().build(createClientForCaller); - } - } diff --git a/core/src/main/java/org/jclouds/rest/functions/AlwaysPresentImplicitOptionalConverter.java b/core/src/main/java/org/jclouds/rest/functions/AlwaysPresentImplicitOptionalConverter.java index 34c4ac5095..d1c22f41c0 100644 --- a/core/src/main/java/org/jclouds/rest/functions/AlwaysPresentImplicitOptionalConverter.java +++ b/core/src/main/java/org/jclouds/rest/functions/AlwaysPresentImplicitOptionalConverter.java @@ -18,7 +18,7 @@ */ package org.jclouds.rest.functions; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; +import org.jclouds.reflect.InvocationSuccess; import com.google.common.annotations.Beta; import com.google.common.base.Optional; @@ -31,8 +31,8 @@ import com.google.common.base.Optional; public class AlwaysPresentImplicitOptionalConverter implements ImplicitOptionalConverter { @Override - public Optional apply(ClassInvokerArgsAndReturnVal input) { - return Optional.of(input.getReturnVal()); + public Optional apply(InvocationSuccess input) { + return input.getResult(); } } diff --git a/core/src/main/java/org/jclouds/rest/functions/ImplicitOptionalConverter.java b/core/src/main/java/org/jclouds/rest/functions/ImplicitOptionalConverter.java index 25c57b61c9..6ed17f5b6d 100644 --- a/core/src/main/java/org/jclouds/rest/functions/ImplicitOptionalConverter.java +++ b/core/src/main/java/org/jclouds/rest/functions/ImplicitOptionalConverter.java @@ -18,7 +18,9 @@ */ package org.jclouds.rest.functions; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; +import org.jclouds.reflect.InvocationSuccess; +import org.jclouds.rest.annotations.ApiVersion; +import org.jclouds.rest.annotations.SinceApiVersion; import org.jclouds.rest.config.RestClientModule; import com.google.common.annotations.Beta; @@ -39,19 +41,19 @@ import com.google.inject.ImplementedBy; * } * * - * The input object of type {@link ClassInvokerArgsAndReturnVal} will include the + * The input object of type {@link InvocationSuccess} will include the * following. *
    *
  1. the class declaring the method that returns optional: - * {@link ClassInvokerArgsAndReturnVal#getClazz}; in the example above, + * {@link InvocationSuccess#getClazz}; in the example above, * {@code MyCloud}
  2. *
  3. the method returning the optional: - * {@link ClassInvokerArgsAndReturnVal#getMethod}; in the example above, + * {@link InvocationSuccess#getMethod}; in the example above, * {@code getKeyPairExtensionForRegion}
  4. *
  5. the args passed to that method at runtime: - * {@link ClassInvokerArgsAndReturnVal#getArgs}; for example {@code North}
  6. + * {@link InvocationSuccess#getArgs}; for example {@code North} *
  7. the rest client to be enclosed in the optional, should you choose to - * return it: {@link ClassInvokerArgsAndReturnVal#getReturnVal}; in the example + * return it: {@link InvocationSuccess#getReturnVal}; in the example * above, an implementation of {@code KeyPairClient}
  8. *
* @@ -80,6 +82,6 @@ import com.google.inject.ImplementedBy; */ @Beta @ImplementedBy(PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.class) -public interface ImplicitOptionalConverter extends Function> { +public interface ImplicitOptionalConverter extends Function> { } diff --git a/core/src/main/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.java b/core/src/main/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.java index 5eae8aae51..e7406da03a 100644 --- a/core/src/main/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.java +++ b/core/src/main/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.java @@ -19,11 +19,12 @@ package org.jclouds.rest.functions; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.util.Optionals2.unwrapIfOptional; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.annotations.ApiVersion; import org.jclouds.rest.annotations.SinceApiVersion; @@ -43,7 +44,7 @@ import com.google.common.cache.LoadingCache; public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion implements ImplicitOptionalConverter { @VisibleForTesting - static final class Loader extends CacheLoader> { + static final class Loader extends CacheLoader> { private final String apiVersion; @Inject @@ -52,22 +53,22 @@ public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion impl } @Override - public Optional load(ClassInvokerArgsAndReturnVal input) { - Optional sinceApiVersion = Optional.fromNullable(input.getClazz().getAnnotation( - SinceApiVersion.class)); + public Optional load(InvocationSuccess input) { + Class target = unwrapIfOptional(input.getInvocation().getInvokable().getReturnType()); + Optional sinceApiVersion = Optional.fromNullable(target.getAnnotation(SinceApiVersion.class)); if (sinceApiVersion.isPresent()) { String since = sinceApiVersion.get().value(); if (since.compareTo(apiVersion) <= 0) - return Optional.of(input.getReturnVal()); + return input.getResult(); return Optional.absent(); } else { // No SinceApiVersion annotation, so return present - return Optional.of(input.getReturnVal()); + return input.getResult(); } } } - private final LoadingCache> lookupCache; + private final LoadingCache> lookupCache; @Inject protected PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion(@ApiVersion String apiVersion) { @@ -76,7 +77,7 @@ public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion impl } @Override - public Optional apply(ClassInvokerArgsAndReturnVal input) { + public Optional apply(InvocationSuccess input) { return lookupCache.getUnchecked(input); } 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 f720d3bf04..425ef969f9 100644 --- a/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java +++ b/core/src/main/java/org/jclouds/rest/internal/AsyncRestClientProxy.java @@ -19,6 +19,9 @@ package org.jclouds.rest.internal; import static com.google.common.base.Functions.compose; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Predicates.in; +import static com.google.common.base.Predicates.not; import static com.google.common.base.Throwables.propagate; import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.find; @@ -31,7 +34,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_XML; import static org.jclouds.concurrent.Futures.makeListenable; import static org.jclouds.http.HttpUtils.tryFindHttpMethod; import static org.jclouds.util.Optionals2.isReturnTypeOptional; -import static org.jclouds.util.Optionals2.returnTypeOrTypeOfOptional; +import static org.jclouds.util.Optionals2.unwrapIfOptional; import static org.jclouds.util.Throwables2.getFirstThrowableOfType; import java.io.InputStream; @@ -41,7 +44,6 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.net.URI; -import java.util.List; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -73,11 +75,12 @@ import org.jclouds.http.functions.ReturnInputStream; import org.jclouds.http.functions.ReturnStringIf2xx; import org.jclouds.http.functions.ReturnTrueIf2xx; import org.jclouds.http.functions.UnwrapOnlyJsonValue; -import org.jclouds.internal.ClassInvokerArgs; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; import org.jclouds.json.internal.GsonWrapper; import org.jclouds.logging.Logger; -import org.jclouds.reflect.AbstractInvocationHandler; +import org.jclouds.reflect.FunctionalReflection; +import org.jclouds.reflect.Invocation; +import org.jclouds.reflect.Invocation.Result; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.InvocationContext; import org.jclouds.rest.annotations.Delegate; @@ -95,7 +98,6 @@ import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Optional; import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.base.Supplier; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -103,7 +105,6 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; import com.google.common.reflect.Invokable; import com.google.common.reflect.Parameter; import com.google.common.reflect.TypeToken; @@ -123,49 +124,36 @@ import com.google.inject.assistedinject.Assisted; *

* Particularly, this code delegates calls to other things. *

    - *
  1. if the method has a {@link Provides} annotation, it responds via a {@link Injector} lookup
  2. - *
  3. if the method has a {@link Delegate} annotation, it responds with an instance of interface set in returnVal, + *
  4. if the invoked has a {@link Provides} annotation, it responds via a {@link Injector} lookup
  5. + *
  6. if the invoked has a {@link Delegate} annotation, it responds with an instance of interface set in returnVal, * adding the current JAXrs annotations to whatever are on that class.
  7. *
      - *
    • ex. if the method with {@link Delegate} has a {@code Path} annotation, and the returnval interface also has + *
    • ex. if the invoked with {@link Delegate} has a {@code Path} annotation, and the returnval interface also has * {@code Path}, these values are combined.
    • *
    *
  8. if {@link RestAnnotationProcessor#delegationMap} contains a mapping for this, and the returnVal is properly * assigned as a {@link ListenableFuture}, it responds with an http implementation.
  9. *
  10. otherwise a RuntimeException is thrown with a message including: - * {@code method is intended solely to set constants}
  11. + * {@code invoked is intended solely to set constants} *
* * @author Adrian Cole */ @Singleton -public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { - - public static interface Factory { - Declaring declaring(Class declaring); - - Caller caller(ClassInvokerArgs caller); - } - - public final static class Declaring extends AsyncRestClientProxy { - @Inject - private Declaring(Injector injector, Function> optionalConverter, - HttpCommandExecutorService http, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads, - @Named("async") LoadingCache delegateMap, RestAnnotationProcessor.Factory rap, - ParseSax.Factory parserFactory, @Assisted Class declaring) { - super(injector, optionalConverter, http, userThreads, delegateMap, rap.declaring(declaring), parserFactory, - declaring); - } - } +public class AsyncRestClientProxy implements Function { public final static class Caller extends AsyncRestClientProxy { + + public static interface Factory { + Caller caller(Invocation caller, Class interfaceType); + } + @Inject - private Caller(Injector injector, Function> optionalConverter, + private Caller(Injector injector, Function> optionalConverter, HttpCommandExecutorService http, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads, - @Named("async") LoadingCache delegateMap, RestAnnotationProcessor.Factory rap, - ParseSax.Factory parserFactory, @Assisted ClassInvokerArgs caller) { - super(injector, optionalConverter, http, userThreads, delegateMap, rap.caller(caller), parserFactory, caller - .getClazz()); + Caller.Factory factory, RestAnnotationProcessor.Caller.Factory rap, ParseSax.Factory parserFactory, + @Assisted Invocation caller) { + super(injector, optionalConverter, http, userThreads, factory, rap.caller(caller), parserFactory); } } @@ -175,18 +163,16 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { private final Injector injector; private final HttpCommandExecutorService http; private final ExecutorService userThreads; - private final Function> optionalConverter; - private final LoadingCache delegateMap; + private final Function> optionalConverter; + private final Caller.Factory factory; private final RestAnnotationProcessor annotationProcessor; private final ParseSax.Factory parserFactory; - private final Class declaring; - - private static final LoadingCache, Set> delegationMapCache = CacheBuilder.newBuilder().build( - new CacheLoader, Set>() { - public Set load(Class declaring) throws ExecutionException { - FluentIterable> methodsToProcess = FluentIterable - .from(ImmutableSet.copyOf(declaring.getMethods())) - .filter(Predicates.not(Predicates.in(ImmutableSet.copyOf(Object.class.getMethods())))) + + private static final LoadingCache, Set> delegationMapCache = CacheBuilder + .newBuilder().build(new CacheLoader, Set>() { + public Set load(final Class interfaceType) throws ExecutionException { + return FluentIterable.from(ImmutableSet.copyOf(interfaceType.getMethods())) + .filter(not(in(ImmutableSet.copyOf(Object.class.getMethods())))) .transform(new Function>() { public Invokable apply(Method in) { return Invokable.from(in); @@ -194,7 +180,7 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { }).filter(new Predicate>() { public boolean apply(Invokable in) { return in.isAnnotationPresent(Path.class) || tryFindHttpMethod(in).isPresent() - || any(in.getParameters(), new Predicate(){ + || any(in.getParameters(), new Predicate() { public boolean apply(Parameter in) { return in.getType().getRawType().isAssignableFrom(HttpRequest.class); } @@ -204,86 +190,97 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { public boolean apply(Invokable in) { return in.getReturnType().getRawType().isAssignableFrom(ListenableFuture.class); } - - }); - return Maps.uniqueIndex(methodsToProcess, HashSignatureExceptReturnVal.INSTANCE).keySet(); + }).transform(new Function, InterfaceNameAndParameters>() { + public InterfaceNameAndParameters apply(Invokable in) { + return new InterfaceNameAndParameters(interfaceType, in.getName(), in.getParameters()); + } + }).toSet(); } }); - private static enum HashSignatureExceptReturnVal implements Function, Integer> { - INSTANCE; - public Integer apply(Invokable in) { - int parametersTypeHashCode = hashParameterTypes(in); - return Objects.hashCode(in.getDeclaringClass(), in.getName(), parametersTypeHashCode); + private static final class InterfaceNameAndParameters { + private final Class interfaceType; + private final String name; + private final int parametersTypeHashCode; + + private InterfaceNameAndParameters(Class interfaceType, String name, ImmutableList parameters) { + this.interfaceType = interfaceType; + this.name = name; + int parametersTypeHashCode = 0; + for (Parameter param : parameters) + parametersTypeHashCode += param.getType().hashCode(); + this.parametersTypeHashCode = parametersTypeHashCode; + } + + public int hashCode() { + return Objects.hashCode(interfaceType, name, parametersTypeHashCode); + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + InterfaceNameAndParameters that = InterfaceNameAndParameters.class.cast(o); + return equal(this.interfaceType, that.interfaceType) && equal(this.name, that.name) + && equal(this.parametersTypeHashCode, that.parametersTypeHashCode); } } - private static int hashParameterTypes(Invokable in) { - int parametersTypeHashCode = 0; - for (Parameter param : in.getParameters()) - parametersTypeHashCode += param.getType().hashCode(); - return parametersTypeHashCode; - } - - - private AsyncRestClientProxy(Injector injector, - Function> optionalConverter, HttpCommandExecutorService http, - ExecutorService userThreads, LoadingCache delegateMap, - RestAnnotationProcessor annotationProcessor, ParseSax.Factory parserFactory, Class declaring) { + @Inject + private AsyncRestClientProxy(Injector injector, Function> optionalConverter, + HttpCommandExecutorService http, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads, + Caller.Factory factory, RestAnnotationProcessor annotationProcessor, ParseSax.Factory parserFactory) { this.injector = injector; this.optionalConverter = optionalConverter; this.http = http; this.userThreads = userThreads; - this.delegateMap = delegateMap; + this.factory = factory; this.annotationProcessor = annotationProcessor; this.parserFactory = parserFactory; - this.declaring = declaring; } private static final Predicate isQualifierPresent = new Predicate() { - - @Override public boolean apply(Annotation input) { return input.annotationType().isAnnotationPresent(Qualifier.class); } - }; @Override - protected Object handleInvocation(Object proxy, Invokable method, List args) throws Throwable { - if (method.isAnnotationPresent(Provides.class)) { - return lookupValueFromGuice(method); - } else if (method.isAnnotationPresent(Delegate.class)) { - return propagateContextToDelegate(method, args); - } else if (isAsyncOrDelegate(method)) { - return createListenableFutureForHttpRequestMappedToMethodAndArgs(method, args); + public Result apply(Invocation invocation) { + if (invocation.getInvokable().isAnnotationPresent(Provides.class)) { + return Result.success(lookupValueFromGuice(invocation.getInvokable())); + } else if (invocation.getInvokable().isAnnotationPresent(Delegate.class)) { + return Result.success(propagateContextToDelegate(invocation)); + } else if (isAsyncOrDelegate(invocation)) { + return Result.success(createListenableFutureForHttpRequestMappedToMethodAndArgs(invocation)); } else { - throw new RuntimeException(String.format("Method is not annotated as either http or provider method: %s", - method)); + return Result.fail(new IllegalStateException(String.format( + "Method is not annotated as either http or provider invoked: %s", invocation.getInvokable()))); } } - private boolean isAsyncOrDelegate(Invokable method) { - return delegationMapCache.getUnchecked(declaring).contains(HashSignatureExceptReturnVal.INSTANCE.apply(method)); + private boolean isAsyncOrDelegate(Invocation invocation) { + return delegationMapCache.getUnchecked(invocation.getInterfaceType()).contains( + new InterfaceNameAndParameters(invocation.getInterfaceType(), invocation.getInvokable().getName(), + invocation.getInvokable().getParameters())); } - private Object propagateContextToDelegate(Invokable method, List args) throws ExecutionException { - Class asyncClass = returnTypeOrTypeOfOptional(method); - ClassInvokerArgs cma = ClassInvokerArgs.builder().clazz(asyncClass).invoker(method).args(args).build(); - Object returnVal = delegateMap.get(cma); - if (isReturnTypeOptional(method)) { - ClassInvokerArgsAndReturnVal cmar = ClassInvokerArgsAndReturnVal.builder().fromClassInvokerArgs(cma) - .returnVal(returnVal).build(); - return optionalConverter.apply(cmar); + private Object propagateContextToDelegate(Invocation invocation) { + Class returnType = unwrapIfOptional(invocation.getInvokable().getReturnType()); + Object result = FunctionalReflection.newProxy(returnType, factory.caller(invocation, returnType)); + if (isReturnTypeOptional(invocation.getInvokable())) { + return optionalConverter.apply(InvocationSuccess.create(invocation, result)); } - return returnVal; + return result; } - private Object lookupValueFromGuice(Invokable method) { + private Object lookupValueFromGuice(Invokable invoked) { try { - Type genericReturnType = method.getReturnType().getType(); + Type genericReturnType = invoked.getReturnType().getType(); try { - Annotation qualifier = find(ImmutableList.copyOf(method.getAnnotations()), isQualifierPresent); + Annotation qualifier = find(ImmutableList.copyOf(invoked.getAnnotations()), isQualifierPresent); return getInstanceOfTypeWithQualifier(genericReturnType, qualifier); } catch (ProvisionException e) { throw propagate(e.getCause()); @@ -336,19 +333,20 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting static Function createResponseParser(ParseSax.Factory parserFactory, Injector injector, - Invokable method, HttpRequest request) { + Invocation invocation, HttpRequest request) { Function transformer; - Class> handler = getSaxResponseParserClassOrNull(method); + Class> handler = getSaxResponseParserClassOrNull(invocation.getInvokable()); if (handler != null) { transformer = parserFactory.create(injector.getInstance(handler)); } else { - transformer = getTransformerForMethod(method, injector); + transformer = getTransformerForMethod(invocation, injector); } if (transformer instanceof InvocationContext) { ((InvocationContext) transformer).setContext(request); } - if (method.isAnnotationPresent(Transform.class)) { - Function wrappingTransformer = injector.getInstance(method.getAnnotation(Transform.class).value()); + if (invocation.getInvokable().isAnnotationPresent(Transform.class)) { + Function wrappingTransformer = injector.getInstance(invocation.getInvokable() + .getAnnotation(Transform.class).value()); if (wrappingTransformer instanceof InvocationContext) { ((InvocationContext) wrappingTransformer).setContext(request); } @@ -357,8 +355,8 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { return transformer; } - private static Class> getSaxResponseParserClassOrNull(Invokable method) { - XMLResponseParser annotation = method.getAnnotation(XMLResponseParser.class); + private static Class> getSaxResponseParserClassOrNull(Invokable invoked) { + XMLResponseParser annotation = invoked.getAnnotation(XMLResponseParser.class); if (annotation != null) { return annotation.value(); } @@ -368,40 +366,40 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { // TODO: refactor this out of here @VisibleForTesting @SuppressWarnings({ "rawtypes", "unchecked" }) - public static Function getTransformerForMethod(Invokable method, Injector injector) { + public static Function getTransformerForMethod(Invocation invocation, Injector injector) { + Invokable invoked = invocation.getInvokable(); Function transformer; - if (method.isAnnotationPresent(SelectJson.class)) { - Type returnVal = getReturnTypeFor(method.getReturnType()); - if (method.isAnnotationPresent(OnlyElement.class)) + if (invoked.isAnnotationPresent(SelectJson.class)) { + Type returnVal = getReturnTypeFor(invoked.getReturnType()); + if (invoked.isAnnotationPresent(OnlyElement.class)) returnVal = newParameterizedType(Set.class, returnVal); transformer = new ParseFirstJsonValueNamed(injector.getInstance(GsonWrapper.class), - TypeLiteral.get(returnVal), method.getAnnotation(SelectJson.class).value()); - if (method.isAnnotationPresent(OnlyElement.class)) + TypeLiteral.get(returnVal), invoked.getAnnotation(SelectJson.class).value()); + if (invoked.isAnnotationPresent(OnlyElement.class)) transformer = compose(new OnlyElementOrNull(), transformer); } else { - transformer = injector.getInstance(getParserOrThrowException(method)); + transformer = injector.getInstance(getParserOrThrowException(invocation)); } return transformer; } - private ListenableFuture createListenableFutureForHttpRequestMappedToMethodAndArgs(Invokable invoker, - List args) { - String name = invoker.getDeclaringClass().getSimpleName() + "." + invoker.getName(); + private ListenableFuture createListenableFutureForHttpRequestMappedToMethodAndArgs(Invocation invocation) { + String name = invocation.getInterfaceType().getSimpleName() + "." + invocation.getInvokable().getName(); logger.trace(">> converting %s", name); - FutureFallback fallback = fallbacks.getUnchecked(invoker); + FutureFallback fallback = fallbacks.getUnchecked(invocation.getInvokable()); // in case there is an exception creating the request, we should at least pass in args if (fallback instanceof InvocationContext) { InvocationContext.class.cast(fallback).setContext((HttpRequest) null); } ListenableFuture result; try { - GeneratedHttpRequest request = annotationProcessor.createRequest(invoker, args); + GeneratedHttpRequest request = annotationProcessor.apply(invocation); if (fallback instanceof InvocationContext) { InvocationContext.class.cast(fallback).setContext(request); } logger.trace("<< converted %s to %s", name, request.getRequestLine()); - Function transformer = createResponseParser(parserFactory, injector, invoker, request); + Function transformer = createResponseParser(parserFactory, injector, invocation, request); logger.trace("<< response from %s is parsed by %s", name, transformer.getClass().getSimpleName()); logger.debug(">> invoking %s", name); @@ -422,7 +420,7 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { @Override public String toString() { - return "Client Proxy for :" + declaring.getName(); + return String.format("async->http"); } private final LoadingCache, FutureFallback> fallbacks = CacheBuilder.newBuilder().build( @@ -460,57 +458,57 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting - static Key> getParserOrThrowException(Invokable method) { - - ResponseParser annotation = method.getAnnotation(ResponseParser.class); + static Key> getParserOrThrowException(Invocation invocation) { + Invokable invoked = invocation.getInvokable(); + ResponseParser annotation = invoked.getAnnotation(ResponseParser.class); if (annotation == null) { - if (method.getReturnType().equals(void.class) || method.getReturnType().equals(futureVoidLiteral)) { + if (invoked.getReturnType().equals(void.class) || invoked.getReturnType().equals(futureVoidLiteral)) { return Key.get(ReleasePayloadAndReturn.class); - } else if (method.getReturnType().equals(boolean.class) || method.getReturnType().equals(Boolean.class) - || method.getReturnType().equals(futureBooleanLiteral)) { + } else if (invoked.getReturnType().equals(boolean.class) || invoked.getReturnType().equals(Boolean.class) + || invoked.getReturnType().equals(futureBooleanLiteral)) { return Key.get(ReturnTrueIf2xx.class); - } else if (method.getReturnType().equals(InputStream.class) - || method.getReturnType().equals(futureInputStreamLiteral)) { + } else if (invoked.getReturnType().equals(InputStream.class) + || invoked.getReturnType().equals(futureInputStreamLiteral)) { return Key.get(ReturnInputStream.class); - } else if (method.getReturnType().equals(HttpResponse.class) - || method.getReturnType().equals(futureHttpResponseLiteral)) { + } else if (invoked.getReturnType().equals(HttpResponse.class) + || invoked.getReturnType().equals(futureHttpResponseLiteral)) { return Key.get(Class.class.cast(IdentityFunction.class)); - } else if (RestAnnotationProcessor.getAcceptHeaders(method).contains(APPLICATION_JSON)) { - return getJsonParserKeyForMethod(method); - } else if (RestAnnotationProcessor.getAcceptHeaders(method).contains(APPLICATION_XML) - || method.isAnnotationPresent(JAXBResponseParser.class)) { - return getJAXBParserKeyForMethod(method); - } else if (method.getReturnType().equals(String.class) || method.getReturnType().equals(futureStringLiteral)) { + } else if (RestAnnotationProcessor.getAcceptHeaders(invocation).contains(APPLICATION_JSON)) { + return getJsonParserKeyForMethod(invoked); + } else if (RestAnnotationProcessor.getAcceptHeaders(invocation).contains(APPLICATION_XML) + || invoked.isAnnotationPresent(JAXBResponseParser.class)) { + return getJAXBParserKeyForMethod(invoked); + } else if (invoked.getReturnType().equals(String.class) || invoked.getReturnType().equals(futureStringLiteral)) { return Key.get(ReturnStringIf2xx.class); - } else if (method.getReturnType().equals(URI.class) || method.getReturnType().equals(futureURILiteral)) { + } else if (invoked.getReturnType().equals(URI.class) || invoked.getReturnType().equals(futureURILiteral)) { return Key.get(ParseURIFromListOrLocationHeaderIf20x.class); } else { - throw new IllegalStateException("You must specify a ResponseParser annotation on: " + method.toString()); + throw new IllegalStateException("You must specify a ResponseParser annotation on: " + invoked.toString()); } } return Key.get(annotation.value()); } @SuppressWarnings("unchecked") - private static Key> getJAXBParserKeyForMethod(Invokable method) { + private static Key> getJAXBParserKeyForMethod(Invokable invoked) { Optional configuredReturnVal = Optional.absent(); - if (method.isAnnotationPresent(JAXBResponseParser.class)) { - Type configuredClass = method.getAnnotation(JAXBResponseParser.class).value(); + if (invoked.isAnnotationPresent(JAXBResponseParser.class)) { + Type configuredClass = invoked.getAnnotation(JAXBResponseParser.class).value(); configuredReturnVal = configuredClass.equals(NullType.class) ? Optional. absent() : Optional . of(configuredClass); } - Type returnVal = configuredReturnVal.or(getReturnTypeFor(method.getReturnType())); + Type returnVal = configuredReturnVal.or(getReturnTypeFor(invoked.getReturnType())); Type parserType = newParameterizedType(ParseXMLWithJAXB.class, returnVal); return (Key>) Key.get(parserType); } @SuppressWarnings({ "unchecked" }) - private static Key> getJsonParserKeyForMethod(Invokable method) { + private static Key> getJsonParserKeyForMethod(Invokable invoked) { ParameterizedType parserType; - if (method.isAnnotationPresent(Unwrap.class)) { - parserType = newParameterizedType(UnwrapOnlyJsonValue.class, getReturnTypeFor(method.getReturnType())); + if (invoked.isAnnotationPresent(Unwrap.class)) { + parserType = newParameterizedType(UnwrapOnlyJsonValue.class, getReturnTypeFor(invoked.getReturnType())); } else { - parserType = newParameterizedType(ParseJson.class, getReturnTypeFor(method.getReturnType())); + parserType = newParameterizedType(ParseJson.class, getReturnTypeFor(invoked.getReturnType())); } return (Key>) Key.get(parserType); } @@ -527,4 +525,5 @@ public abstract class AsyncRestClientProxy extends AbstractInvocationHandler { } return returnVal; } + } diff --git a/core/src/main/java/org/jclouds/rest/internal/CreateAsyncClientForCaller.java b/core/src/main/java/org/jclouds/rest/internal/CreateAsyncClientForCaller.java deleted file mode 100644 index 8d679bafb3..0000000000 --- a/core/src/main/java/org/jclouds/rest/internal/CreateAsyncClientForCaller.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.rest.internal; - -import static com.google.common.reflect.Reflection.newProxy; - -import org.jclouds.internal.ClassInvokerArgs; -import org.jclouds.rest.internal.AsyncRestClientProxy.Factory; - -import com.google.common.cache.CacheLoader; -import com.google.inject.Inject; - -public final class CreateAsyncClientForCaller extends CacheLoader { - private final Factory factory; - - @Inject - private CreateAsyncClientForCaller(AsyncRestClientProxy.Factory factory) { - this.factory = factory; - } - - @Override - public Object load(ClassInvokerArgs from) { - return newProxy(from.getClazz(), factory.caller(from)); - } -} \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/rest/internal/CreateClientForCaller.java b/core/src/main/java/org/jclouds/rest/internal/CreateClientForCaller.java deleted file mode 100644 index 25bcf9f00f..0000000000 --- a/core/src/main/java/org/jclouds/rest/internal/CreateClientForCaller.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.rest.internal; - -import static com.google.common.base.Preconditions.checkState; -import static com.google.common.reflect.Reflection.newProxy; - -import java.util.Map; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.jclouds.concurrent.internal.SyncProxy; -import org.jclouds.internal.ClassInvokerArgs; -import org.jclouds.util.Optionals2; - -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; - -/** - * CreateClientForCaller is parameterized, so clients it creates aren't singletons. For example, CreateClientForCaller - * satisfies a call like this: {@code context.getProviderSpecificContext().getApi().getOrgClientForName(name)} - * - * @author Adrian Cole - */ -public class CreateClientForCaller extends CacheLoader { - private final SyncProxy.Factory factory; - private final LoadingCache asyncMap; - private final Map, Class> sync2Async; - - @Inject - private CreateClientForCaller(SyncProxy.Factory factory, - @Named("async") LoadingCache asyncMap, Map, Class> sync2Async) { - this.factory = factory; - this.asyncMap = asyncMap; - this.sync2Async = sync2Async; - } - - @Override - public Object load(ClassInvokerArgs from) { - Class syncClass = Optionals2.returnTypeOrTypeOfOptional(from.getInvoker()); - Class asyncClass = sync2Async.get(syncClass); - checkState(asyncClass != null, "configuration error, sync class " + syncClass + " not mapped to an async class"); - Object asyncClient = asyncMap.getUnchecked(from); - checkState(asyncClient != null, "configuration error, sync client for " + from + " not found"); - return newProxy(syncClass, factory.create(syncClass, asyncClient)); - } -} diff --git a/core/src/main/java/org/jclouds/rest/internal/GeneratedHttpRequest.java b/core/src/main/java/org/jclouds/rest/internal/GeneratedHttpRequest.java index d66ef3c4d8..2db7a528a4 100644 --- a/core/src/main/java/org/jclouds/rest/internal/GeneratedHttpRequest.java +++ b/core/src/main/java/org/jclouds/rest/internal/GeneratedHttpRequest.java @@ -21,28 +21,22 @@ package org.jclouds.rest.internal; import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequestFilter; -import org.jclouds.internal.ClassInvokerArgs; import org.jclouds.io.Payload; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.reflect.Invocation; import com.google.common.base.Optional; -import com.google.common.collect.Lists; import com.google.common.collect.Multimap; -import com.google.common.reflect.Invokable; /** * Represents a request generated from annotations * * @author Adrian Cole */ -// TODO: get rid of all the mock tests so that this can be made final -public class GeneratedHttpRequest extends HttpRequest { +public final class GeneratedHttpRequest extends HttpRequest { public static Builder builder() { return new Builder(); } @@ -51,70 +45,34 @@ public class GeneratedHttpRequest extends HttpRequest { return new Builder().fromGeneratedHttpRequest(this); } - public static class Builder extends HttpRequest.Builder { - protected Class declaring; - protected Invokable invoker; - // args can be null, so cannot use immutable list - protected List args = Lists.newArrayList(); - protected Optional caller = Optional.absent(); - - /** - * @see GeneratedHttpRequest#getDeclaring() - */ - public Builder declaring(Class declaring) { - this.declaring = checkNotNull(declaring, "declaring"); - return this; - } + public final static class Builder extends HttpRequest.Builder { + protected Invocation invocation; + protected Optional caller = Optional.absent(); /** - * @see GeneratedHttpRequest#getInvoker() + * @see GeneratedHttpRequest#getInvocation() */ - public Builder invoker(Invokable invoker) { - this.invoker = checkNotNull(invoker, "invoker"); + public Builder invocation(Invocation invocation) { + this.invocation = checkNotNull(invocation, "invocation"); return this; } - /** - * @see GeneratedHttpRequest#getArgs() - */ - public Builder args(Iterable args) { - this.args = Lists.newArrayList(checkNotNull(args, "args")); - return this; - } - - /** - * @see GeneratedHttpRequest#getArgs() - */ - public Builder args(@Nullable Object[] args) { - return args(Arrays.asList(args != null ? args : new Object[] {})); - } - - /** - * @see GeneratedHttpRequest#getArgs() - */ - public Builder arg(@Nullable Object arg) { - this.args.add(arg); - return this; - } - /** * @see GeneratedHttpRequest#getCaller() */ - public Builder caller(@Nullable ClassInvokerArgs caller) { + public Builder caller(@Nullable Invocation caller) { this.caller = Optional.fromNullable(caller); return this; } public GeneratedHttpRequest build() { - return new GeneratedHttpRequest(method, endpoint, headers.build(), payload, declaring, invoker, args, - filters.build(), caller); + return new GeneratedHttpRequest(method, endpoint, headers.build(), payload, invocation, filters.build(), + caller); } public Builder fromGeneratedHttpRequest(GeneratedHttpRequest in) { return super.fromHttpRequest(in) - .declaring(in.getDeclaring()) - .invoker(in.invoker) - .args(in.getArgs()) + .invocation(in.invocation) .caller(in.getCaller().orNull()); } @@ -124,35 +82,25 @@ public class GeneratedHttpRequest extends HttpRequest { } } - private final Class declaring; - private final Invokable invoker; - private final List args; - private final Optional caller; + private final Invocation invocation; + private final Optional caller; protected GeneratedHttpRequest(String method, URI endpoint, Multimap headers, - @Nullable Payload payload, Class declaring, Invokable invoker, - List args, Iterable filters, Optional caller) { + @Nullable Payload payload, Invocation invocation, Iterable filters, + Optional caller) { super(method, endpoint, headers, payload, filters); - this.declaring = checkNotNull(declaring, "declaring"); - this.invoker = checkNotNull(invoker, "invoker"); - // TODO make immutable. ImmutableList.of() doesn't accept nulls - this.args = Collections.unmodifiableList(checkNotNull(args, "args")); + this.invocation = checkNotNull(invocation, "invocation"); this.caller = checkNotNull(caller, "caller"); } - public Class getDeclaring() { - return declaring; + /** + * what was interpreted to create this request + */ + public Invocation getInvocation() { + return invocation; } - public Invokable getInvoker() { - return invoker; - } - - public List getArgs() { - return args; - } - - public Optional getCaller() { + public Optional getCaller() { return caller; } } 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 b610605719..b786bd9d31 100644 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -17,6 +17,7 @@ * under the License. */ package org.jclouds.rest.internal; + import static com.google.common.base.Functions.toStringFunction; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -67,7 +68,6 @@ import org.jclouds.http.HttpRequestFilter; import org.jclouds.http.HttpUtils; import org.jclouds.http.Uris.UriBuilder; import org.jclouds.http.options.HttpRequestOptions; -import org.jclouds.internal.ClassInvokerArgs; import org.jclouds.io.ContentMetadataCodec; import org.jclouds.io.Payload; import org.jclouds.io.PayloadEnclosing; @@ -77,6 +77,7 @@ import org.jclouds.io.payloads.Part; import org.jclouds.io.payloads.Part.PartOptions; import org.jclouds.javax.annotation.Nullable; import org.jclouds.logging.Logger; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.Binder; import org.jclouds.rest.InputParamValidator; import org.jclouds.rest.annotations.ApiVersion; @@ -127,34 +128,25 @@ import com.google.inject.TypeLiteral; import com.google.inject.assistedinject.Assisted; /** - * Creates http methods based on annotations on a class or interface. - * + * Creates http invocation.getInvoked()s based on annotations on a class or interface. + * * @author Adrian Cole */ -public abstract class RestAnnotationProcessor { - - public static interface Factory { - Declaring declaring(Class declaring); - Caller caller(ClassInvokerArgs caller); - } - - public static final class Declaring extends RestAnnotationProcessor { - @Inject - private Declaring(Injector injector, @ApiVersion String apiVersion, @BuildVersion String buildVersion, - HttpUtils utils, ContentMetadataCodec contentMetadataCodec, InputParamValidator inputParamValidator, - @Assisted Class declaring) { - super(injector, apiVersion, buildVersion, utils, contentMetadataCodec, inputParamValidator, declaring); - } - } +public class RestAnnotationProcessor implements Function { public static final class Caller extends RestAnnotationProcessor { - private final ClassInvokerArgs caller; + + public static interface Factory { + Caller caller(Invocation caller); + } + + private final Invocation caller; @Inject private Caller(Injector injector, @ApiVersion String apiVersion, @BuildVersion String buildVersion, HttpUtils utils, ContentMetadataCodec contentMetadataCodec, InputParamValidator inputParamValidator, - @Assisted ClassInvokerArgs caller) { - super(injector, apiVersion, buildVersion, utils, contentMetadataCodec, inputParamValidator, caller.getClazz()); + @Assisted Invocation caller) { + super(injector, apiVersion, buildVersion, utils, contentMetadataCodec, inputParamValidator); this.caller = caller; } @@ -164,29 +156,22 @@ public abstract class RestAnnotationProcessor { } @Override - protected Optional findEndpoint(Invokable method, List args) { - Optional endpoint = getEndpointFor(caller.getInvoker(), caller.getArgs()); + protected Optional findEndpoint(Invocation invocation) { + Optional endpoint = getEndpointFor(caller); if (endpoint.isPresent()) - logger.trace("using endpoint %s from caller %s for %s", endpoint, caller, cma(method, args)); + logger.trace("using endpoint %s from caller %s for %s", endpoint, caller, invocation); else - endpoint = super.findEndpoint(method, args); + endpoint = super.findEndpoint(invocation); return endpoint; } @Override - protected Multimap addPathAndGetTokens(Class clazz, Invokable method, List args, - UriBuilder uriBuilder) { - Class callerClass = caller.getInvoker().getDeclaringClass(); - return ImmutableMultimap. builder() - .putAll(super.addPathAndGetTokens(callerClass, caller.getInvoker(), caller.getArgs(), uriBuilder)) - .putAll(super.addPathAndGetTokens(clazz, method, args, uriBuilder)).build(); + protected Multimap addPathAndGetTokens(Invocation invocation, UriBuilder uriBuilder) { + return ImmutableMultimap. builder().putAll(super.addPathAndGetTokens(caller, uriBuilder)) + .putAll(super.addPathAndGetTokens(invocation, uriBuilder)).build(); } } - protected ClassInvokerArgs cma(Invokable method, List args) { - return logger.isTraceEnabled() ? new ClassInvokerArgs(method.getDeclaringClass(), method, args) : null; - } - @Resource protected Logger logger = Logger.NULL; @@ -197,75 +182,79 @@ public abstract class RestAnnotationProcessor { } }; - private final Class declaring; private final Injector injector; private final HttpUtils utils; private final ContentMetadataCodec contentMetadataCodec; private final String apiVersion; private final String buildVersion; private final InputParamValidator inputParamValidator; - - private RestAnnotationProcessor(Injector injector, String apiVersion, String buildVersion, - HttpUtils utils, ContentMetadataCodec contentMetadataCodec, - InputParamValidator inputParamValidator, Class declaring) { + + @Inject + private RestAnnotationProcessor(Injector injector, @ApiVersion String apiVersion, @BuildVersion String buildVersion, + HttpUtils utils, ContentMetadataCodec contentMetadataCodec, InputParamValidator inputParamValidator) { this.injector = injector; this.utils = utils; this.contentMetadataCodec = contentMetadataCodec; this.apiVersion = apiVersion; this.buildVersion = buildVersion; this.inputParamValidator = inputParamValidator; - this.declaring = declaring; } - public GeneratedHttpRequest createRequest(Invokable invoker, List args) { - checkNotNull(invoker, "invoker"); - checkNotNull(args, "args"); - inputParamValidator.validateMethodParametersOrThrow(invoker, args); + /** + * Note this is dangerous as it cannot pass the inheriting class! Using this when subclassing interfaces may result + * in lost data. + */ + @Deprecated + public GeneratedHttpRequest createRequest(Invokable invokable, List args) { + return apply(Invocation.create(invokable, args)); + } + + @Override + public GeneratedHttpRequest apply(Invocation invocation) { + checkNotNull(invocation, "invocation"); + inputParamValidator.validateMethodParametersOrThrow(invocation); Optional endpoint = Optional.absent(); - HttpRequest r = findOrNull(args, HttpRequest.class); + HttpRequest r = findOrNull(invocation.getArgs(), HttpRequest.class); if (r != null) { endpoint = Optional.fromNullable(r.getEndpoint()); if (endpoint.isPresent()) - logger.trace("using endpoint %s from args for %s", endpoint, cma(invoker, args)); + logger.trace("using endpoint %s from invocation.getArgs() for %s", endpoint, invocation); } else { - endpoint = findEndpoint(invoker, args); + endpoint = findEndpoint(invocation); } if (!endpoint.isPresent()) - throw new NoSuchElementException(format("no endpoint found for %s", cma(invoker, args))); + throw new NoSuchElementException(format("no endpoint found for %s", invocation)); GeneratedHttpRequest.Builder requestBuilder = requestBuilder(); if (r != null) { requestBuilder.fromHttpRequest(r); } else { - requestBuilder.method(tryFindHttpMethod(invoker).get()); + requestBuilder.method(tryFindHttpMethod(invocation.getInvokable()).get()); } - - requestBuilder.declaring(declaring) - .invoker(invoker) - .args(args) - .filters(getFiltersIfAnnotated(invoker)); - + + requestBuilder.invocation(invocation).filters(getFiltersIfAnnotated(invocation)); + Multimap tokenValues = LinkedHashMultimap.create(); tokenValues.put(Constants.PROPERTY_API_VERSION, apiVersion); tokenValues.put(Constants.PROPERTY_BUILD_VERSION, buildVersion); - + UriBuilder uriBuilder = uriBuilder(endpoint.get().toString()); // URI template in rfc6570 form - - overridePathEncoding(uriBuilder, invoker); - - tokenValues.putAll(addPathAndGetTokens(declaring, invoker, args, uriBuilder)); - - Multimap formParams = addFormParams(tokenValues, invoker, args); - Multimap queryParams = addQueryParams(tokenValues, invoker, args); - Multimap headers = buildHeaders(tokenValues, invoker, args); + + overridePathEncoding(uriBuilder, invocation); + + tokenValues.putAll(addPathAndGetTokens(invocation, uriBuilder)); + + Multimap formParams = addFormParams(tokenValues, invocation); + Multimap queryParams = addQueryParams(tokenValues, invocation); + Multimap headers = buildHeaders(tokenValues, invocation); if (r != null) headers.putAll(r.getHeaders()); - if (shouldAddHostHeader(invoker)) { + if (shouldAddHostHeader(invocation)) { StringBuilder hostHeader = new StringBuilder(endpoint.get().getHost()); if (endpoint.get().getPort() != -1) hostHeader.append(":").append(endpoint.get().getPort()); @@ -273,7 +262,7 @@ public abstract class RestAnnotationProcessor { } Payload payload = null; - for(HttpRequestOptions options : findOptionsIn(invoker, args)) { + for (HttpRequestOptions options : findOptionsIn(invocation)) { injector.injectMembers(options);// TODO test case for (Entry header : options.buildRequestHeaders().entries()) { headers.put(header.getKey(), replaceTokens(header.getValue(), tokenValues)); @@ -293,7 +282,7 @@ public abstract class RestAnnotationProcessor { if (stringPayload != null) payload = Payloads.newStringPayload(stringPayload); } - + if (queryParams.size() > 0) { uriBuilder.query(queryParams); } @@ -301,16 +290,16 @@ public abstract class RestAnnotationProcessor { requestBuilder.headers(filterOutContentHeaders(headers)); requestBuilder.endpoint(uriBuilder.build(convertUnsafe(tokenValues))); - + if (payload == null) { - PayloadEnclosing payloadEnclosing = findOrNull(args, PayloadEnclosing.class); - payload = (payloadEnclosing != null) ? payloadEnclosing.getPayload() : findOrNull(args, Payload.class); + PayloadEnclosing payloadEnclosing = findOrNull(invocation.getArgs(), PayloadEnclosing.class); + payload = (payloadEnclosing != null) ? payloadEnclosing.getPayload() : findOrNull(invocation.getArgs(), + Payload.class); } - List parts = getParts(invoker, args, ImmutableMultimap. builder() - .putAll(tokenValues) - .putAll(formParams).build()); - + List parts = getParts(invocation, ImmutableMultimap. builder() + .putAll(tokenValues).putAll(formParams).build()); + if (parts.size() > 0) { if (formParams.size() > 0) { parts = newLinkedList(concat(transform(formParams.entries(), ENTRY_TO_PART), parts)); @@ -328,11 +317,11 @@ public abstract class RestAnnotationProcessor { } GeneratedHttpRequest request = requestBuilder.build(); - org.jclouds.rest.MapBinder mapBinder = getMapPayloadBinderOrNull(invoker, args); + org.jclouds.rest.MapBinder mapBinder = getMapPayloadBinderOrNull(invocation); if (mapBinder != null) { - Map mapParams = buildPayloadParams(invoker, args); - if (invoker.isAnnotationPresent(PayloadParams.class)) { - PayloadParams params = invoker.getAnnotation(PayloadParams.class); + Map mapParams = buildPayloadParams(invocation); + if (invocation.getInvokable().isAnnotationPresent(PayloadParams.class)) { + PayloadParams params = invocation.getInvokable().getAnnotation(PayloadParams.class); addMapPayload(mapParams, params, headers); } request = mapBinder.bindToRequest(request, mapParams); @@ -347,10 +336,10 @@ public abstract class RestAnnotationProcessor { return request; } - private T findOrNull(Iterable args, Class clazz) { + private static T findOrNull(Iterable args, Class clazz) { return clazz.cast(tryFind(args, instanceOf(clazz)).orNull()); } - + private static Map convertUnsafe(Multimap in) { LinkedHashMap out = Maps.newLinkedHashMap(); for (Entry entry : in.entries()) { @@ -358,17 +347,18 @@ public abstract class RestAnnotationProcessor { } return ImmutableMap.copyOf(out); } - + protected org.jclouds.rest.internal.GeneratedHttpRequest.Builder requestBuilder() { return GeneratedHttpRequest.builder(); } - private void overridePathEncoding(UriBuilder uriBuilder, Invokable method) { - if (declaring.isAnnotationPresent(SkipEncoding.class)) { - uriBuilder.skipPathEncoding(Chars.asList(declaring.getAnnotation(SkipEncoding.class).value())); + private void overridePathEncoding(UriBuilder uriBuilder, Invocation invocation) { + if (invocation.getInterfaceType().isAnnotationPresent(SkipEncoding.class)) { + uriBuilder.skipPathEncoding(Chars.asList(invocation.getInterfaceType().getAnnotation(SkipEncoding.class) + .value())); } - if (method.isAnnotationPresent(SkipEncoding.class)) { - uriBuilder.skipPathEncoding(Chars.asList(method.getAnnotation(SkipEncoding.class).value())); + if (invocation.getInvokable().isAnnotationPresent(SkipEncoding.class)) { + uriBuilder.skipPathEncoding(Chars.asList(invocation.getInvokable().getAnnotation(SkipEncoding.class).value())); } } @@ -382,64 +372,60 @@ public abstract class RestAnnotationProcessor { return o.toString(); } } - - protected Optional findEndpoint(Invokable method, List args) { - ClassInvokerArgs cma = cma(method, args); - Optional endpoint = getEndpointFor(method, args); + + protected Optional findEndpoint(Invocation invocation) { + Optional endpoint = getEndpointFor(invocation); if (endpoint.isPresent()) - logger.trace("using endpoint %s for %s", endpoint, cma); + logger.trace("using endpoint %s for %s", endpoint, invocation); if (!endpoint.isPresent()) { - logger.trace("looking up default endpoint for %s", cma); + logger.trace("looking up default endpoint for %s", invocation); endpoint = Optional.fromNullable(injector.getInstance( Key.get(uriSupplierLiteral, org.jclouds.location.Provider.class)).get()); if (endpoint.isPresent()) - logger.trace("using default endpoint %s for %s", endpoint, cma); + logger.trace("using default endpoint %s for %s", endpoint, invocation); } return endpoint; } - protected Multimap addPathAndGetTokens(Class clazz, Invokable method, List args, - UriBuilder uriBuilder) { - if (clazz.isAnnotationPresent(Path.class)) - uriBuilder.appendPath(clazz.getAnnotation(Path.class).value()); - if (method.isAnnotationPresent(Path.class)) - uriBuilder.appendPath(method.getAnnotation(Path.class).value()); - return getPathParamKeyValues(method, args); + protected Multimap addPathAndGetTokens(Invocation invocation, UriBuilder uriBuilder) { + if (invocation.getInterfaceType().isAnnotationPresent(Path.class)) + uriBuilder.appendPath(invocation.getInterfaceType().getAnnotation(Path.class).value()); + if (invocation.getInvokable().isAnnotationPresent(Path.class)) + uriBuilder.appendPath(invocation.getInvokable().getAnnotation(Path.class).value()); + return getPathParamKeyValues(invocation); } - private Multimap addFormParams(Multimap tokenValues, Invokable method, - List args) { + private Multimap addFormParams(Multimap tokenValues, Invocation invocation) { Multimap formMap = LinkedListMultimap.create(); - if (declaring.isAnnotationPresent(FormParams.class)) { - FormParams form = declaring.getAnnotation(FormParams.class); + if (invocation.getInterfaceType().isAnnotationPresent(FormParams.class)) { + FormParams form = invocation.getInterfaceType().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } - if (method.isAnnotationPresent(FormParams.class)) { - FormParams form = method.getAnnotation(FormParams.class); + if (invocation.getInvokable().isAnnotationPresent(FormParams.class)) { + FormParams form = invocation.getInvokable().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } - for (Entry form : getFormParamKeyValues(method, args).entries()) { + for (Entry form : getFormParamKeyValues(invocation).entries()) { formMap.put(form.getKey(), replaceTokens(form.getValue().toString(), tokenValues)); } return formMap; } - private Multimap addQueryParams(Multimap tokenValues, Invokable method, - List args) { + private Multimap addQueryParams(Multimap tokenValues, Invocation invocation) { Multimap queryMap = LinkedListMultimap.create(); - if (declaring.isAnnotationPresent(QueryParams.class)) { - QueryParams query = declaring.getAnnotation(QueryParams.class); + if (invocation.getInterfaceType().isAnnotationPresent(QueryParams.class)) { + QueryParams query = invocation.getInterfaceType().getAnnotation(QueryParams.class); addQuery(queryMap, query, tokenValues); } - if (method.isAnnotationPresent(QueryParams.class)) { - QueryParams query = method.getAnnotation(QueryParams.class); + if (invocation.getInvokable().isAnnotationPresent(QueryParams.class)) { + QueryParams query = invocation.getInvokable().getAnnotation(QueryParams.class); addQuery(queryMap, query, tokenValues); } - for (Entry query : getQueryParamKeyValues(method, args).entries()) { + for (Entry query : getQueryParamKeyValues(invocation).entries()) { queryMap.put(query.getKey(), replaceTokens(query.getValue().toString(), tokenValues)); } return queryMap; @@ -478,47 +464,53 @@ public abstract class RestAnnotationProcessor { } } - private List getFiltersIfAnnotated(Invokable method) { + private List getFiltersIfAnnotated(Invocation invocation) { List filters = newArrayList(); - if (declaring.isAnnotationPresent(RequestFilters.class)) { - for (Class clazz : declaring.getAnnotation(RequestFilters.class).value()) { + if (invocation.getInterfaceType().isAnnotationPresent(RequestFilters.class)) { + for (Class clazz : invocation.getInterfaceType() + .getAnnotation(RequestFilters.class).value()) { HttpRequestFilter instance = injector.getInstance(clazz); filters.add(instance); - logger.trace("adding filter %s from annotation on %s", instance, declaring.getName()); + logger.trace("adding filter %s from annotation on %s", instance, invocation.getInterfaceType().getName()); } } - if (method.isAnnotationPresent(RequestFilters.class)) { - if (method.isAnnotationPresent(OverrideRequestFilters.class)) + if (invocation.getInvokable().isAnnotationPresent(RequestFilters.class)) { + if (invocation.getInvokable().isAnnotationPresent(OverrideRequestFilters.class)) filters.clear(); - for (Class clazz : method.getAnnotation(RequestFilters.class).value()) { + for (Class clazz : invocation.getInvokable().getAnnotation(RequestFilters.class) + .value()) { HttpRequestFilter instance = injector.getInstance(clazz); filters.add(instance); - logger.trace("adding filter %s from annotation on %s", instance, method.getName()); + logger.trace("adding filter %s from annotation on %s", instance, invocation.getInvokable().getName()); } } return filters; } - + @VisibleForTesting - static URI getEndpointInParametersOrNull(Invokable method, List args, Injector injector) { - Collection endpointParams = parametersWithAnnotation(method, EndpointParam.class); + static URI getEndpointInParametersOrNull(Invocation invocation, Injector injector) { + Collection endpointParams = parametersWithAnnotation(invocation.getInvokable(), EndpointParam.class); if (endpointParams.isEmpty()) return null; - checkState(endpointParams.size() == 1, "method %s has too many EndpointParam annotations", method); + checkState(endpointParams.size() == 1, "invocation.getInvoked() %s has too many EndpointParam annotations", + invocation.getInvokable()); Parameter endpointParam = get(endpointParams, 0); Function parser = injector.getInstance(endpointParam.getAnnotation(EndpointParam.class).parser()); int position = endpointParam.hashCode();// guava issue 1243 try { - URI returnVal = parser.apply(args.get(position)); - checkArgument(returnVal != null, format("endpoint for [%s] not configured for %s", position, method)); + URI returnVal = parser.apply(invocation.getArgs().get(position)); + checkArgument(returnVal != null, + format("endpoint for [%s] not configured for %s", position, invocation.getInvokable())); return returnVal; } catch (NullPointerException e) { - throw new IllegalArgumentException(format("argument at index %d on method %s was null", position, method), e); + throw new IllegalArgumentException(format("argument at index %d on invocation.getInvoked() %s was null", + position, invocation.getInvokable()), e); } } - private static Collection parametersWithAnnotation(Invokable method, final Class annotationType) { - return filter(method.getParameters(), new Predicate() { + private static Collection parametersWithAnnotation(Invokable invokable, + final Class annotationType) { + return filter(invokable.getParameters(), new Predicate() { public boolean apply(Parameter in) { return in.isAnnotationPresent(annotationType); } @@ -528,16 +520,16 @@ public abstract class RestAnnotationProcessor { private static final TypeLiteral> uriSupplierLiteral = new TypeLiteral>() { }; - protected Optional getEndpointFor(Invokable method, List args) { - URI endpoint = getEndpointInParametersOrNull(method, args, injector); + protected Optional getEndpointFor(Invocation invocation) { + URI endpoint = getEndpointInParametersOrNull(invocation, injector); if (endpoint == null) { Endpoint annotation; - if (method.isAnnotationPresent(Endpoint.class)) { - annotation = method.getAnnotation(Endpoint.class); - } else if (method.getDeclaringClass().isAnnotationPresent(Endpoint.class)) { - annotation = method.getDeclaringClass().getAnnotation(Endpoint.class); + if (invocation.getInvokable().isAnnotationPresent(Endpoint.class)) { + annotation = invocation.getInvokable().getAnnotation(Endpoint.class); + } else if (invocation.getInterfaceType().isAnnotationPresent(Endpoint.class)) { + annotation = invocation.getInterfaceType().getAnnotation(Endpoint.class); } else { - logger.trace("no annotations on class or method: %s", method); + logger.trace("no annotations on class or invocation.getInvoked(): %s", invocation.getInvokable()); return Optional.absent(); } endpoint = injector.getInstance(Key.get(uriSupplierLiteral, annotation.value())).get(); @@ -557,9 +549,9 @@ public abstract class RestAnnotationProcessor { return withHost.resolve(original); } - private org.jclouds.rest.MapBinder getMapPayloadBinderOrNull(Invokable method, List args) { - if (args != null) { - for (Object arg : args) { + private org.jclouds.rest.MapBinder getMapPayloadBinderOrNull(Invocation invocation) { + if (invocation.getArgs() != null) { + for (Object arg : invocation.getArgs()) { if (arg instanceof Object[]) { Object[] postBinders = (Object[]) arg; if (postBinders.length == 0) { @@ -571,8 +563,9 @@ public abstract class RestAnnotationProcessor { } } else { if (postBinders[0] instanceof org.jclouds.rest.MapBinder) { - throw new IllegalArgumentException("we currently do not support multiple varargs postBinders in: " - + method.getName()); + throw new IllegalArgumentException( + "we currently do not support multiple varinvocation.getArgs() postBinders in: " + + invocation.getInvokable().getName()); } } } else if (arg instanceof org.jclouds.rest.MapBinder) { @@ -582,25 +575,28 @@ public abstract class RestAnnotationProcessor { } } } - if (method.isAnnotationPresent(MapBinder.class)) { - return injector.getInstance(method.getAnnotation(MapBinder.class).value()); - } else if (method.isAnnotationPresent(org.jclouds.rest.annotations.Payload.class)) { + if (invocation.getInvokable().isAnnotationPresent(MapBinder.class)) { + return injector.getInstance(invocation.getInvokable().getAnnotation(MapBinder.class).value()); + } else if (invocation.getInvokable().isAnnotationPresent(org.jclouds.rest.annotations.Payload.class)) { return injector.getInstance(BindMapToStringPayload.class); - } else if (method.isAnnotationPresent(WrapWith.class)) { + } else if (invocation.getInvokable().isAnnotationPresent(WrapWith.class)) { return injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class).create( - method.getAnnotation(WrapWith.class).value()); + invocation.getInvokable().getAnnotation(WrapWith.class).value()); } return null; } - private boolean shouldAddHostHeader(Invokable method) { - return (declaring.isAnnotationPresent(VirtualHost.class) || method.isAnnotationPresent(VirtualHost.class)); + private boolean shouldAddHostHeader(Invocation invocation) { + return (invocation.getInterfaceType().isAnnotationPresent(VirtualHost.class) || invocation.getInvokable() + .isAnnotationPresent(VirtualHost.class)); } - - private GeneratedHttpRequest decorateRequest(GeneratedHttpRequest request) throws NegativeArraySizeException { - Set binderOrWrapWith = ImmutableSet.copyOf(concat(parametersWithAnnotation(request.getInvoker(), BinderParam.class), - parametersWithAnnotation(request.getInvoker(), WrapWith.class))); + private GeneratedHttpRequest decorateRequest(GeneratedHttpRequest request) throws NegativeArraySizeException { + Invocation invocation = request.getInvocation(); + List args = request.getInvocation().getArgs(); + Set binderOrWrapWith = ImmutableSet.copyOf(concat( + parametersWithAnnotation(invocation.getInvokable(), BinderParam.class), + parametersWithAnnotation(invocation.getInvokable(), WrapWith.class))); OUTER: for (Parameter entry : binderOrWrapWith) { int position = entry.hashCode(); boolean shouldBreak = false; @@ -610,18 +606,19 @@ public abstract class RestAnnotationProcessor { else binder = injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class).create( entry.getAnnotation(WrapWith.class).value()); - Object arg = request.getArgs().size() >= position + 1 ? request.getArgs().get(position) : null; - if (request.getArgs().size() >= position + 1 && arg != null) { + Object arg = args.size() >= position + 1 ? args.get(position) : null; + if (args.size() >= position + 1 && arg != null) { Class parameterType = entry.getType().getRawType(); Class argType = arg.getClass(); - if (!argType.isArray() && parameterType.isArray()) { // TODO && varargs guava issue 1244 - int arrayLength = request.getArgs().size() - request.getInvoker().getParameters().size() + 1; + if (!argType.isArray() && parameterType.isArray()) { // TODO && varinvocation.getArgs() guava issue 1244 + int arrayLength = args.size() - invocation.getInvokable().getParameters().size() + 1; if (arrayLength == 0) break OUTER; arg = (Object[]) Array.newInstance(arg.getClass(), arrayLength); - System.arraycopy(request.getArgs().toArray(), position, arg, 0, arrayLength); + System.arraycopy(args.toArray(), position, arg, 0, arrayLength); shouldBreak = true; - } else if (argType.isArray() && parameterType.isArray()) { // TODO && varargs guava issue 1244 + } else if (argType.isArray() && parameterType.isArray()) { // TODO && varinvocation.getArgs() guava issue + // 1244 } else { if (arg.getClass().isArray()) { Object[] payloadArray = (Object[]) arg; @@ -634,24 +631,24 @@ public abstract class RestAnnotationProcessor { if (shouldBreak) break OUTER; } else { - if (position + 1 == request.getInvoker().getParameters().size() && entry.getType().isArray()) - continue OUTER; // TODO should only skip on null when varargs: guava issue 1244 + if (position + 1 == invocation.getInvokable().getParameters().size() && entry.getType().isArray()) + continue OUTER; // TODO should only skip on null when varinvocation.getArgs(): guava issue 1244 if (entry.isAnnotationPresent(Nullable.class)) { continue OUTER; } - checkNotNull(arg, request.getInvoker().getName() + " parameter " + (position + 1)); + checkNotNull(arg, invocation.getInvokable().getName() + " parameter " + (position + 1)); } } return request; } - private static final LoadingCache, Set> methodToIndexesOfOptions = CacheBuilder.newBuilder().build( - new CacheLoader, Set>() { + private static final LoadingCache, Set> invokableToIndexesOfOptions = CacheBuilder + .newBuilder().build(new CacheLoader, Set>() { @Override - public Set load(Invokable method) { + public Set load(Invokable invokable) { Builder toReturn = ImmutableSet.builder(); - for (Parameter param : method.getParameters()) { + for (Parameter param : invokable.getParameters()) { Class type = param.getType().getRawType(); if (HttpRequestOptions.class.isAssignableFrom(type) || HttpRequestOptions[].class.isAssignableFrom(type)) @@ -660,21 +657,21 @@ public abstract class RestAnnotationProcessor { return toReturn.build(); } }); - - private Set findOptionsIn(Invokable method, List args) { - ImmutableSet.Builder result = ImmutableSet.builder(); - for (int index : methodToIndexesOfOptions.getUnchecked(method)) { - if (args.size() >= index + 1) {// accommodate varargs - if (args.get(index) instanceof Object[]) { - for (Object option : (Object[]) args.get(index)) { + + private Set findOptionsIn(Invocation invocation) { + ImmutableSet.Builder result = ImmutableSet.builder(); + for (int index : invokableToIndexesOfOptions.getUnchecked(invocation.getInvokable())) { + if (invocation.getArgs().size() >= index + 1) {// accommodate varinvocation.getArgs() + if (invocation.getArgs().get(index) instanceof Object[]) { + for (Object option : (Object[]) invocation.getArgs().get(index)) { if (option instanceof HttpRequestOptions) { result.add((HttpRequestOptions) option); } } } else { - for (; index < args.size(); index++) { - if (args.get(index) instanceof HttpRequestOptions) { - result.add((HttpRequestOptions) args.get(index)); + for (; index < invocation.getArgs().size(); index++) { + if (invocation.getArgs().get(index) instanceof HttpRequestOptions) { + result.add((HttpRequestOptions) invocation.getArgs().get(index)); } } } @@ -683,58 +680,57 @@ public abstract class RestAnnotationProcessor { return result.build(); } - private Multimap buildHeaders(Multimap tokenValues, Invokable method, - List args) { + private Multimap buildHeaders(Multimap tokenValues, Invocation invocation) { Multimap headers = LinkedHashMultimap.create(); - addHeaderIfAnnotationPresentOnMethod(headers, method, tokenValues); - for (Parameter headerParam : parametersWithAnnotation(method, HeaderParam.class)) { + addHeaderIfAnnotationPresentOnMethod(headers, invocation, tokenValues); + for (Parameter headerParam : parametersWithAnnotation(invocation.getInvokable(), HeaderParam.class)) { Annotation key = headerParam.getAnnotation(HeaderParam.class); - String value = args.get(headerParam.hashCode()).toString(); // TODO position guava issue 1243 + String value = invocation.getArgs().get(headerParam.hashCode()).toString(); // TODO position guava issue 1243 value = replaceTokens(value, tokenValues); headers.put(((HeaderParam) key).value(), value); } - addProducesIfPresentOnTypeOrMethod(headers, method); - addConsumesIfPresentOnTypeOrMethod(headers, method); + addProducesIfPresentOnTypeOrMethod(headers, invocation); + addConsumesIfPresentOnTypeOrMethod(headers, invocation); return headers; } - private void addConsumesIfPresentOnTypeOrMethod(Multimap headers, Invokable method) { - Set accept = getAcceptHeaders(method); + private static void addConsumesIfPresentOnTypeOrMethod(Multimap headers, Invocation invocation) { + Set accept = getAcceptHeaders(invocation); if (!accept.isEmpty()) headers.replaceValues(ACCEPT, accept); } - + // TODO: refactor this out - static Set getAcceptHeaders(Invokable method) { - Optional accept = Optional.fromNullable(method.getAnnotation(Consumes.class)).or( - Optional.fromNullable(method.getDeclaringClass().getAnnotation(Consumes.class))); + static Set getAcceptHeaders(Invocation invocation) { + Optional accept = Optional.fromNullable(invocation.getInvokable().getAnnotation(Consumes.class)).or( + Optional.fromNullable(invocation.getInterfaceType().getAnnotation(Consumes.class))); return (accept.isPresent()) ? ImmutableSet.copyOf(accept.get().value()) : ImmutableSet. of(); } - private void addProducesIfPresentOnTypeOrMethod(Multimap headers, Invokable method) { - if (declaring.isAnnotationPresent(Produces.class)) { - Produces header = declaring.getAnnotation(Produces.class); + private static void addProducesIfPresentOnTypeOrMethod(Multimap headers, Invocation invocation) { + if (invocation.getInterfaceType().isAnnotationPresent(Produces.class)) { + Produces header = invocation.getInterfaceType().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } - if (method.isAnnotationPresent(Produces.class)) { - Produces header = method.getAnnotation(Produces.class); + if (invocation.getInvokable().isAnnotationPresent(Produces.class)) { + Produces header = invocation.getInvokable().getAnnotation(Produces.class); headers.replaceValues(CONTENT_TYPE, asList(header.value())); } } - private void addHeaderIfAnnotationPresentOnMethod(Multimap headers, Invokable method, + private static void addHeaderIfAnnotationPresentOnMethod(Multimap headers, Invocation invocation, Multimap tokenValues) { - if (declaring.isAnnotationPresent(Headers.class)) { - Headers header = declaring.getAnnotation(Headers.class); + if (invocation.getInterfaceType().isAnnotationPresent(Headers.class)) { + Headers header = invocation.getInterfaceType().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } - if (method.isAnnotationPresent(Headers.class)) { - Headers header = method.getAnnotation(Headers.class); + if (invocation.getInvokable().isAnnotationPresent(Headers.class)) { + Headers header = invocation.getInvokable().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } } - private void addHeader(Multimap headers, Headers header, Multimap tokenValues) { + private static void addHeader(Multimap headers, Headers header, Multimap tokenValues) { for (int i = 0; i < header.keys().length; i++) { String value = header.values()[i]; value = replaceTokens(value, tokenValues); @@ -742,16 +738,16 @@ public abstract class RestAnnotationProcessor { } } - private List getParts(Invokable method, List args, Multimap tokenValues) { + private static List getParts(Invocation invocation, Multimap tokenValues) { ImmutableList.Builder parts = ImmutableList. builder(); - for (Parameter param : parametersWithAnnotation(method, PartParam.class)) { + for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PartParam.class)) { PartParam partParam = param.getAnnotation(PartParam.class); PartOptions options = new PartOptions(); if (!PartParam.NO_CONTENT_TYPE.equals(partParam.contentType())) options.contentType(partParam.contentType()); if (!PartParam.NO_FILENAME.equals(partParam.filename())) options.filename(replaceTokens(partParam.filename(), tokenValues)); - Object arg = args.get(param.hashCode()); // TODO position guava issue 1243 + Object arg = invocation.getArgs().get(param.hashCode()); // TODO position guava issue 1243 checkNotNull(arg, partParam.name()); Part part = Part.create(partParam.name(), newPayload(arg), options); parts.add(part); @@ -759,12 +755,12 @@ public abstract class RestAnnotationProcessor { return parts.build(); } - private Multimap getPathParamKeyValues(Invokable method, List args) { + private Multimap getPathParamKeyValues(Invocation invocation) { Multimap pathParamValues = LinkedHashMultimap.create(); - for (Parameter param : parametersWithAnnotation(method, PathParam.class)) { + for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PathParam.class)) { PathParam pathParam = param.getAnnotation(PathParam.class); String paramKey = pathParam.value(); - Optional paramValue = getParamValue(method, args, param.getAnnotation(ParamParser.class), param.hashCode(), + Optional paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); // TODO position guava issue 1243 if (paramValue.isPresent()) pathParamValues.put(paramKey, paramValue.get().toString()); @@ -772,29 +768,29 @@ public abstract class RestAnnotationProcessor { return pathParamValues; } - private Optional getParamValue(Invokable method, List args, @Nullable ParamParser extractor, - int argIndex, String paramKey) { - Object arg = args.get(argIndex); - if (extractor != null && checkPresentOrNullable(method, paramKey, argIndex, arg)) { + private Optional getParamValue(Invocation invocation, @Nullable ParamParser extractor, int argIndex, + String paramKey) { + Object arg = invocation.getArgs().get(argIndex); + if (extractor != null && checkPresentOrNullable(invocation, paramKey, argIndex, arg)) { arg = injector.getInstance(extractor.value()).apply(arg); // ParamParsers can deal with nullable parameters } - checkPresentOrNullable(method, paramKey, argIndex, arg); + checkPresentOrNullable(invocation, paramKey, argIndex, arg); return Optional.fromNullable(arg); } - private static boolean checkPresentOrNullable(Invokable method, String paramKey, int argIndex, Object arg) { - if (arg == null && !method.getParameters().get(argIndex).isAnnotationPresent(Nullable.class)) - throw new NullPointerException(format("param{%s} for method %s.%s", paramKey, method - .getDeclaringClass().getSimpleName(), method.getName())); + private static boolean checkPresentOrNullable(Invocation invocation, String paramKey, int argIndex, Object arg) { + if (arg == null && !invocation.getInvokable().getParameters().get(argIndex).isAnnotationPresent(Nullable.class)) + throw new NullPointerException(format("param{%s} for invocation %s.%s", paramKey, invocation + .getInterfaceType().getSimpleName(), invocation.getInvokable().getName())); return true; } - private Multimap getFormParamKeyValues(Invokable method, List args) { + private Multimap getFormParamKeyValues(Invocation invocation) { Multimap formParamValues = LinkedHashMultimap.create(); - for (Parameter param : parametersWithAnnotation(method, FormParam.class)) { + for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), FormParam.class)) { FormParam formParam = param.getAnnotation(FormParam.class); String paramKey = formParam.value(); - Optional paramValue = getParamValue(method, args, param.getAnnotation(ParamParser.class), param.hashCode(), + Optional paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); // TODO position guava issue 1243 if (paramValue.isPresent()) formParamValues.put(paramKey, paramValue.get().toString()); @@ -802,15 +798,15 @@ public abstract class RestAnnotationProcessor { return formParamValues; } - private Multimap getQueryParamKeyValues(Invokable method, List args) { + private Multimap getQueryParamKeyValues(Invocation invocation) { Multimap queryParamValues = LinkedHashMultimap.create(); - for (Parameter param : parametersWithAnnotation(method, QueryParam.class)) { + for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), QueryParam.class)) { QueryParam queryParam = param.getAnnotation(QueryParam.class); String paramKey = queryParam.value(); - Optional paramValue = getParamValue(method, args, param.getAnnotation(ParamParser.class), param.hashCode(), + Optional paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); // TODO position guava issue 1243 if (paramValue.isPresent()) - if (paramValue.get() instanceof Iterable) { + if (paramValue.get() instanceof Iterable) { @SuppressWarnings("unchecked") Iterable iterableStrings = transform(Iterable.class.cast(paramValue.get()), toStringFunction()); queryParamValues.putAll(paramKey, iterableStrings); @@ -821,12 +817,12 @@ public abstract class RestAnnotationProcessor { return queryParamValues; } - private Map buildPayloadParams(Invokable method, List args) { + private Map buildPayloadParams(Invocation invocation) { Map payloadParamValues = Maps.newLinkedHashMap(); - for (Parameter param : parametersWithAnnotation(method, PayloadParam.class)) { + for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PayloadParam.class)) { PayloadParam payloadParam = param.getAnnotation(PayloadParam.class); String paramKey = payloadParam.value(); - Optional paramValue = getParamValue(method, args, param.getAnnotation(ParamParser.class), param.hashCode(), + Optional paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class), param.hashCode(), paramKey); // TODO position guava issue 1243 if (paramValue.isPresent()) payloadParamValues.put(paramKey, paramValue.get()); diff --git a/core/src/main/java/org/jclouds/util/Optionals2.java b/core/src/main/java/org/jclouds/util/Optionals2.java index 3488aa9923..c572194f37 100644 --- a/core/src/main/java/org/jclouds/util/Optionals2.java +++ b/core/src/main/java/org/jclouds/util/Optionals2.java @@ -31,23 +31,17 @@ import com.google.common.reflect.TypeToken; * @author Adrian Cole */ public class Optionals2 { - public static Class returnTypeOrTypeOfOptional(Invokable method) { - TypeToken type = method.getReturnType(); - return returnTypeOrTypeOfOptional(type.getRawType(), type.getType()); - } - - private static Class returnTypeOrTypeOfOptional(Class syncClass, Type genericType) { - if (syncClass.isAssignableFrom(Optional.class)) { - ParameterizedType futureType = ParameterizedType.class.cast(genericType); + public static Class unwrapIfOptional(TypeToken type) { + if (type.getRawType().isAssignableFrom(Optional.class)) { + ParameterizedType futureType = ParameterizedType.class.cast(type.getType()); // TODO: error checking in case this is a type, not a class. Type t = futureType.getActualTypeArguments()[0]; if (t instanceof WildcardType) { t = ((WildcardType) t).getUpperBounds()[0]; } - syncClass = Class.class.cast(t); - } else { + return Class.class.cast(t); } - return syncClass; + return type.getRawType(); } public static boolean isReturnTypeOptional(Invokable method) { diff --git a/core/src/main/java/org/jclouds/util/Throwables2.java b/core/src/main/java/org/jclouds/util/Throwables2.java index aa78fdcf93..35023a3d90 100644 --- a/core/src/main/java/org/jclouds/util/Throwables2.java +++ b/core/src/main/java/org/jclouds/util/Throwables2.java @@ -33,7 +33,6 @@ import org.jclouds.rest.ResourceNotFoundException; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Predicate; -import com.google.common.base.Throwables; import com.google.common.reflect.TypeToken; import com.google.inject.CreationException; import com.google.inject.ProvisionException; @@ -119,32 +118,6 @@ public class Throwables2 { return null; } - // Note this needs to be kept up-to-date with all top-level exceptions jclouds works against - @SuppressWarnings( { "unchecked", "rawtypes" }) - public static Exception returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(Iterable> throwables, - Exception exception) throws Exception { - for (TypeToken type : throwables) { - Throwable throwable = getFirstThrowableOfType(exception, (Class) type.getRawType()); - if (throwable != null) { - return (Exception) throwable; - } - } - for (Class propagatableExceptionType : new Class[] { IllegalStateException.class, - AssertionError.class, UnsupportedOperationException.class, IllegalArgumentException.class, - AuthorizationException.class, ResourceNotFoundException.class, InsufficientResourcesException.class, - HttpResponseException.class }) { - Throwable throwable = getFirstThrowableOfType(exception, propagatableExceptionType); - if (throwable != null) { - if (throwable instanceof AssertionError) - throw (AssertionError) throwable; - else - throw (Exception) throwable; - } - } - Throwables.propagateIfPossible(exception.getCause(), Exception.class); - throw exception; - } - public static T propagateAuthorizationOrOriginalException(Exception e) { AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class); if (aex != null) @@ -154,4 +127,24 @@ public class Throwables2 { return null; } + // Note this needs to be kept up-to-date with all top-level exceptions jclouds works against + @SuppressWarnings("unchecked") + public static void propagateIfPossible(Throwable exception, Iterable> throwables) + throws Throwable { + for (TypeToken type : throwables) { + Throwable throwable = Throwables2.getFirstThrowableOfType(exception, (Class) type.getRawType()); + if (throwable != null) { + throw throwable; + } + } + for (Class propagatableExceptionType : new Class[] { IllegalStateException.class, + AssertionError.class, UnsupportedOperationException.class, IllegalArgumentException.class, + AuthorizationException.class, ResourceNotFoundException.class, InsufficientResourcesException.class, + HttpResponseException.class }) { + Throwable throwable = Throwables2.getFirstThrowableOfType(exception, propagatableExceptionType); + if (throwable != null) { + throw throwable; + } + } + } } diff --git a/core/src/test/java/org/jclouds/concurrent/internal/SyncProxyTest.java b/core/src/test/java/org/jclouds/concurrent/internal/SyncProxyTest.java index 9b6f56b62c..4cccc58f06 100644 --- a/core/src/test/java/org/jclouds/concurrent/internal/SyncProxyTest.java +++ b/core/src/test/java/org/jclouds/concurrent/internal/SyncProxyTest.java @@ -18,7 +18,6 @@ */ package org.jclouds.concurrent.internal; -import static com.google.common.reflect.Reflection.newProxy; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; @@ -29,7 +28,8 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.jclouds.internal.ClassInvokerArgs; +import org.jclouds.internal.ForwardInvocationToInterface; +import org.jclouds.reflect.FunctionalReflection; import org.jclouds.rest.functions.AlwaysPresentImplicitOptionalConverter; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -73,14 +73,12 @@ public class SyncProxyTest { Sync withOverride = syncProxyForTimeouts(ImmutableMap.of("default", 250L)); assertEquals(withOverride.get(), "foo"); verify(future); - } public void testWithClassPropTimeout() throws Exception { Sync withOverride = syncProxyForTimeouts(ImmutableMap.of("default", 50L, "Sync", 250L)); assertEquals(withOverride.get(), "foo"); verify(future); - } public void testWithMethodPropTimeout() throws Exception { @@ -99,16 +97,13 @@ public class SyncProxyTest { assertEquals(noOverrides.get(), "foo"); verify(future); - } private Sync syncProxyForTimeouts(ImmutableMap timeouts) throws NoSuchMethodException { - LoadingCache cache = CacheBuilder.newBuilder().build( + LoadingCache cache = CacheBuilder.newBuilder().build( CacheLoader.from(Functions. constant(null))); - return newProxy( - Sync.class, - new SyncProxy(new AlwaysPresentImplicitOptionalConverter(), cache, ImmutableMap., Class> of( - Sync.class, Async.class), timeouts, Sync.class, new Async())); + return FunctionalReflection.newProxy(Sync.class, new SyncProxy(new AlwaysPresentImplicitOptionalConverter(), + cache, ImmutableMap., Class> of(Sync.class, Async.class), timeouts, Sync.class, new Async())); } } diff --git a/core/src/test/java/org/jclouds/http/functions/BaseHandlerTest.java b/core/src/test/java/org/jclouds/http/functions/BaseHandlerTest.java index 7503fee9ae..612e68b4f5 100644 --- a/core/src/test/java/org/jclouds/http/functions/BaseHandlerTest.java +++ b/core/src/test/java/org/jclouds/http/functions/BaseHandlerTest.java @@ -18,11 +18,18 @@ */ package org.jclouds.http.functions; +import static com.google.common.base.Throwables.propagate; + +import java.util.List; + import org.jclouds.http.functions.config.SaxParserModule; +import org.jclouds.reflect.Invocation; +import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; +import com.google.common.reflect.Invokable; import com.google.inject.Guice; import com.google.inject.Injector; @@ -30,11 +37,12 @@ import com.google.inject.Injector; * * @author Adrian Cole */ -@Test(groups = "unit") public class BaseHandlerTest { protected Injector injector = null; protected ParseSax.Factory factory; + protected GeneratedHttpRequest request; + private Invocation toString; @BeforeTest protected void setUpInjector() { @@ -43,10 +51,27 @@ public class BaseHandlerTest { assert factory != null; } + @BeforeTest + protected void setUpRequest() { + try { + toString = Invocation.create(Invokable.from(String.class.getDeclaredMethod("toString")), ImmutableList.of()); + } catch (SecurityException e) { + throw propagate(e); + } catch (NoSuchMethodException e) { + throw propagate(e); + } + request = GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key").invocation(toString) + .build(); + } + @AfterTest protected void tearDownInjector() { factory = null; injector = null; } + protected GeneratedHttpRequest requestForArgs(List args) { + return GeneratedHttpRequest.builder().method("POST").endpoint("http://localhost/key") + .invocation(Invocation.create(toString.getInvokable(), args)).build(); + } } diff --git a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java index 820d078cb7..e7cd0b4d59 100644 --- a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java +++ b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java @@ -167,7 +167,7 @@ public class BackoffLimitedRetryHandlerTest { } private final RestAnnotationProcessor processor = BaseJettyTest.newBuilder(8100, new Properties()).buildInjector() - .getInstance(RestAnnotationProcessor.Factory.class).declaring(IntegrationTestAsyncClient.class); + .getInstance(RestAnnotationProcessor.class); private HttpCommand createCommand() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(IntegrationTestAsyncClient.class.getMethod("download", String.class)); diff --git a/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java b/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java index eaaaa76bd4..fd5d869a09 100644 --- a/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java +++ b/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java @@ -78,11 +78,12 @@ public class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpComman } public static Invokable getInvokerOfRequest(HttpCommand commandInvoked) { - return GeneratedHttpRequest.class.cast(commandInvoked.getCurrentRequest()).getInvoker(); + return GeneratedHttpRequest.class.cast(commandInvoked.getCurrentRequest()).getInvocation().getInvokable(); } public static List getArgsForRequestAtIndex(final Collection commandsInvoked, int index) { - return GeneratedHttpRequest.class.cast(Iterables.get(commandsInvoked, index).getCurrentRequest()).getArgs(); + return GeneratedHttpRequest.class.cast(Iterables.get(commandsInvoked, index).getCurrentRequest()).getInvocation() + .getArgs(); } @Inject diff --git a/core/src/test/java/org/jclouds/json/BaseParserTest.java b/core/src/test/java/org/jclouds/json/BaseParserTest.java index 9c4b71a137..e6552a397f 100644 --- a/core/src/test/java/org/jclouds/json/BaseParserTest.java +++ b/core/src/test/java/org/jclouds/json/BaseParserTest.java @@ -31,11 +31,13 @@ import org.jclouds.http.HttpResponse; import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.json.config.GsonModule; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.AsyncRestClientProxy; import org.testng.annotations.Test; import com.google.common.base.Function; import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; import com.google.common.reflect.Invokable; import com.google.inject.Guice; import com.google.inject.Injector; @@ -57,7 +59,7 @@ public abstract class BaseParserTest { protected Function parser(Injector i) { try { return (Function) AsyncRestClientProxy.getTransformerForMethod( - Invokable.from(getClass().getMethod("expected")), i); + Invocation.create(Invokable.from(getClass().getMethod("expected")), ImmutableList.of()), i); } catch (Exception e) { throw Throwables.propagate(e); } diff --git a/core/src/test/java/org/jclouds/reflect/AbstractInvocationHandlerTest.java b/core/src/test/java/org/jclouds/reflect/AbstractInvocationHandlerTest.java deleted file mode 100644 index a94488d0b5..0000000000 --- a/core/src/test/java/org/jclouds/reflect/AbstractInvocationHandlerTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.reflect; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -import java.io.IOException; -import java.util.List; - -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.reflect.Invokable; -import com.google.common.reflect.Reflection; - -/** - * - * @author Adrian Cole - */ -@Test -public class AbstractInvocationHandlerTest { - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void testNullArgsAreAllowedAndUnmodifiable() throws IOException { - Reflection.newProxy(Appendable.class, new AbstractInvocationHandler() { - protected Object handleInvocation(Object proxy, Invokable method, List args) throws Throwable { - assertNotNull(args); - assertNull(args.get(0)); - args.add("foo"); - throw new AssertionError("shouldn't be able to mutate the list!"); - } - }).append(null); - } - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void testImmutableListWhenArgsAreNotNull() throws IOException { - Reflection.newProxy(Appendable.class, new AbstractInvocationHandler() { - protected Object handleInvocation(Object proxy, Invokable method, List args) throws Throwable { - assertNotNull(args); - assertTrue(args instanceof ImmutableList); - assertEquals(args.get(0), "foo"); - args.add("bar"); - throw new AssertionError("shouldn't be able to mutate the list!"); - } - }).append("foo"); - } -} diff --git a/core/src/test/java/org/jclouds/rest/InputParamValidatorTest.java b/core/src/test/java/org/jclouds/rest/InputParamValidatorTest.java index d7c2afcb4b..bbdfc7c4f1 100644 --- a/core/src/test/java/org/jclouds/rest/InputParamValidatorTest.java +++ b/core/src/test/java/org/jclouds/rest/InputParamValidatorTest.java @@ -26,6 +26,7 @@ import org.jclouds.http.IntegrationTestAsyncClient; import org.jclouds.http.IntegrationTestClient; import org.jclouds.predicates.validators.AllLowerCaseValidator; import org.jclouds.providers.AnonymousProviderMetadata; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.annotations.ParamValidators; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.TestException; @@ -62,7 +63,6 @@ public class InputParamValidatorTest { "allParamsValidated", String.class, String.class)); Invokable oneParamValidatedMethod = Invokable.from(InputParamValidatorForm.class.getMethod( "oneParamValidated", String.class, String.class)); - RestAnnotationProcessor restAnnotationProcessor = factory(InputParamValidatorForm.class); restAnnotationProcessor.createRequest(allParamsValidatedMethod, ImmutableList. of("blah", "blah")); restAnnotationProcessor.createRequest(oneParamValidatedMethod, ImmutableList. of("blah", "blah")); @@ -98,15 +98,13 @@ public class InputParamValidatorTest { @Test(expectedExceptions = ClassCastException.class) public void testWrongPredicateTypeLiteral() throws Exception { - Invokable method = Invokable.from(WrongValidator.class.getMethod("method", Integer.class)); - new InputParamValidator(injector).validateMethodParametersOrThrow(method, ImmutableList. of(55)); - } - - private RestAnnotationProcessor factory(Class clazz) { - return injector.getInstance(RestAnnotationProcessor.Factory.class).declaring(clazz); + Invocation invocation = Invocation.create(Invokable.from(WrongValidator.class.getMethod("method", Integer.class)), + ImmutableList. of(55)); + new InputParamValidator(injector).validateMethodParametersOrThrow(invocation); } Injector injector; + RestAnnotationProcessor restAnnotationProcessor; @BeforeClass void setupFactory() { @@ -114,7 +112,7 @@ public class InputParamValidatorTest { .newBuilder( AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost:9999")).buildInjector(); - + restAnnotationProcessor = injector.getInstance(RestAnnotationProcessor.class); } } diff --git a/core/src/test/java/org/jclouds/rest/binders/BindMapToStringPayloadTest.java b/core/src/test/java/org/jclouds/rest/binders/BindMapToStringPayloadTest.java index 8e48bc5c40..87c7ca0873 100644 --- a/core/src/test/java/org/jclouds/rest/binders/BindMapToStringPayloadTest.java +++ b/core/src/test/java/org/jclouds/rest/binders/BindMapToStringPayloadTest.java @@ -25,6 +25,7 @@ import java.io.File; import javax.ws.rs.PathParam; import org.jclouds.http.HttpRequest; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.annotations.Payload; import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.internal.GeneratedHttpRequest; @@ -55,7 +56,7 @@ public class BindMapToStringPayloadTest { public void testCorrect() throws SecurityException, NoSuchMethodException { Invokable testPayload = Invokable.from(TestPayload.class.getMethod("testPayload", String.class)); GeneratedHttpRequest request = GeneratedHttpRequest.builder() - .declaring(TestPayload.class).invoker(testPayload).args(ImmutableList. of("robot")) + .invocation(Invocation.create(testPayload, ImmutableList. of("robot"))) .method("POST").endpoint("http://localhost").build(); GeneratedHttpRequest newRequest = binder() @@ -69,7 +70,7 @@ public class BindMapToStringPayloadTest { public void testDecodes() throws SecurityException, NoSuchMethodException { Invokable testPayload = Invokable.from(TestPayload.class.getMethod("changeAdminPass", String.class)); GeneratedHttpRequest request = GeneratedHttpRequest.builder() - .declaring(TestPayload.class).invoker(testPayload).args(ImmutableList. of("foo")) + .invocation(Invocation.create(testPayload, ImmutableList. of("foo"))) .method("POST").endpoint("http://localhost").build(); GeneratedHttpRequest newRequest = binder() @@ -83,7 +84,7 @@ public class BindMapToStringPayloadTest { public void testMustHavePayloadAnnotation() throws SecurityException, NoSuchMethodException { Invokable noPayload = Invokable.from(TestPayload.class.getMethod("noPayload", String.class)); GeneratedHttpRequest request = GeneratedHttpRequest.builder() - .declaring(TestPayload.class).invoker(noPayload).args(ImmutableList. of("robot")) + .invocation(Invocation.create(noPayload, ImmutableList. of("robot"))) .method("POST").endpoint("http://localhost").build(); binder().bindToRequest(request, ImmutableMap.of("fooble", "robot")); } diff --git a/core/src/test/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest.java b/core/src/test/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest.java index 6e39c6c157..6acd1bc26e 100644 --- a/core/src/test/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest.java +++ b/core/src/test/java/org/jclouds/rest/functions/PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest.java @@ -25,7 +25,8 @@ import static org.testng.Assert.assertTrue; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; +import org.jclouds.reflect.Invocation; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.annotations.SinceApiVersion; import org.jclouds.rest.functions.PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersion.Loader; @@ -122,7 +123,7 @@ public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest } public void testCacheIsFasterWhenNoAnnotationPresent() { - ClassInvokerArgsAndReturnVal keyPairApi = getKeyPairApi(); + InvocationSuccess keyPairApi = getKeyPairApi(); ImplicitOptionalConverter fn = forApiVersion("2011-07-15"); Stopwatch watch = new Stopwatch().start(); fn.apply(keyPairApi); @@ -136,7 +137,7 @@ public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest } public void testCacheIsFasterWhenAnnotationPresent() { - ClassInvokerArgsAndReturnVal floatingIpApi = getKeyPairApi(); + InvocationSuccess floatingIpApi = getKeyPairApi(); ImplicitOptionalConverter fn = forApiVersion("2011-07-15"); Stopwatch watch = new Stopwatch().start(); fn.apply(floatingIpApi); @@ -150,23 +151,23 @@ public class PresentWhenApiVersionLexicographicallyAtOrAfterSinceApiVersionTest } - ClassInvokerArgsAndReturnVal getFloatingIPApi() { + InvocationSuccess getFloatingIPApi() { return getApi("Tag", TagAsyncApi.class); } - ClassInvokerArgsAndReturnVal getKeyPairApi() { + InvocationSuccess getKeyPairApi() { return getApi("KeyPair", KeyPairAsyncApi.class); } - ClassInvokerArgsAndReturnVal getVpcApi() { + InvocationSuccess getVpcApi() { return getApi("Vpc", VpcAsyncApi.class); } - ClassInvokerArgsAndReturnVal getApi(String name, Class type) { + InvocationSuccess getApi(String name, Class target) { try { - return ClassInvokerArgsAndReturnVal.builder().clazz(type) - .invoker(Invokable.from(EC2AsyncApi.class.getDeclaredMethod("get" + name + "ApiForRegion", String.class))) - .args(ImmutableList. of("region")).returnVal("present").build(); + return InvocationSuccess.create(Invocation.create( + Invokable.from(EC2AsyncApi.class.getDeclaredMethod("get" + name + "ApiForRegion", String.class)), + ImmutableList. of("region")), "present"); } catch (Exception e) { throw propagate(e); } diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseAsyncApiTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseAsyncApiTest.java index 53175ac088..2b883e0fcc 100644 --- a/core/src/test/java/org/jclouds/rest/internal/BaseAsyncApiTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/BaseAsyncApiTest.java @@ -33,7 +33,6 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; -import com.google.common.reflect.TypeToken; import com.google.inject.Binder; import com.google.inject.Injector; import com.google.inject.Module; @@ -64,9 +63,7 @@ public abstract class BaseAsyncApiTest extends BaseRestApiTest { protected void setupFactory() throws IOException { injector = createInjector(); parserFactory = injector.getInstance(ParseSax.Factory.class); - processor = injector.getInstance(RestAnnotationProcessor.Factory.class).declaring(new TypeToken(getClass()) { - private static final long serialVersionUID = 1L; - }.getRawType()); + processor = injector.getInstance(RestAnnotationProcessor.class); } protected String identity = "identity"; diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java index e085b3179f..57c91b784d 100644 --- a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java @@ -40,11 +40,13 @@ import org.jclouds.http.config.ConfiguresHttpCommandExecutorService; import org.jclouds.http.functions.ParseSax; import org.jclouds.io.MutableContentMetadata; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.XMLResponseParser; import org.jclouds.util.Strings2; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; import com.google.common.collect.SortedSetMultimap; import com.google.common.collect.TreeMultimap; @@ -176,11 +178,8 @@ public abstract class BaseRestApiTest { } protected void assertResponseParserClassEquals(Invokable method, HttpRequest request, @Nullable Class parserClass) { - assertEquals(AsyncRestClientProxy.createResponseParser(parserFactory, injector, method, request).getClass(), parserClass); + assertEquals( + AsyncRestClientProxy.createResponseParser(parserFactory, injector, + Invocation.create(method, ImmutableList.of()), request).getClass(), parserClass); } - - protected RestAnnotationProcessor factory(Class clazz) { - return injector.getInstance(RestAnnotationProcessor.Factory.class).declaring(clazz); - } - } 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 3b13893d14..436cfb38f6 100644 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -86,13 +86,14 @@ import org.jclouds.http.internal.PayloadEnclosingImpl; import org.jclouds.http.options.BaseHttpRequestOptions; import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.HttpRequestOptions; -import org.jclouds.internal.ClassInvokerArgsAndReturnVal; import org.jclouds.io.Payload; import org.jclouds.io.PayloadEnclosing; import org.jclouds.io.Payloads; import org.jclouds.javax.annotation.Nullable; import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.providers.AnonymousProviderMetadata; +import org.jclouds.reflect.Invocation; +import org.jclouds.reflect.InvocationSuccess; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.InvocationContext; @@ -389,7 +390,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { bind(ImplicitOptionalConverter.class).toInstance(new ImplicitOptionalConverter() { @Override - public Optional apply(ClassInvokerArgsAndReturnVal input) { + public Optional apply(InvocationSuccess input) { return Optional.absent(); } @@ -491,7 +492,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testQuery() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("foo")); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&x-ms-rubbish=bin"); @@ -500,7 +501,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testQuery2() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("foo2")); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=bar&fooble=baz"); @@ -509,74 +510,74 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testQuery3() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("foo3", String.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of("wonder")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("wonder")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=bar&fooble=baz&robbie=wonder"); assertEquals(request.getMethod(), "FOO"); } - @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{robbie\\} for method TestQuery.foo3") + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{robbie\\} for invocation TestQuery.foo3") public void testNiceNPEQueryParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestQuery.class.getMethod("foo3", String.class)); - factory(TestPath.class).createRequest(method, Lists. newArrayList((String) null)); + processor.createRequest(method, Lists. newArrayList((String) null)); } public void testNoNPEOnQueryParamWithNullable() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("foo3Nullable", String.class)); - HttpRequest request = factory(TestPath.class).createRequest(method, Lists. newArrayList((String) null)); + HttpRequest request = processor.createRequest(method, Lists. newArrayList((String) null)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), "foo=bar&fooble=baz"); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=bar&fooble=baz"); assertEquals(request.getMethod(), "FOO"); } public void testQueryParamIterableOneString() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("queryParamIterable", Iterable.class)); Set bars = ImmutableSortedSet. of("1"); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of(bars)); + HttpRequest request = processor.createRequest(method, ImmutableList. of(bars)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), "foo=1"); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=1"); assertEquals(request.getMethod(), "FOO"); } public void testQueryParamIterableString() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("queryParamIterable", Iterable.class)); Set bars = ImmutableSortedSet. of("1", "2", "3"); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of(bars)); + HttpRequest request = processor.createRequest(method, ImmutableList. of(bars)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3"); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=1&foo=2&foo=3"); assertEquals(request.getMethod(), "FOO"); } public void testQueryParamIterableInteger() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("queryParamIterable", Iterable.class)); Set bars = ImmutableSortedSet. of(1, 2, 3); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of(bars)); + HttpRequest request = processor.createRequest(method, ImmutableList. of(bars)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3"); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=1&foo=2&foo=3"); assertEquals(request.getMethod(), "FOO"); } public void testQueryParamIterableEmpty() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("queryParamIterable", Iterable.class)); Set bars = Collections.emptySet(); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of(bars)); + HttpRequest request = processor.createRequest(method, ImmutableList. of(bars)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), null); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17"); assertEquals(request.getMethod(), "FOO"); } public void testQueryParamIterableNull() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQuery.class.getMethod("queryParamIterable", Iterable.class)); - HttpRequest request = factory(TestPath.class).createRequest(method, Lists. newArrayList((String) null)); + HttpRequest request = processor.createRequest(method, Lists. newArrayList((String) null)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); - assertEquals(request.getEndpoint().getQuery(), null); + assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17"); assertEquals(request.getMethod(), "FOO"); } @@ -601,8 +602,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testHttpRequestOptionsNoPayloadParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("post")); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList.of()); - assertRequestLineEquals(request, "POST http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); + assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "", "application/octet-stream", false); } @@ -615,15 +616,15 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testHttpRequestOptionsPayloadParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("post", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(Payloads.newStringPayload("foo"))); - assertRequestLineEquals(request, "POST http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(Payloads.newStringPayload("foo"))); + assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "foo", "application/octet-stream", false); } public void testHttpRequestWithOnlyContentType() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("post", HttpRequestOptions.class)); - HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, ImmutableList. of(new TestHttpRequestOptions().payload("fooya"))); + HttpRequest request = processor.createRequest(method, ImmutableList. of(new TestHttpRequestOptions().payload("fooya"))); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "fooya", "application/unknown", false); @@ -631,7 +632,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testHeaderAndQueryVarargs() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("varargs", HttpRequestOptions[].class)); - HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, ImmutableList. of( + HttpRequest request = processor.createRequest(method, ImmutableList. of( new TestHttpRequestOptions().payload("fooya"), new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")), new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value")))); @@ -642,7 +643,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testHeaderAndQueryVarargsPlusReq() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("varargsWithReq", String.class, HttpRequestOptions[].class)); - HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, ImmutableList. of("required param", + HttpRequest request = processor.createRequest(method, ImmutableList. of("required param", new TestHttpRequestOptions().payload("fooya"), new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")), new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value")))); @@ -653,7 +654,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testDuplicateHeaderAndQueryVarargs() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPayloadParamVarargs.class.getMethod("varargs", HttpRequestOptions[].class)); - HttpRequest request = factory(TestPayloadParamVarargs.class).createRequest(method, ImmutableList. of( + HttpRequest request = processor.createRequest(method, ImmutableList. of( new TestHttpRequestOptions().queryParams(ImmutableMultimap.of("key", "value")), new TestHttpRequestOptions().payload("fooya"), new TestHttpRequestOptions().headerParams(ImmutableMultimap.of("X-header-1", "fooya")), @@ -673,7 +674,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCustomMethod() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestCustomMethod.class.getMethod("foo")); - HttpRequest request = factory(TestCustomMethod.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), ""); assertEquals(request.getMethod(), "FOO"); @@ -691,7 +692,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testOverriddenMethod() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestOverridden.class.getMethod("foo")); - HttpRequest request = factory(TestOverridden.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), ""); assertEquals(request.getMethod(), "POST"); @@ -711,7 +712,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testOverriddenEndpointMethod() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestOverriddenEndpoint.class.getMethod("foo")); - HttpRequest request = factory(TestOverriddenEndpoint.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPort(), 1111); assertEquals(request.getEndpoint().getPath(), ""); @@ -720,7 +721,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testOverriddenEndpointParameter() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestOverriddenEndpoint.class.getMethod("foo", URI.class)); - HttpRequest request = factory(TestOverriddenEndpoint.class).createRequest(method, + HttpRequest request = processor.createRequest(method, ImmutableList. of(URI.create("http://wowsa:8001"))); assertEquals(request.getEndpoint().getHost(), "wowsa"); assertEquals(request.getEndpoint().getPort(), 8001); @@ -763,7 +764,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostRequest() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("post", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -772,7 +773,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostRequestNullOk1() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("post", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -781,7 +782,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostRequestNullOk2() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("post", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, Lists. newArrayList((String) null)); + HttpRequest request = processor.createRequest(method, Lists. newArrayList((String) null)); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -791,7 +792,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostRequestNullNotOk1() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postNonnull", String.class)); try { - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); Assert.fail("call should have failed with illegal null parameter, not permitted " + request + " to be created"); } catch (NullPointerException e) { Assert.assertTrue(e.toString().indexOf("postNonnull parameter 1") >= 0, @@ -802,12 +803,12 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "postNonnull parameter 1") public void testCreatePostRequestNullNotOk2() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postNonnull", String.class)); - factory(TestPost.class).createRequest(method, Lists. newArrayList((String) null)); + processor.createRequest(method, Lists. newArrayList((String) null)); } public void testCreatePostJsonRequest() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postAsJson", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -816,7 +817,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postWithPath", String.class, MapBinder.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data", new org.jclouds.rest.MapBinder() { + HttpRequest request = processor.createRequest(method, ImmutableList. of("data", new org.jclouds.rest.MapBinder() { @Override public R bindToRequest(R request, Map postParams) { request.setPayload((String) postParams.get("fooble")); @@ -834,7 +835,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostWithMethodBinder() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postWithMethodBinder", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "POST http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -843,7 +844,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostWithMethodBinderAndDefaults() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("postWithMethodBinderAndDefaults", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "POST http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -852,7 +853,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePostWithPayload() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPost.class.getMethod("testPayload", String.class)); - HttpRequest request = factory(TestPost.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "POST http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -881,7 +882,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testMultipartWithStringPart() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestMultipartForm.class.getMethod("withStringPart", String.class)); - GeneratedHttpRequest httpRequest = factory(TestMultipartForm.class).createRequest(method,ImmutableList. of( + GeneratedHttpRequest httpRequest = processor.createRequest(method,ImmutableList. of( "foobledata")); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, ""); @@ -896,12 +897,12 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "fooble") public void testMultipartWithStringPartNullNotOkay() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestMultipartForm.class.getMethod("withStringPart", String.class)); - factory(TestMultipartForm.class).createRequest(method, Lists. newArrayList((String) null)); + processor.createRequest(method, Lists. newArrayList((String) null)); } public void testMultipartWithParamStringPart() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestMultipartForm.class.getMethod("withParamStringPart", String.class, String.class)); - GeneratedHttpRequest httpRequest = factory(TestMultipartForm.class).createRequest(method, + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList. of("name", "foobledata")); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, ""); @@ -917,10 +918,10 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); } - @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{name\\} for method TestMultipartForm.withParamStringPart") + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{name\\} for invocation TestMultipartForm.withParamStringPart") public void testMultipartWithParamStringPartNullNotOk() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestMultipartForm.class.getMethod("withParamStringPart", String.class, String.class)); - factory(TestMultipartForm.class).createRequest(method, Lists. newArrayList(null, "foobledata")); + processor.createRequest(method, Lists. newArrayList(null, "foobledata")); } public void testMultipartWithParamFilePart() throws SecurityException, NoSuchMethodException, IOException { @@ -929,7 +930,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Files.append("foobledata", file, UTF_8); file.deleteOnExit(); - GeneratedHttpRequest httpRequest = factory(TestMultipartForm.class).createRequest(method, + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList. of("name", file)); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, ""); @@ -947,7 +948,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestMultipartForm.class.getMethod("withParamByteArrayBinaryPart", String.class, byte[].class)); - GeneratedHttpRequest httpRequest = factory(TestMultipartForm.class).createRequest(method, + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList. of("name", "goo".getBytes())); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, ""); @@ -970,7 +971,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Files.write(new byte[] { 17, 26, 39, 40, 50 }, file); file.deleteOnExit(); - GeneratedHttpRequest httpRequest = factory(TestMultipartForm.class).createRequest(method, + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList. of("name", file)); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, ""); @@ -1100,7 +1101,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testAlternateHttpMethod() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("rowdy", String.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "ROWDY http://localhost:9999/strings/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -1109,7 +1110,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testAlternateHttpMethodSameArity() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("rowdy", int.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "ROWDY http://localhost:9999/ints/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -1118,7 +1119,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePutWithMethodBinder() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("putWithMethodBinder", String.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "PUT http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -1127,7 +1128,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePutWithMethodProduces() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("putWithMethodBinderProduces", String.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "PUT http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -1137,7 +1138,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testCreatePutWithMethodConsumes() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("putWithMethodBinderConsumes", String.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("data")); assertRequestLineEquals(request, "PUT http://localhost:9999/data HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Accept: application/json\n"); @@ -1147,7 +1148,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { // now test that it works! Function parser = (Function) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()).foo, "bar"); @@ -1156,13 +1157,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testGeneric1() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testGeneric")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, ParseJson.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), ImmutableMap.of("foo", "bar")); @@ -1172,13 +1173,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testGeneric2() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testGeneric2")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, ParseJson.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), ImmutableMap.of("foo", "bar")); @@ -1188,13 +1189,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testGeneric3() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testGeneric3")); - HttpRequest request = factory(TestPut.class).createRequest(method ,ImmutableList.of()); + HttpRequest request = processor.createRequest(method ,ImmutableList.of()); assertResponseParserClassEquals(method, request, ParseJson.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), ImmutableMap.of("foo", "bar")); @@ -1204,13 +1205,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testUnwrap1() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testUnwrap")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), "bar"); @@ -1219,13 +1220,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testUnwrapValueNamed() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testUnwrapValueNamed")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, ParseFirstJsonValueNamed.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), "bar"); @@ -1233,20 +1234,20 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testWrapWith() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testWrapWith", String.class)); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList. of("bar")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("bar")); assertPayloadEquals(request, "{\"foo\":\"bar\"}", "application/json", false); } @SuppressWarnings("unchecked") public void testUnwrap2() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testUnwrap2")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{ foo:\"bar\"}").build()), "bar"); @@ -1255,13 +1256,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testUnwrap3() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testUnwrap3")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}").build()), ImmutableSet.of("0.7.0", "0.7.1")); @@ -1270,13 +1271,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void testUnwrap4() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("testUnwrap4")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload("{\"runit\":[\"0.7.0\",\"0.7.1\"]}").build()), ImmutableSet.of("0.7.0", "0.7.1")); @@ -1285,13 +1286,13 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void selectLong() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("selectLong")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertResponseParserClassEquals(method, request, ParseFirstJsonValueNamed.class); // now test that it works! Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok") .payload("{ \"destroyvirtualmachineresponse\" : {\"jobid\":4} }").build()), Long.valueOf(4)); @@ -1300,10 +1301,10 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SuppressWarnings("unchecked") public void selectLongAddOne() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPut.class.getMethod("selectLongAddOne")); - HttpRequest request = factory(TestPut.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); Function> parser = (Function>) AsyncRestClientProxy - .createResponseParser(parserFactory, injector, method, request); + .createResponseParser(parserFactory, injector, Invocation.create(method, ImmutableList.of()), request); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok") .payload("{ \"destroyvirtualmachineresponse\" : {\"jobid\":4} }").build()), Long.valueOf(5)); @@ -1340,7 +1341,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testRequestFilter() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequestFilter.class.getMethod("get")); - HttpRequest request = factory(TestRequestFilter.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getFilters().size(), 2); assertEquals(request.getFilters().get(0).getClass(), TestRequestFilter1.class); assertEquals(request.getFilters().get(1).getClass(), TestRequestFilter2.class); @@ -1348,14 +1349,14 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testRequestFilterOverride() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequestFilter.class.getMethod("getOverride")); - HttpRequest request = factory(TestRequestFilter.class).createRequest(method, ImmutableList.of()); + HttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getFilters().size(), 1); assertEquals(request.getFilters().get(0).getClass(), TestRequestFilter2.class); } public void testRequestFilterOverrideOnRequest() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequestFilter.class.getMethod("getOverride", HttpRequest.class)); - HttpRequest request = factory(TestRequestFilter.class).createRequest( + HttpRequest request = processor.createRequest( method, ImmutableList. of( HttpRequest.builder().method("GET").endpoint("http://localhost") .addHeader("foo", "bar").build())); @@ -1374,7 +1375,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testSkipEncoding() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestEncoding.class.getMethod("twoPaths", String.class, String.class)); - HttpRequest request = factory(TestEncoding.class).createRequest(method, ImmutableList. of("1", "localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "localhost")); assertEquals(request.getEndpoint().getPath(), "/1/localhost"); assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getHeaders().size(), 0); @@ -1383,7 +1384,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testEncodingPath() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestEncoding.class.getMethod("twoPaths", String.class, String.class)); - HttpRequest request = factory(TestEncoding.class).createRequest(method, ImmutableList. of("/", "localhost" )); + HttpRequest request = processor.createRequest(method, ImmutableList. of("/", "localhost" )); assertEquals(request.getEndpoint().getPath(), "///localhost"); assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getHeaders().size(), 0); @@ -1403,7 +1404,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test(enabled = false) public void testConstantPathParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestConstantPathParam.class.getMethod("twoPaths", String.class, String.class)); - HttpRequest request = factory(TestConstantPathParam.class).createRequest(method, + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "localhost")); assertRequestLineEquals(request, "GET http://localhost:9999/v1/ralphie/1/localhost HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -1447,16 +1448,16 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { } } - @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{path\\} for method TestPath.onePath") + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{path\\} for invocation TestPath.onePath") public void testNiceNPEPathParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPath.class.getMethod("onePath", String.class)); - factory(TestPath.class).createRequest(method, Lists. newArrayList((String) null)); + processor.createRequest(method, Lists. newArrayList((String) null)); } @Test public void testPathParamExtractor() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPath.class.getMethod("onePathParamExtractor", String.class)); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of("localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("localhost")); assertRequestLineEquals(request, "GET http://localhost:9999/l HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, null, null, false); @@ -1465,7 +1466,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testQueryParamExtractor() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPath.class.getMethod("oneQueryParamExtractor", String.class)); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of("localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("localhost")); assertRequestLineEquals(request, "GET http://localhost:9999/?one=l HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, null, null, false); @@ -1474,16 +1475,16 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testFormParamExtractor() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPath.class.getMethod("oneFormParamExtractor", String.class)); - HttpRequest request = factory(TestPath.class).createRequest(method, ImmutableList. of("localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("localhost")); assertRequestLineEquals(request, "POST http://localhost:9999/ HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "one=l", "application/x-www-form-urlencoded", false); } - @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{one\\} for method TestPath.oneFormParamExtractor") + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{one\\} for invocation TestPath.oneFormParamExtractor") public void testNiceNPEFormParam() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestPath.class.getMethod("oneFormParamExtractor", String.class)); - factory(TestPath.class).createRequest(method, Lists. newArrayList((String) null)); + processor.createRequest(method, Lists. newArrayList((String) null)); } static class FirstCharacter implements Function { @@ -1527,7 +1528,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoHeader() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestHeader.class.getMethod("twoHeader", String.class)); - Multimap headers = factory(TestHeader.class).createRequest(method, ImmutableList. of("robot")) + Multimap headers = processor.createRequest(method, ImmutableList. of("robot")) .getHeaders(); assertEquals(headers.size(), 2); assertEquals(headers.get("slash"), ImmutableList.of("/robot")); @@ -1545,7 +1546,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildOneClassHeader() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestClassHeader.class.getMethod("oneHeader", String.class)); - Multimap headers = factory(TestClassHeader.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot")).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/robot")); @@ -1554,7 +1555,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildOneHeader() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestHeader.class.getMethod("oneHeader", String.class)); - Multimap headers = factory(TestHeader.class).createRequest(method, ImmutableList. of("robot")) + Multimap headers = processor.createRequest(method, ImmutableList. of("robot")) .getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/robot")); @@ -1563,7 +1564,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoHeaders() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestHeader.class.getMethod("twoHeaders", String.class, String.class)); - Multimap headers = factory(TestHeader.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot", "eggs")).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/robot/eggs")); @@ -1572,7 +1573,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoHeadersOutOfOrder() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestHeader.class.getMethod("twoHeadersOutOfOrder", String.class, String.class)); - Multimap headers = factory(TestHeader.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot", "eggs")).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/eggs/robot")); @@ -1587,7 +1588,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testQueryInOptions() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQueryReplace.class.getMethod("queryInOptions", String.class, TestReplaceQueryOptions.class)); - String query = factory(TestQueryReplace.class) + String query = processor .createRequest(method, ImmutableList. of("robot", new TestReplaceQueryOptions())).getEndpoint().getQuery(); assertEquals(query, "x-amz-copy-source=/robot"); } @@ -1627,7 +1628,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoQuery() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQueryReplace.class.getMethod("twoQuery", String.class)); - String query = factory(TestQueryReplace.class).createRequest(method, ImmutableList. of("robot")).getEndpoint() + String query = processor.createRequest(method, ImmutableList. of("robot")).getEndpoint() .getQuery(); assertEquals(query, "slash=/robot&hyphen=-robot"); } @@ -1643,7 +1644,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildOneClassQuery() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestClassQuery.class.getMethod("oneQuery", String.class)); - String query = factory(TestClassQuery.class).createRequest(method, ImmutableList. of("robot")).getEndpoint() + String query = processor.createRequest(method, ImmutableList. of("robot")).getEndpoint() .getQuery(); assertEquals(query, "x-amz-copy-source=/robot"); } @@ -1651,7 +1652,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildOneQuery() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQueryReplace.class.getMethod("oneQuery", String.class)); - String query = factory(TestQueryReplace.class).createRequest(method, ImmutableList. of("robot")).getEndpoint() + String query = processor.createRequest(method, ImmutableList. of("robot")).getEndpoint() .getQuery(); assertEquals(query, "x-amz-copy-source=/robot"); } @@ -1659,7 +1660,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoQuerys() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQueryReplace.class.getMethod("twoQuerys", String.class, String.class)); - String query = factory(TestQueryReplace.class).createRequest(method, ImmutableList. of("robot", "eggs")) + String query = processor.createRequest(method, ImmutableList. of("robot", "eggs")) .getEndpoint().getQuery(); assertEquals(query, "x-amz-copy-source=/robot/eggs"); } @@ -1667,7 +1668,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoQuerysOutOfOrder() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestQueryReplace.class.getMethod("twoQuerysOutOfOrder", String.class, String.class)); - String query = factory(TestQueryReplace.class) + String query = processor .createRequest(method, ImmutableList. of("robot", "eggs")).getEndpoint().getQuery(); assertEquals(query, "x-amz-copy-source=/eggs/robot"); } @@ -1703,9 +1704,9 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testPutPayloadEnclosing() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", PayloadEnclosing.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of( + HttpRequest request = processor.createRequest(method, ImmutableList. of( new PayloadEnclosingImpl(newStringPayload("whoops")))); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", false); } @@ -1714,8 +1715,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", PayloadEnclosing.class)); PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(newStringPayload("whoops")); calculateMD5(payloadEnclosing); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payloadEnclosing)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payloadEnclosing)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", true); @@ -1728,8 +1729,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { newInputStreamPayload(Strings2.toInputStream("whoops"))); calculateMD5(payloadEnclosing); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payloadEnclosing)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payloadEnclosing)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", true); @@ -1737,16 +1738,16 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testPutPayloadChunkedNoContentLength() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestTransformers.class.getMethod("putXfer", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(newStringPayload("whoops"))); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(newStringPayload("whoops"))); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Transfer-Encoding: chunked\n"); assertPayloadEquals(request, "whoops", "application/unknown", false); } public void testPutPayload() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(newStringPayload("whoops"))); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(newStringPayload("whoops"))); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", false); } @@ -1755,8 +1756,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); Payload payload = newStringPayload("whoops"); payload.getContentMetadata().setContentDisposition("attachment; filename=photo.jpg"); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", "attachment; filename=photo.jpg", null, null, false); } @@ -1765,8 +1766,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); Payload payload = newStringPayload("whoops"); payload.getContentMetadata().setContentEncoding("gzip"); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", null, "gzip", null, false); } @@ -1775,8 +1776,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); Payload payload = newStringPayload("whoops"); payload.getContentMetadata().setContentLanguage("en"); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", null, null, "en", false); } @@ -1786,8 +1787,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Payload payload = newStringPayload("whoops"); calculateMD5(payload); Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", true); } @@ -1796,8 +1797,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Payload payload = newInputStreamPayload(Strings2.toInputStream("whoops")); payload.getContentMetadata().setContentLength((long) "whoops".length()); Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", false); } @@ -1807,27 +1808,27 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Payload payload = newStringPayload("whoops"); calculateMD5(payload); Invokable method = Invokable.from(TestTransformers.class.getMethod("put", Payload.class)); - HttpRequest request = factory(TestQuery.class).createRequest(method, ImmutableList. of(payload)); - assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1"); + HttpRequest request = processor.createRequest(method, ImmutableList. of(payload)); + assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "whoops", "application/unknown", true); } public void testInputStreamListenableFuture() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestTransformers.class.getMethod("futureInputStream")); - Class> transformer = unwrap(factory(TestTransformers.class), method); + Class> transformer = unwrap(processor, method); assertEquals(transformer, ReturnInputStream.class); } @SuppressWarnings("unchecked") public static Class> unwrap(RestAnnotationProcessor processor, Invokable method) { return (Class>) AsyncRestClientProxy - .getParserOrThrowException(method).getTypeLiteral().getRawType(); + .getParserOrThrowException(Invocation.create(method, ImmutableList.of())).getTypeLiteral().getRawType(); } public void testURIListenableFuture() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestTransformers.class.getMethod("futureUri")); - Class> transformer = unwrap(factory(TestTransformers.class), method); + Class> transformer = unwrap(processor, method); assertEquals(transformer, ParseURIFromListOrLocationHeaderIf20x.class); } @@ -1846,21 +1847,27 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test(expectedExceptions = { RuntimeException.class }) public void testNoTransformer() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestTransformers.class.getMethod("noTransformer")); - AsyncRestClientProxy.getParserOrThrowException(method); + AsyncRestClientProxy.getParserOrThrowException(Invocation.create(method, ImmutableList.of())); } public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException { - GeneratedHttpRequest request = GeneratedHttpRequest.builder() - .method("GET").endpoint("http://localhost").declaring(TestTransformers.class) - .invoker(Invokable.from(TestTransformers.class.getMethod("oneTransformerWithContext"))).build(); - Function transformer = AsyncRestClientProxy.createResponseParser(parserFactory, injector, request.getInvoker(), request); + GeneratedHttpRequest request = GeneratedHttpRequest + .builder() + .method("GET") + .endpoint("http://localhost") + .invocation( + Invocation.create(Invokable.from(TestTransformers.class.getMethod("oneTransformerWithContext")), + ImmutableList.of())) + .build(); + Function transformer = AsyncRestClientProxy.createResponseParser(parserFactory, injector, + request.getInvocation(), request); assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(((ReturnStringIf200Context) transformer).request, request); } public void testOneTransformer() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestTransformers.class.getMethod("oneTransformer")); - Class> transformer = unwrap(factory(TestTransformers.class), method); + Class> transformer = unwrap(processor, method); assertEquals(transformer, ReturnStringIf2xx.class); } @@ -1917,7 +1924,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Date date = new Date(); GetOptions options = GetOptions.Builder.ifModifiedSince(date); Invokable method = Invokable.from(TestRequest.class.getMethod("get", String.class, HttpRequestOptions[].class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1", options)); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", options)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getMethod(), HttpMethod.GET); @@ -1931,7 +1938,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Date date = new Date(); GetOptions options = GetOptions.Builder.ifModifiedSince(date); Invokable method = Invokable.from(TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1", options)); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", options)); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getMethod(), HttpMethod.GET); @@ -1951,7 +1958,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetOptionsThatProducesQuery() throws SecurityException, NoSuchMethodException, IOException { PrefixOptions options = new PrefixOptions().withPrefix("1"); Invokable method = Invokable.from(TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1", options)); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", options)); assertRequestLineEquals(request, "GET http://localhost:9999/1?prefix=1 HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Host: localhost:9999\n"); assertPayloadEquals(request, null, null, false); @@ -1959,7 +1966,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetQuery() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequest.class.getMethod("getQuery", String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getEndpoint().getQuery(), "max-keys=0"); @@ -1969,7 +1976,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetQueryNull() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequest.class.getMethod("getQueryNull", String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getEndpoint().getQuery(), "acl"); @@ -1979,7 +1986,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetQueryEmpty() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestRequest.class.getMethod("getQueryEmpty", String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getEndpoint().getQuery(), "acl="); @@ -1997,7 +2004,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetOptionsThatProducesPayload() throws SecurityException, NoSuchMethodException, IOException { PayloadOptions options = new PayloadOptions(); Invokable method = Invokable.from(TestRequest.class.getMethod("putOptions", String.class, HttpRequestOptions.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1", options)); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", options)); assertRequestLineEquals(request, "PUT http://localhost:9999/1 HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Host: localhost:9999\n"); @@ -2013,7 +2020,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { Invokable method = Invokable.from(TestRequest.class.getMethod("get", String.class, String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of(key, "localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of(key, "localhost")); assertEquals(request.getEndpoint().getHost(), "localhost"); String expectedPath = "/" + URLEncoder.encode(key, "UTF-8").replaceAll("\\+", "%20"); assertEquals(request.getEndpoint().getRawPath(), expectedPath); @@ -2025,7 +2032,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePutRequest() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestRequest.class.getMethod("put", String.class, String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("111", "data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("111", "data")); assertRequestLineEquals(request, "PUT http://localhost:9999/1 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); @@ -2034,7 +2041,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { public void testCreatePutHeader() throws SecurityException, NoSuchMethodException, IOException { Invokable method = Invokable.from(TestRequest.class.getMethod("putHeader", String.class, String.class)); - HttpRequest request = factory(TestRequest.class).createRequest(method, ImmutableList. of("1", "data")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "data")); assertRequestLineEquals(request, "PUT http://localhost:9999/1 HTTP/1.1"); assertNonPayloadHeadersEqual(request, "foo: --1--\n"); @@ -2053,7 +2060,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestVirtualHostMethod.class.getMethod("get", String.class, String.class)); - HttpRequest request = factory(TestVirtualHostMethod.class).createRequest(method, + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "localhost")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); @@ -2077,7 +2084,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testVirtualHost() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestVirtualHost.class.getMethod("get", String.class, String.class)); - HttpRequest request = factory(TestVirtualHost.class).createRequest(method, ImmutableList. of("1", "localhost")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "localhost")); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getMethod(), HttpMethod.GET); @@ -2088,7 +2095,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testHostPrefix() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestVirtualHost.class.getMethod("getPrefix", String.class, String.class)); - HttpRequest request = factory(TestVirtualHost.class).createRequest(method, ImmutableList. of("1", "holy")); + HttpRequest request = processor.createRequest(method, ImmutableList. of("1", "holy")); assertEquals(request.getEndpoint().getHost(), "holy.localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getMethod(), HttpMethod.GET); @@ -2098,7 +2105,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test(expectedExceptions = IllegalArgumentException.class) public void testHostPrefixEmpty() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestVirtualHost.class.getMethod("getPrefix", String.class, String.class)); - factory(TestVirtualHost.class).createRequest(method, ImmutableList. of("1", "")); + processor.createRequest(method, ImmutableList. of("1", "")); } public interface TestHeaders { @@ -2118,7 +2125,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testOneHeader() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable method = Invokable.from(TestHeaders.class.getMethod("oneHeader", String.class)); - Multimap headers = factory(TestHeaders.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot")).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("header"), ImmutableList.of("robot")); @@ -2127,7 +2134,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testOneIntHeader() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable method = Invokable.from(TestHeaders.class.getMethod("oneIntHeader", int.class)); - Multimap headers = factory(TestHeaders.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of(1)).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("header"), ImmutableList.of("1")); @@ -2136,7 +2143,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable method = Invokable.from(TestHeaders.class.getMethod("twoDifferentHeaders", String.class, String.class)); - Multimap headers = factory(TestHeaders.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot", "egg")).getHeaders(); assertEquals(headers.size(), 2); assertEquals(headers.get("header1"), ImmutableList.of("robot")); @@ -2146,7 +2153,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable method = Invokable.from(TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class)); - Multimap headers = factory(TestHeaders.class).createRequest(method, + Multimap headers = processor.createRequest(method, ImmutableList. of("robot", "egg")).getHeaders(); assertEquals(headers.size(), 2); Collection values = headers.get("header"); @@ -2188,17 +2195,18 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testOneEndpointParam() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable method = Invokable.from(TestEndpointParams.class.getMethod("oneEndpointParam", String.class)); - URI uri = RestAnnotationProcessor.getEndpointInParametersOrNull(method, ImmutableList. of("robot"), - injector); + URI uri = RestAnnotationProcessor.getEndpointInParametersOrNull( + Invocation.create(method, ImmutableList. of("robot")), injector); assertEquals(uri, URI.create("robot")); } @Test(expectedExceptions = IllegalStateException.class) public void testTwoDifferentEndpointParams() throws SecurityException, NoSuchMethodException, ExecutionException { - Invokable method = Invokable.from(TestEndpointParams.class.getMethod("twoEndpointParams", String.class, String.class)); - RestAnnotationProcessor.getEndpointInParametersOrNull(method, - ImmutableList. of("robot", "egg"), injector); + Invokable method = Invokable.from(TestEndpointParams.class.getMethod("twoEndpointParams", String.class, + String.class)); + RestAnnotationProcessor.getEndpointInParametersOrNull( + Invocation.create(method, ImmutableList. of("robot", "egg")), injector); } public interface TestPayload { @@ -2217,7 +2225,6 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testPut() throws SecurityException, NoSuchMethodException, IOException { - RestAnnotationProcessor processor = factory(TestPayload.class); Invokable method = Invokable.from(TestPayload.class.getMethod("put", String.class)); GeneratedHttpRequest request = processor.createRequest(method, ImmutableList. of("test")); @@ -2228,7 +2235,6 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void putWithPath() throws SecurityException, NoSuchMethodException, IOException { - RestAnnotationProcessor processor = factory(TestPayload.class); Invokable method = Invokable.from(TestPayload.class.getMethod("putWithPath", String.class, String.class)); GeneratedHttpRequest request = processor.createRequest(method, ImmutableList. of("rabble", "test")); @@ -2278,7 +2284,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoForm() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestFormReplace.class.getMethod("twoForm", String.class)); - Object form = factory(TestFormReplace.class).createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); + Object form = processor.createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); assertEquals(form, "slash=/robot&hyphen=-robot"); } @@ -2330,21 +2336,21 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildOneClassForm() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestClassForm.class.getMethod("oneForm", String.class)); - Object form = factory(TestClassForm.class).createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); + Object form = processor.createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); assertEquals(form, "x-amz-copy-source=/robot"); } @Test public void testBuildOneForm() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestFormReplace.class.getMethod("oneForm", String.class)); - Object form = factory(TestFormReplace.class).createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); + Object form = processor.createRequest(method, ImmutableList. of("robot")).getPayload().getRawContent(); assertEquals(form, "x-amz-copy-source=/robot"); } @Test public void testBuildTwoForms() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestFormReplace.class.getMethod("twoForms", String.class, String.class)); - Object form = factory(TestFormReplace.class).createRequest(method, ImmutableList. of("robot", "eggs")).getPayload() + Object form = processor.createRequest(method, ImmutableList. of("robot", "eggs")).getPayload() .getRawContent(); assertEquals(form, "x-amz-copy-source=/robot/eggs"); } @@ -2352,7 +2358,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @Test public void testBuildTwoFormsOutOfOrder() throws SecurityException, NoSuchMethodException { Invokable method = Invokable.from(TestFormReplace.class.getMethod("twoFormsOutOfOrder", String.class, String.class)); - Object form = factory(TestFormReplace.class).createRequest(method, ImmutableList. of("robot", "eggs")).getPayload() + Object form = processor.createRequest(method, ImmutableList. of("robot", "eggs")).getPayload() .getRawContent(); assertEquals(form, "x-amz-copy-source=/eggs/robot"); } @@ -2387,7 +2393,8 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { } DateService dateService = new SimpleDateFormatDateService(); - + RestAnnotationProcessor processor; + @BeforeClass void setupFactory() { injector = ContextBuilder @@ -2421,6 +2428,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { })).buildInjector(); parserFactory = injector.getInstance(ParseSax.Factory.class); + processor = injector.getInstance(RestAnnotationProcessor.class); } } diff --git a/core/src/test/java/org/jclouds/util/Optionals2Test.java b/core/src/test/java/org/jclouds/util/Optionals2Test.java index 233ee03618..5f156176f4 100644 --- a/core/src/test/java/org/jclouds/util/Optionals2Test.java +++ b/core/src/test/java/org/jclouds/util/Optionals2Test.java @@ -40,26 +40,26 @@ public class Optionals2Test { } public void testReturnTypeOrTypeOfOptionalWhenOptional() throws SecurityException, NoSuchMethodException { - Invokable method = Invokable.from(Test.class.getMethod("getOptional")); + Invokable invoked = Invokable.from(Test.class.getMethod("getOptional")); - assertEquals(Optionals2.returnTypeOrTypeOfOptional(method), String.class); + assertEquals(Optionals2.unwrapIfOptional(invoked.getReturnType()), String.class); } public void testReturnTypeOrTypeOfOptionalWhenNotOptional() throws SecurityException, NoSuchMethodException { - Invokable method = Invokable.from(Test.class.getMethod("getNotOptional")); + Invokable invoked = Invokable.from(Test.class.getMethod("getNotOptional")); - assertEquals(Optionals2.returnTypeOrTypeOfOptional(method), String.class); + assertEquals(Optionals2.unwrapIfOptional(invoked.getReturnType()), String.class); } public void testIsReturnTypeOptionalWhenOptional() throws SecurityException, NoSuchMethodException { - Invokable method = Invokable.from(Test.class.getMethod("getOptional")); + Invokable invoked = Invokable.from(Test.class.getMethod("getOptional")); - assertTrue(Optionals2.isReturnTypeOptional(method)); + assertTrue(Optionals2.isReturnTypeOptional(invoked)); } public void testIsReturnTypeOptionalWhenNotOptional() throws SecurityException, NoSuchMethodException { - Invokable method = Invokable.from(Test.class.getMethod("getNotOptional")); + Invokable invoked = Invokable.from(Test.class.getMethod("getNotOptional")); - assertFalse(Optionals2.isReturnTypeOptional(method)); + assertFalse(Optionals2.isReturnTypeOptional(invoked)); } } diff --git a/core/src/test/java/org/jclouds/util/Throwables2Test.java b/core/src/test/java/org/jclouds/util/Throwables2Test.java index 425a200d4d..98f493e213 100644 --- a/core/src/test/java/org/jclouds/util/Throwables2Test.java +++ b/core/src/test/java/org/jclouds/util/Throwables2Test.java @@ -21,7 +21,7 @@ package org.jclouds.util; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createNiceMock; import static org.jclouds.util.Throwables2.getFirstThrowableOfType; -import static org.jclouds.util.Throwables2.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause; +import static org.jclouds.util.Throwables2.propagateIfPossible; import static org.testng.Assert.assertEquals; import java.io.IOException; @@ -52,17 +52,16 @@ import com.google.inject.spi.Message; @Test public class Throwables2Test { - public void testGetFirstThrowableOfTypeSubclass() { SocketException aex = createMock(SocketException.class); assertEquals(getFirstThrowableOfType(aex, IOException.class), aex); } - + public void testGetFirstThrowableOfTypeOuter() { AuthorizationException aex = createMock(AuthorizationException.class); assertEquals(getFirstThrowableOfType(aex, AuthorizationException.class), aex); } - + public void testGetCause() { AuthorizationException aex = createMock(AuthorizationException.class); Message message = new Message(ImmutableList.of(), "test", aex); @@ -90,7 +89,6 @@ public class Throwables2Test { assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null); } - public void testGetCauseCreation() { AuthorizationException aex = createMock(AuthorizationException.class); Message message = new Message(ImmutableList.of(), "test", aex); @@ -146,132 +144,125 @@ public class Throwables2Test { assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null); } - @Test - public void testReturnExceptionThatsInList() throws Exception { + @Test(expectedExceptions = TestException.class) + public void testPropagateExceptionThatsInList() throws Throwable { Exception e = new TestException(); - assertEquals( - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause( - ImmutableSet.> of(TypeToken.of(TestException.class)), e), e); - assertEquals( - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause( - ImmutableSet.> of(TypeToken.of(TestException.class)), - new RuntimeException(e)), e); + propagateIfPossible(e, ImmutableSet.> of(TypeToken.of(TestException.class))); } @Test(expectedExceptions = TestException.class) - public void testThrowExceptionNotInList() throws Exception { + public void testPropagateWrappedExceptionThatsInList() throws Throwable { Exception e = new TestException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), e); + propagateIfPossible(new RuntimeException(e), + ImmutableSet.> of(TypeToken.of(TestException.class))); + } + + public void testPropagateIfPossibleDoesnThrowExceptionNotInList() throws Throwable { + Exception e = new TestException(); + propagateIfPossible(e, ImmutableSet.> of()); } @Test(expectedExceptions = IllegalStateException.class) - public void testPropagateStandardExceptionIllegalStateException() throws Exception { + public void testPropagateStandardExceptionIllegalStateException() throws Throwable { Exception e = new IllegalStateException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = IllegalArgumentException.class) - public void testPropagateStandardExceptionIllegalArgumentException() throws Exception { + public void testPropagateStandardExceptionIllegalArgumentException() throws Throwable { Exception e = new IllegalArgumentException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = UnsupportedOperationException.class) - public void testPropagateStandardExceptionUnsupportedOperationException() throws Exception { + public void testPropagateStandardExceptionUnsupportedOperationException() throws Throwable { Exception e = new UnsupportedOperationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = AssertionError.class) - public void testPropagateStandardExceptionAssertionError() throws Exception { + public void testPropagateStandardExceptionAssertionError() throws Throwable { AssertionError e = new AssertionError(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = AuthorizationException.class) - public void testPropagateStandardExceptionAuthorizationException() throws Exception { + public void testPropagateStandardExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = AuthorizationException.class) - public void testPropagateProvisionExceptionAuthorizationException() throws Exception { + public void testPropagateProvisionExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new ProvisionException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e)))); + propagateIfPossible( + new ProvisionException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))), + ImmutableSet.> of()); } @Test(expectedExceptions = AuthorizationException.class) - public void testPropagateCreationExceptionAuthorizationException() throws Exception { + public void testPropagateCreationExceptionAuthorizationException() throws Throwable { Exception e = new AuthorizationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new CreationException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e)))); + propagateIfPossible( + new CreationException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))), + ImmutableSet.> of()); } @Test(expectedExceptions = InsufficientResourcesException.class) - public void testPropagateStandardExceptionInsufficientResourcesException() throws Exception { + public void testPropagateStandardExceptionInsufficientResourcesException() throws Throwable { Exception e = new InsufficientResourcesException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = ResourceNotFoundException.class) - public void testPropagateStandardExceptionResourceNotFoundException() throws Exception { + public void testPropagateStandardExceptionResourceNotFoundException() throws Throwable { Exception e = new ResourceNotFoundException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } @Test(expectedExceptions = IllegalStateException.class) - public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Exception { + public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Throwable { Exception e = new IllegalStateException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e)); + propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), + ImmutableSet.> of()); } @Test(expectedExceptions = IllegalArgumentException.class) - public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Exception { + public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Throwable { Exception e = new IllegalArgumentException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e)); + propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), + ImmutableSet.> of()); } @Test(expectedExceptions = UnsupportedOperationException.class) public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException() - throws Exception { + throws Throwable { Exception e = new UnsupportedOperationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e)); + propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), + ImmutableSet.> of()); } @Test(expectedExceptions = AuthorizationException.class) - public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Exception { + public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Throwable { Exception e = new AuthorizationException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e)); + propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), + ImmutableSet.> of()); } @Test(expectedExceptions = ResourceNotFoundException.class) - public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Exception { + public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Throwable { Exception e = new ResourceNotFoundException(); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e)); + propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), + ImmutableSet.> of()); } @Test(expectedExceptions = HttpResponseException.class) - public void testPropagateStandardExceptionHttpResponseException() throws Exception { + public void testPropagateStandardExceptionHttpResponseException() throws Throwable { Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null); - returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(ImmutableSet.> of(), - new RuntimeException(e)); + propagateIfPossible(new RuntimeException(e), ImmutableSet.> of()); } static class TestException extends Exception { private static final long serialVersionUID = 1L; } - } diff --git a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToPath.java b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToPath.java index f9fe587b51..ca892d90dc 100644 --- a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToPath.java +++ b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToPath.java @@ -52,7 +52,7 @@ public class BindToPath implements Binder { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); + checkState(gRequest.getInvocation().getArgs() != null, "args should be initialized at this point"); // Update the request URI with the configured link URI String newEndpoint = getNewEndpoint(gRequest, input); @@ -83,8 +83,8 @@ public class BindToPath implements Binder { * @return The link to be used to build the request URI. */ static RESTLink getLinkToUse(final GeneratedHttpRequest request, final SingleResourceTransportDto payload) { - int argIndex = request.getArgs().indexOf(payload); - Annotation[] annotations = request.getInvoker().getParameters().get(argIndex).getAnnotations(); + int argIndex = request.getInvocation().getArgs().indexOf(payload); + Annotation[] annotations = request.getInvocation().getInvokable().getParameters().get(argIndex).getAnnotations(); EndpointLink linkName = (EndpointLink) Iterables.find(Arrays.asList(annotations), Predicates.instanceOf(EndpointLink.class), null); diff --git a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToXMLPayloadAndPath.java b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToXMLPayloadAndPath.java index ccdc002db0..620a700618 100644 --- a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToXMLPayloadAndPath.java +++ b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/BindToXMLPayloadAndPath.java @@ -54,7 +54,7 @@ public class BindToXMLPayloadAndPath extends BindToXMLPayload { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); + checkState(gRequest.getInvocation().getArgs() != null, "args should be initialized at this point"); // Update the request URI with the configured link URI String newEndpoint = getNewEndpoint(gRequest, payload); diff --git a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayload.java b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayload.java index 3ffd9905cd..f166d31a4a 100644 --- a/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayload.java +++ b/labs/abiquo/src/main/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayload.java @@ -57,10 +57,10 @@ public class BindNetworkConfigurationRefToPayload extends BindToXMLPayload { checkArgument(checkNotNull(input, "input") instanceof VLANNetworkDto, "this binder is only valid for VLANNetworkDto"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); + checkState(gRequest.getInvocation().getArgs() != null, "args should be initialized at this point"); VLANNetworkDto network = (VLANNetworkDto) input; - VirtualMachineDto vm = (VirtualMachineDto) Iterables.find(gRequest.getArgs(), + VirtualMachineDto vm = (VirtualMachineDto) Iterables.find(gRequest.getInvocation().getArgs(), Predicates.instanceOf(VirtualMachineDto.class)); RESTLink configLink = checkNotNull(vm.searchLink("configurations"), "missing required link"); diff --git a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/BindToPathTest.java b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/BindToPathTest.java index ad71c57d2b..fd0c20a3d3 100644 --- a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/BindToPathTest.java +++ b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/BindToPathTest.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.MediaType; import org.jclouds.abiquo.rest.annotations.EndpointLink; import org.jclouds.http.HttpRequest; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.binders.BindException; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; @@ -60,8 +61,9 @@ public class BindToPathTest { @Test(expectedExceptions = NullPointerException.class) public void testInvalidNullInput() throws SecurityException, NoSuchMethodException { Invokable withEndpointLink = Invokable.from(TestEndpointLink.class.getMethod("withEndpointLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withEndpointLink).args(ImmutableList. of(new TestDto())).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(new TestDto()))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindToPath binder = new BindToPath(); @@ -71,8 +73,9 @@ public class BindToPathTest { @Test(expectedExceptions = IllegalArgumentException.class) public void testInvalidInputType() throws SecurityException, NoSuchMethodException { Invokable withEndpointLink = Invokable.from(TestEndpointLink.class.getMethod("withEndpointLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withEndpointLink).args(ImmutableList. of(new TestDto())).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(new TestDto()))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindToPath binder = new BindToPath(); @@ -83,8 +86,9 @@ public class BindToPathTest { public void testAnnotationNotPresent() throws SecurityException, NoSuchMethodException { TestDto dto = new TestDto(); Invokable withoutEndpointLink = Invokable.from(TestEndpointLink.class.getMethod("withoutEndpointLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withoutEndpointLink).args(ImmutableList. of(dto)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withoutEndpointLink, ImmutableList. of(dto))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindToPath binder = new BindToPath(); @@ -95,8 +99,9 @@ public class BindToPathTest { public void testLinkNotPresent() throws SecurityException, NoSuchMethodException { TestDto dto = new TestDto(); Invokable withUnexistingLink = Invokable.from(TestEndpointLink.class.getMethod("withUnexistingLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withUnexistingLink).args(ImmutableList. of(dto)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withUnexistingLink, ImmutableList. of(dto))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindToPath binder = new BindToPath(); @@ -106,8 +111,9 @@ public class BindToPathTest { public void testBindWithoutParameters() throws SecurityException, NoSuchMethodException { TestDto dto = new TestDto(); Invokable withEndpointLink = Invokable.from(TestEndpointLink.class.getMethod("withEndpointLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withEndpointLink).args(ImmutableList. of(dto)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(dto))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindToPath binder = new BindToPath(); @@ -118,8 +124,9 @@ public class BindToPathTest { public void testBindWithQueryParameters() throws SecurityException, NoSuchMethodException { TestDto dto = new TestDto(); Invokable withEndpointLink = Invokable.from(TestEndpointLink.class.getMethod("withEndpointLink", TestDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestEndpointLink.class) - .invoker(withEndpointLink).args(ImmutableList. of(dto)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(dto))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost?param=value")).build(); BindToPath binder = new BindToPath(); diff --git a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindMoveVolumeToPathTest.java b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindMoveVolumeToPathTest.java index 759b6c3512..ee0231d19a 100644 --- a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindMoveVolumeToPathTest.java +++ b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindMoveVolumeToPathTest.java @@ -27,6 +27,7 @@ import javax.ws.rs.HttpMethod; import org.jclouds.abiquo.domain.CloudResources; import org.jclouds.abiquo.features.CloudAsyncApi; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; @@ -63,8 +64,8 @@ public class BindMoveVolumeToPathTest { private static GeneratedHttpRequest generatedHttpRequest() throws SecurityException, NoSuchMethodException { Invokable withEndpointLink = Invokable.from(CloudAsyncApi.class.getMethod("moveVolume", VolumeManagementDto.class, VirtualDatacenterDto.class)); - return GeneratedHttpRequest.builder().declaring(CloudAsyncApi.class).invoker(withEndpointLink) - .args(ImmutableList. of(CloudResources.volumePut(), CloudResources.virtualDatacenterPut())) + return GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(CloudResources.volumePut(), CloudResources.virtualDatacenterPut()))) .method(HttpMethod.POST).endpoint(URI.create("http://localhost")).build(); } } diff --git a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayloadTest.java b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayloadTest.java index 0c294863f0..49b41d7d00 100644 --- a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayloadTest.java +++ b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/cloud/BindNetworkConfigurationRefToPayloadTest.java @@ -32,6 +32,7 @@ import javax.ws.rs.HttpMethod; import org.jclouds.abiquo.domain.CloudResources; import org.jclouds.abiquo.domain.NetworkResources; import org.jclouds.http.HttpRequest; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.xml.internal.JAXBParser; import org.testng.annotations.Test; @@ -69,8 +70,9 @@ public class BindNetworkConfigurationRefToPayloadTest { Invokable method = Invokable.from(TestNetworkConfig.class.getMethod("withAll", VirtualMachineDto.class, VLANNetworkDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestNetworkConfig.class) - .invoker(method).args(Lists. newArrayList(vm, null)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(method, Lists. newArrayList(vm, null))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindNetworkConfigurationRefToPayload binder = new BindNetworkConfigurationRefToPayload(new JAXBParser("false")); @@ -84,8 +86,9 @@ public class BindNetworkConfigurationRefToPayloadTest { Invokable method = Invokable.from(TestNetworkConfig.class.getMethod("withAll", VirtualMachineDto.class, VLANNetworkDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestNetworkConfig.class) - .invoker(method).args(ImmutableList. of(vm, network)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(method, ImmutableList. of(vm, network))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindNetworkConfigurationRefToPayload binder = new BindNetworkConfigurationRefToPayload(new JAXBParser("false")); @@ -98,8 +101,9 @@ public class BindNetworkConfigurationRefToPayloadTest { Invokable method = Invokable.from(TestNetworkConfig.class.getMethod("withoutVirtualMachine", VLANNetworkDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestNetworkConfig.class) - .invoker(method).args(ImmutableList. of(network)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(method, ImmutableList. of(network))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindNetworkConfigurationRefToPayload binder = new BindNetworkConfigurationRefToPayload(new JAXBParser("false")); @@ -112,8 +116,9 @@ public class BindNetworkConfigurationRefToPayloadTest { Invokable method = Invokable.from(TestNetworkConfig.class.getMethod("withAll", VirtualMachineDto.class, VLANNetworkDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(TestNetworkConfig.class) - .invoker(method).args(ImmutableList. of(vm, network)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(method, ImmutableList. of(vm, network))) + .method(HttpMethod.GET) .endpoint(URI.create("http://localhost")).build(); BindNetworkConfigurationRefToPayload binder = new BindNetworkConfigurationRefToPayload(new JAXBParser("false")); diff --git a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/infrastructure/BindSupportedDevicesLinkToPathTest.java b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/infrastructure/BindSupportedDevicesLinkToPathTest.java index 823b6935d1..e7cd227b2d 100644 --- a/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/infrastructure/BindSupportedDevicesLinkToPathTest.java +++ b/labs/abiquo/src/test/java/org/jclouds/abiquo/binders/infrastructure/BindSupportedDevicesLinkToPathTest.java @@ -26,6 +26,7 @@ import java.net.URI; import javax.ws.rs.HttpMethod; import org.jclouds.abiquo.features.InfrastructureAsyncApi; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; @@ -62,8 +63,9 @@ public class BindSupportedDevicesLinkToPathTest { Invokable withEndpointLink = Invokable.from(InfrastructureAsyncApi.class.getMethod( "listSupportedStorageDevices", DatacenterDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(InfrastructureAsyncApi.class) - .invoker(withEndpointLink).args(ImmutableList. of(datacenter)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(datacenter))) + .method(HttpMethod.GET) .endpoint(URI.create("http://foo/bar")).build(); assertEquals(binder.getNewEndpoint(request, datacenter), "http://foo/bar/action/supported"); @@ -78,8 +80,9 @@ public class BindSupportedDevicesLinkToPathTest { Invokable withEndpointLink = Invokable.from(InfrastructureAsyncApi.class.getMethod( "listSupportedStorageDevices", DatacenterDto.class)); - GeneratedHttpRequest request = GeneratedHttpRequest.builder().declaring(InfrastructureAsyncApi.class) - .invoker(withEndpointLink).args(ImmutableList. of(datacenter)).method(HttpMethod.GET) + GeneratedHttpRequest request = GeneratedHttpRequest.builder() + .invocation(Invocation.create(withEndpointLink, ImmutableList. of(datacenter))) + .method(HttpMethod.GET) .endpoint(URI.create("http://foo/bar")).build(); assertEquals(binder.getNewEndpoint(request, datacenter), "http://foo/bar/action/supported"); diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/BaseToPagedIterable.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/BaseToPagedIterable.java index de07a03170..c57ab52631 100644 --- a/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/BaseToPagedIterable.java +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/BaseToPagedIterable.java @@ -51,10 +51,10 @@ public abstract class BaseToPagedIterable Optional project = tryFind(request.getCaller().get().getArgs(), instanceOf(String.class)); - Optional listOptions = tryFind(request.getArgs(), instanceOf(ListOptions.class)); + Optional listOptions = tryFind(request.getInvocation().getArgs(), instanceOf(ListOptions.class)); assert project.isPresent() : String.format("programming error, method %s should have a string param for the " - + "project", request.getCaller().get().getInvoker()); + + "project", request.getCaller().get().getInvokable()); return PagedIterables.advance( input, diff --git a/labs/oauth/src/main/java/org/jclouds/oauth/v2/functions/BuildTokenRequest.java b/labs/oauth/src/main/java/org/jclouds/oauth/v2/functions/BuildTokenRequest.java index 44cd58e29c..77d2fa8e20 100644 --- a/labs/oauth/src/main/java/org/jclouds/oauth/v2/functions/BuildTokenRequest.java +++ b/labs/oauth/src/main/java/org/jclouds/oauth/v2/functions/BuildTokenRequest.java @@ -43,6 +43,7 @@ import com.google.common.base.Joiner; import com.google.common.base.Supplier; import com.google.common.base.Ticker; import com.google.common.collect.ImmutableMap; +import com.google.common.reflect.Invokable; import com.google.inject.Inject; import com.google.inject.name.Named; @@ -114,16 +115,19 @@ public class BuildTokenRequest implements Function invokable = request.getInvocation().getInvokable(); + Class interfaceType = request.getInvocation().getInterfaceType(); + + OAuthScopes classScopes = interfaceType.getAnnotation(OAuthScopes.class); + OAuthScopes methodScopes = invokable.getAnnotation(OAuthScopes.class); // if no annotations are present the rely on globally set scopes if (classScopes == null && methodScopes == null) { checkState(globalScopes != null, String.format("REST class or method should be annotated " + "with OAuthScopes specifying required permissions. Alternatively a global property " + "\"oauth.scopes\" may be set to define scopes globally. REST Class: %s, Method: %s", - request.getDeclaring().getName(), - request.getInvoker().getName())); + interfaceType.getName(), + invokable.getName())); return globalScopes; } diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BaseBindVMSpecToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BaseBindVMSpecToXmlPayload.java index a2b5969130..36345300c4 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BaseBindVMSpecToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BaseBindVMSpecToXmlPayload.java @@ -20,11 +20,10 @@ package org.jclouds.savvis.vpdc.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.util.Map; -import java.util.Properties; import java.util.Map.Entry; +import java.util.Properties; import javax.ws.rs.core.MediaType; import javax.xml.parsers.FactoryConfigurationError; @@ -59,8 +58,6 @@ public abstract class BaseBindVMSpecToXmlPayload extends BindToStringPayload checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); - request = super.bindToRequest(request, generateXml(findSpecInArgsOrNull(gRequest))); request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); return request; diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCaptureVAppTemplateToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCaptureVAppTemplateToXmlPayload.java index 8cb77c059c..de2a9aa6d4 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCaptureVAppTemplateToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCaptureVAppTemplateToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.savvis.vpdc.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.net.URI; import java.util.Map; @@ -53,7 +52,7 @@ public class BindCaptureVAppTemplateToXmlPayload extends BindToStringPayload imp } protected URI findVAppURIInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof URI) { return (URI) arg; } else if (arg instanceof FirewallRule[]) { @@ -69,8 +68,6 @@ public class BindCaptureVAppTemplateToXmlPayload extends BindToStringPayload imp checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); - request = super.bindToRequest(request, generateXml(findVAppURIInArgsOrNull(gRequest))); request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCloneVMToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCloneVMToXmlPayload.java index e0a819db8e..6ff53fdd17 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCloneVMToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindCloneVMToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.savvis.vpdc.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.net.URI; import java.util.Map; @@ -53,7 +52,7 @@ public class BindCloneVMToXmlPayload extends BindToStringPayload implements MapB } protected URI findVAppURIInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof URI) { return (URI) arg; } else if (arg instanceof FirewallRule[]) { @@ -69,8 +68,6 @@ public class BindCloneVMToXmlPayload extends BindToStringPayload implements MapB checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); - request = super.bindToRequest(request, generateXml(findVAppURIInArgsOrNull(gRequest), (String) postParams.get("name"), (String) postParams.get("networkTierName"))); request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindFirewallRuleToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindFirewallRuleToXmlPayload.java index dd78c5ff11..ef7c8d52a4 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindFirewallRuleToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindFirewallRuleToXmlPayload.java @@ -20,7 +20,6 @@ package org.jclouds.savvis.vpdc.binders; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import java.util.Map; import java.util.Properties; @@ -52,7 +51,7 @@ public class BindFirewallRuleToXmlPayload extends BindToStringPayload implements } protected FirewallRule findRuleInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof FirewallRule) { return (FirewallRule) arg; } else if (arg instanceof FirewallRule[]) { @@ -68,8 +67,6 @@ public class BindFirewallRuleToXmlPayload extends BindToStringPayload implements checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; - checkState(gRequest.getArgs() != null, "args should be initialized at this point"); - request = super.bindToRequest(request, generateXml(findRuleInArgsOrNull(gRequest))); request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecToXmlPayload.java index b7430253fb..fb7c5fd737 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecToXmlPayload.java @@ -36,7 +36,7 @@ import com.jamesmurty.utils.XMLBuilder; public class BindVMSpecToXmlPayload extends BaseBindVMSpecToXmlPayload { protected VMSpec findSpecInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof VMSpec) { return VMSpec.class.cast(arg); } diff --git a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecsToXmlPayload.java b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecsToXmlPayload.java index 2621466ec2..57cc60902a 100644 --- a/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecsToXmlPayload.java +++ b/labs/savvis-symphonyvpdc/src/main/java/org/jclouds/savvis/vpdc/binders/BindVMSpecsToXmlPayload.java @@ -40,7 +40,7 @@ public class BindVMSpecsToXmlPayload extends BaseBindVMSpecToXmlPayload findSpecInArgsOrNull(GeneratedHttpRequest gRequest) { - for (Object arg : gRequest.getArgs()) { + for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof Iterable) { Iterable specs = (Iterable) arg; checkArgument(Iterables.size(specs) > 0, diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/parse/DescribeSecurityGroupsResponseTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/parse/DescribeSecurityGroupsResponseTest.java index 0394d2baae..8463dfc076 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/parse/DescribeSecurityGroupsResponseTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/parse/DescribeSecurityGroupsResponseTest.java @@ -18,6 +18,7 @@ */ package org.jclouds.aws.ec2.parse; +import static com.google.common.base.Throwables.propagate; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; @@ -32,11 +33,12 @@ import org.jclouds.ec2.domain.SecurityGroup; import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.ec2.xml.DescribeSecurityGroupsResponseHandler; import org.jclouds.http.functions.ParseSax; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.reflect.Invokable; /** * @@ -78,9 +80,6 @@ public class DescribeSecurityGroupsResponseTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java index 73d4a37eaa..f8edb6a894 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java @@ -18,9 +18,6 @@ s * Licensed to jclouds, Inc. (jclouds) under one or more */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -37,13 +34,11 @@ import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.location.Region; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.TypeLiteral; @@ -116,9 +111,6 @@ public class AWSRunInstancesResponseHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribePlacementGroupsResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribePlacementGroupsResponseHandlerTest.java index 76c090efc8..1564e90221 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribePlacementGroupsResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribePlacementGroupsResponseHandlerTest.java @@ -18,9 +18,7 @@ */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; +import static com.google.common.collect.Iterables.getOnlyElement; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,12 +26,8 @@ import java.io.InputStream; import org.jclouds.aws.ec2.domain.PlacementGroup; import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; - /** * Tests behavior of {@code DescribePlacementGroupsResponseHandler} * @@ -50,15 +44,12 @@ public class DescribePlacementGroupsResponseHandlerTest extends BaseEC2HandlerTe DescribePlacementGroupsResponseHandler handler = injector .getInstance(DescribePlacementGroupsResponseHandler.class); addDefaultRegionToHandler(handler); - PlacementGroup result = Iterables.getOnlyElement(factory.create(handler).parse(is)); + PlacementGroup result = getOnlyElement(factory.create(handler).parse(is)); assertEquals(result, expected); } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()); - replay(request); handler.setContext(request); } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeReservedInstancesOfferingResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeReservedInstancesOfferingResponseHandlerTest.java index 611ecdddb9..a8757f609d 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeReservedInstancesOfferingResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeReservedInstancesOfferingResponseHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -28,10 +25,8 @@ import java.io.InputStream; import org.jclouds.ec2.domain.ReservedInstancesOffering; import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.http.functions.ParseSax; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; /** @@ -57,9 +52,6 @@ public class DescribeReservedInstancesOfferingResponseHandlerTest extends BaseEC } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList.of()); - replay(request); handler.setContext(request); } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java index a2534ac3a5..9e88b4fbee 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -35,13 +32,11 @@ import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.location.Region; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.TypeLiteral; @@ -136,9 +131,6 @@ public class SpotInstanceHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java index 6e47775969..a71970059e 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java @@ -18,9 +18,6 @@ */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -33,14 +30,12 @@ import org.jclouds.ec2.xml.BaseEC2HandlerTest; import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.location.Region; -import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.google.common.base.Function; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.inject.AbstractModule; @@ -119,9 +114,6 @@ public class SpotInstancesHandlerTest extends BaseEC2HandlerTest { } private void addDefaultRegionToHandler(ParseSax.HandlerWithResult handler) { - GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); - expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); - replay(request); handler.setContext(request); } } diff --git a/providers/aws-s3/src/main/java/org/jclouds/aws/s3/blobstore/config/AWSS3BlobStoreContextModule.java b/providers/aws-s3/src/main/java/org/jclouds/aws/s3/blobstore/config/AWSS3BlobStoreContextModule.java index 5f56e5d66c..89cd87ad69 100644 --- a/providers/aws-s3/src/main/java/org/jclouds/aws/s3/blobstore/config/AWSS3BlobStoreContextModule.java +++ b/providers/aws-s3/src/main/java/org/jclouds/aws/s3/blobstore/config/AWSS3BlobStoreContextModule.java @@ -20,16 +20,19 @@ package org.jclouds.aws.s3.blobstore.config; import java.util.concurrent.TimeUnit; +import org.jclouds.aws.s3.AWSS3AsyncClient; import org.jclouds.aws.s3.blobstore.AWSS3AsyncBlobStore; import org.jclouds.aws.s3.blobstore.AWSS3BlobStore; import org.jclouds.aws.s3.blobstore.strategy.AsyncMultipartUploadStrategy; import org.jclouds.aws.s3.blobstore.strategy.MultipartUploadStrategy; import org.jclouds.aws.s3.blobstore.strategy.internal.ParallelMultipartUploadStrategy; import org.jclouds.aws.s3.blobstore.strategy.internal.SequentialMultipartUploadStrategy; +import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.cache.RetryingCacheLoaderDecorator; import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.s3.S3Client; import org.jclouds.s3.blobstore.S3AsyncBlobStore; +import org.jclouds.s3.blobstore.S3BlobRequestSigner; import org.jclouds.s3.blobstore.S3BlobStore; import org.jclouds.s3.blobstore.config.S3BlobStoreContextModule; import org.jclouds.s3.domain.AccessControlList; @@ -38,6 +41,7 @@ import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.inject.Scopes; +import com.google.inject.TypeLiteral; /** * @@ -54,7 +58,13 @@ public class AWSS3BlobStoreContextModule extends S3BlobStoreContextModule { bind(MultipartUploadStrategy.class).to(SequentialMultipartUploadStrategy.class); bind(AsyncMultipartUploadStrategy.class).to(ParallelMultipartUploadStrategy.class); } - + + @Override + protected void bindRequestSigner() { + bind(BlobRequestSigner.class).to(new TypeLiteral>() { + }); + } + @Override protected LoadingCache bucketAcls(final S3Client client) { CacheLoader loader = RetryingCacheLoaderDecorator.newDecorator() diff --git a/providers/azureblob/src/main/java/org/jclouds/azureblob/blobstore/AzureBlobRequestSigner.java b/providers/azureblob/src/main/java/org/jclouds/azureblob/blobstore/AzureBlobRequestSigner.java index ef2d89fa86..934f7a8e44 100644 --- a/providers/azureblob/src/main/java/org/jclouds/azureblob/blobstore/AzureBlobRequestSigner.java +++ b/providers/azureblob/src/main/java/org/jclouds/azureblob/blobstore/AzureBlobRequestSigner.java @@ -32,6 +32,7 @@ import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.functions.BlobToHttpGetOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.options.GetOptions; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.internal.RestAnnotationProcessor; import com.google.common.collect.ImmutableList; @@ -52,9 +53,9 @@ public class AzureBlobRequestSigner implements BlobRequestSigner { private final Invokable createMethod; @Inject - public AzureBlobRequestSigner(RestAnnotationProcessor.Factory processor, BlobToAzureBlob blobToBlob, + public AzureBlobRequestSigner(RestAnnotationProcessor processor, BlobToAzureBlob blobToBlob, BlobToHttpGetOptions blob2HttpGetOptions) throws SecurityException, NoSuchMethodException { - this.processor = checkNotNull(processor, "processor").declaring(AzureBlobAsyncClient.class); + this.processor = checkNotNull(processor, "processor"); this.blobToBlob = checkNotNull(blobToBlob, "blobToBlob"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); this.getMethod = Invokable.from(AzureBlobAsyncClient.class.getMethod("getBlob", String.class, String.class, @@ -69,7 +70,7 @@ public class AzureBlobRequestSigner implements BlobRequestSigner { public HttpRequest signGetBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(getMethod, ImmutableList. of(container, name)))); } @Override @@ -81,8 +82,8 @@ public class AzureBlobRequestSigner implements BlobRequestSigner { public HttpRequest signPutBlob(String container, Blob blob) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - return cleanRequest(processor.createRequest(createMethod, - ImmutableList. of(container, blobToBlob.apply(blob)))); + return cleanRequest(processor.apply(Invocation.create(createMethod, + ImmutableList. of(container, blobToBlob.apply(blob))))); } @Override @@ -94,14 +95,14 @@ public class AzureBlobRequestSigner implements BlobRequestSigner { public HttpRequest signRemoveBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(deleteMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(deleteMethod, ImmutableList. of(container, name)))); } @Override public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, - ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options"))))); + return cleanRequest(processor.apply(Invocation.create(getMethod, + ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options")))))); } } diff --git a/providers/azureblob/src/main/java/org/jclouds/azureblob/functions/ParseBlobPropertiesFromHeaders.java b/providers/azureblob/src/main/java/org/jclouds/azureblob/functions/ParseBlobPropertiesFromHeaders.java index db7fc931ac..82ac84c81f 100644 --- a/providers/azureblob/src/main/java/org/jclouds/azureblob/functions/ParseBlobPropertiesFromHeaders.java +++ b/providers/azureblob/src/main/java/org/jclouds/azureblob/functions/ParseBlobPropertiesFromHeaders.java @@ -68,7 +68,7 @@ public class ParseBlobPropertiesFromHeaders implements Function formParams = ImmutableMultimap.builder(); for(String key : postParams.keySet()) formParams.put(key, (String) postParams.get(key)); - ServerSpec serverSpec = ServerSpec.class.cast(find(gRequest.getArgs(), instanceOf(ServerSpec.class))); + ServerSpec serverSpec = ServerSpec.class.cast(find(gRequest.getInvocation().getArgs(), + instanceOf(ServerSpec.class))); formParams.put("datacenter", serverSpec.getDatacenter()); formParams.put("platform", serverSpec.getPlatform()); formParams.put("templatename", serverSpec.getTemplateName()); diff --git a/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java b/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java index 4fc3634117..9c881a59ea 100644 --- a/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java +++ b/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java @@ -49,6 +49,7 @@ import org.jclouds.openstack.keystone.v2_0.domain.Access; import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; import org.jclouds.openstack.swift.blobstore.functions.BlobToObject; import org.jclouds.openstack.swift.domain.SwiftObject; +import org.jclouds.reflect.Invocation; import org.jclouds.rest.annotations.Credential; import org.jclouds.rest.annotations.Identity; import org.jclouds.rest.internal.RestAnnotationProcessor; @@ -61,9 +62,8 @@ import com.google.common.reflect.Invokable; import com.google.inject.Provider; /** - * Signer for HP's variant of temporary signed URLs. They prefix the signature - * with the tenant id. - * + * Signer for HP's variant of temporary signed URLs. They prefix the signature with the tenant id. + * * @author Andrew Gaul */ @Singleton @@ -86,13 +86,11 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner private final Invokable createMethod; @Inject - public HPCloudObjectStorageBlobRequestSigner(RestAnnotationProcessor.Factory processor, BlobToObject blobToObject, - BlobToHttpGetOptions blob2HttpGetOptions, - Crypto crypto, @TimeStamp Provider unixEpochTimestampProvider, - Supplier access, - @Identity String accessKey, @Credential String secretKey) - throws SecurityException, NoSuchMethodException { - this.processor = checkNotNull(processor, "processor").declaring(HPCloudObjectStorageAsyncApi.class); + public HPCloudObjectStorageBlobRequestSigner(RestAnnotationProcessor processor, BlobToObject blobToObject, + BlobToHttpGetOptions blob2HttpGetOptions, Crypto crypto, @TimeStamp Provider unixEpochTimestampProvider, + Supplier access, @Identity String accessKey, @Credential String secretKey) throws SecurityException, + NoSuchMethodException { + this.processor = checkNotNull(processor, "processor"); this.crypto = checkNotNull(crypto, "crypto"); this.unixEpochTimestampProvider = checkNotNull(unixEpochTimestampProvider, "unixEpochTimestampProvider"); @@ -104,10 +102,12 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); - this.getMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("getObject", String.class, String.class, - GetOptions[].class)); - this.deleteMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("removeObject", String.class, String.class)); - this.createMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("putObject", String.class, SwiftObject.class)); + this.getMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("getObject", String.class, + String.class, GetOptions[].class)); + this.deleteMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("removeObject", String.class, + String.class)); + this.createMethod = Invokable.from(HPCloudObjectStorageAsyncApi.class.getMethod("putObject", String.class, + SwiftObject.class)); } @PostConstruct @@ -120,14 +120,16 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner public HttpRequest signGetBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, getMethod, + ImmutableList. of(container, name)))); } @Override public HttpRequest signGetBlob(String container, String name, long timeInSeconds) { checkNotNull(container, "container"); checkNotNull(name, "name"); - HttpRequest request = processor.createRequest(getMethod, ImmutableList. of(container, name)); + HttpRequest request = processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, getMethod, + ImmutableList. of(container, name))); return cleanRequest(signForTemporaryAccess(request, timeInSeconds)); } @@ -135,24 +137,24 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(getMethod, ImmutableList.of(container, name, - blob2HttpGetOptions.apply(checkNotNull(options, "options"))))); + return cleanRequest(processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, getMethod, + ImmutableList.of(container, name, blob2HttpGetOptions.apply(checkNotNull(options, "options")))))); } @Override public HttpRequest signPutBlob(String container, Blob blob) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - return cleanRequest(processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob)))); + return cleanRequest(processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, createMethod, + ImmutableList. of(container, blobToObject.apply(blob))))); } @Override public HttpRequest signPutBlob(String container, Blob blob, long timeInSeconds) { checkNotNull(container, "container"); checkNotNull(blob, "blob"); - HttpRequest request = processor.createRequest(createMethod, - ImmutableList. of(container, blobToObject.apply(blob))); + HttpRequest request = processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, createMethod, + ImmutableList. of(container, blobToObject.apply(blob)))); return cleanRequest(signForTemporaryAccess(request, timeInSeconds)); } @@ -160,7 +162,8 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner public HttpRequest signRemoveBlob(String container, String name) { checkNotNull(container, "container"); checkNotNull(name, "name"); - return cleanRequest(processor.createRequest(deleteMethod, ImmutableList. of(container, name))); + return cleanRequest(processor.apply(Invocation.create(HPCloudObjectStorageAsyncApi.class, deleteMethod, + ImmutableList. of(container, name)))); } private HttpRequest signForTemporaryAccess(HttpRequest request, long timeInSeconds) {