From c57e09c5aee1f7e7e1b94b2be4079dbab41d5491 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sat, 18 Dec 2010 21:36:52 +0100 Subject: [PATCH] Issue 412: updated syntax for readDrive based on notes from elasticstack --- .../elasticstack/ElasticStackAsyncClient.java | 22 +---- .../elasticstack/ElasticStackClient.java | 14 +-- .../binders/BindReadDriveOptionsToPath.java | 59 ------------ .../options/ReadDriveOptions.java | 91 ------------------- .../CommonElasticStackClientLiveTest.java | 36 ++++---- .../ElasticStackAsyncClientTest.java | 21 +---- .../ElasticStackClientLiveTest.java | 7 +- .../BindReadDriveOptionsToPathTest.java | 51 ----------- .../options/ReadDriveOptionsTest.java | 83 ----------------- 9 files changed, 33 insertions(+), 351 deletions(-) delete mode 100644 sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPath.java delete mode 100644 sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/options/ReadDriveOptions.java delete mode 100644 sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPathTest.java delete mode 100644 sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/options/ReadDriveOptionsTest.java diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackAsyncClient.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackAsyncClient.java index 4314cdfb24..cea0b3c52f 100644 --- a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackAsyncClient.java +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackAsyncClient.java @@ -20,20 +20,16 @@ package org.jclouds.elasticstack; import javax.ws.rs.Consumes; -import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import org.jclouds.elasticstack.binders.BindReadDriveOptionsToPath; import org.jclouds.elasticstack.domain.ImageConversionType; import org.jclouds.elasticstack.functions.ReturnPayload; -import org.jclouds.elasticstack.options.ReadDriveOptions; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.io.Payload; -import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; @@ -72,25 +68,15 @@ public interface ElasticStackAsyncClient extends CommonElasticStackAsyncClient { @PathParam("conversion") ImageConversionType conversionType); /** - * @see ElasticStackClient#readDrive(String) - */ - @GET - @Consumes(MediaType.APPLICATION_OCTET_STREAM) - @Path("/drives/{uuid}/read") - @ResponseParser(ReturnPayload.class) - @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture readDrive(@PathParam("uuid") String uuid); - - /** - * @see ElasticStackClient#readDrive(String,ReadDriveOptions) + * @see ElasticStackClient#readDrive */ @POST @Consumes(MediaType.APPLICATION_OCTET_STREAM) - @Path("/drives/{uuid}/read") + @Path("/drives/{uuid}/read/{offset}/{size}") @ResponseParser(ReturnPayload.class) @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture readDrive(@PathParam("uuid") String uuid, - @BinderParam(BindReadDriveOptionsToPath.class) ReadDriveOptions options); + ListenableFuture readDrive(@PathParam("uuid") String uuid, @PathParam("offset") long offset, + @PathParam("size") long size); /** * @see ElasticStackClient#writeDrive(String, Payload) diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackClient.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackClient.java index 5d4998c366..129ac4a94f 100644 --- a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackClient.java +++ b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/ElasticStackClient.java @@ -23,7 +23,6 @@ import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; import org.jclouds.elasticstack.domain.ImageConversionType; -import org.jclouds.elasticstack.options.ReadDriveOptions; import org.jclouds.io.Payload; /** @@ -60,16 +59,13 @@ public interface ElasticStackClient extends CommonElasticStackClient { * * @param uuid * drive to read + * @param offset + * start at the specified offset in bytes + * @param size + * the specified size in bytes; must be <=4096k * @return binary content of the drive. */ - Payload readDrive(String uuid); - - /** - * @see #readDrive(String) - * @param options - * controls offset and size of the request - */ - Payload readDrive(String uuid, ReadDriveOptions options); + Payload readDrive(String uuid, long offset, long size); /** * Write binary data to a drive diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPath.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPath.java deleted file mode 100644 index 878b4bca3a..0000000000 --- a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPath.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * - * Copyright (C) 2010 Cloud Conscious, LLC. - * - * ==================================================================== - * Licensed 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.elasticstack.binders; - -import static com.google.common.base.Preconditions.checkArgument; - -import javax.inject.Inject; -import javax.inject.Provider; -import javax.inject.Singleton; -import javax.ws.rs.core.UriBuilder; - -import org.jclouds.elasticstack.options.ReadDriveOptions; -import org.jclouds.http.HttpRequest; -import org.jclouds.rest.Binder; - -/** - * - * @author Adrian Cole - */ -@Singleton -public class BindReadDriveOptionsToPath implements Binder { - private final Provider uriBuilderProvider; - - @Inject - public BindReadDriveOptionsToPath(Provider uriBuilderProvider) { - this.uriBuilderProvider = uriBuilderProvider; - } - - public void bindToRequest(HttpRequest request, Object payload) { - checkArgument(payload instanceof ReadDriveOptions, "this binder is only valid for ReadDriveOptions!"); - ReadDriveOptions options = ReadDriveOptions.class.cast(payload); - if (options.getOffset() != null || options.getSize() != null){ - UriBuilder builder = uriBuilderProvider.get().uri(request.getEndpoint()); - if (options.getOffset() != null) - builder.path("/"+options.getOffset()); - if (options.getSize() != null) - builder.path("/"+options.getSize()); - request.setEndpoint(builder.build()); - } - - } -} diff --git a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/options/ReadDriveOptions.java b/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/options/ReadDriveOptions.java deleted file mode 100644 index cfbe4af50e..0000000000 --- a/sandbox/elasticstack/src/main/java/org/jclouds/elasticstack/options/ReadDriveOptions.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * - * Copyright (C) 2010 Cloud Conscious, LLC. - * - * ==================================================================== - * Licensed 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.elasticstack.options; - -import static com.google.common.base.Preconditions.checkArgument; - -/** - * Contains options supported for read drive operations.

- * Usage

The recommended way to instantiate a ReadDriveOptions object is to statically import - * ReadDriveOptions.Builder.* and invoke a static creation method followed by an instance mutator - * (if needed): - *

- * - * import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.*; - * - * - * // this will get the first 1024 bytes starting at offset 2048 - * Payload payload = client.readDrive("drive-uuid",offset(2048l).size(1024l)); - * - * - * @author Adrian Cole - * - */ -public class ReadDriveOptions { - - private Long offset; - private Long size; - - /** - * start at the specified offset in bytes - */ - public ReadDriveOptions offset(long offset) { - checkArgument(offset >= 0, "start must be >= 0"); - this.offset = offset; - return this; - } - - /** - * download the specified size in bytes - */ - public ReadDriveOptions size(long size) { - checkArgument(size >= 0, "start must be >= 0"); - this.size = size; - return this; - } - - public static class Builder { - - /** - * @see ReadDriveOptions#offset - */ - public static ReadDriveOptions offset(long offset) { - ReadDriveOptions options = new ReadDriveOptions(); - return options.offset(offset); - } - - /** - * @see ReadDriveOptions#size - */ - public static ReadDriveOptions size(long size) { - ReadDriveOptions options = new ReadDriveOptions(); - return options.size(size); - } - - } - - public Long getOffset() { - return offset; - } - - public Long getSize() { - return size; - } -} diff --git a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/CommonElasticStackClientLiveTest.java b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/CommonElasticStackClientLiveTest.java index 40500e9fa3..48be0d32ca 100644 --- a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/CommonElasticStackClientLiveTest.java +++ b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/CommonElasticStackClientLiveTest.java @@ -246,21 +246,24 @@ public abstract class CommonElasticStackClientLiveTest httpRequest = processor.createRequest(method, "100"); - - assertRequestLineEquals(httpRequest, "GET https://api.elasticstack.com/drives/100/read HTTP/1.1"); - assertNonPayloadHeadersEqual(httpRequest, "Accept: application/octet-stream\n"); - assertPayloadEquals(httpRequest, null, null, false); - - assertResponseParserClassEquals(method, httpRequest, ReturnPayload.class); - assertSaxResponseParserClassEquals(method, null); - assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); - - checkFilters(httpRequest); - } - - public void testReadDriveOptions() throws SecurityException, NoSuchMethodException, IOException { - Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class, ReadDriveOptions.class); - GeneratedHttpRequest httpRequest = processor.createRequest(method, "100", - new ReadDriveOptions().offset(1024).size(2048)); + Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class, long.class, long.class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, "100", 1024, 2048); assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/read/1024/2048 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/octet-stream\n"); diff --git a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/ElasticStackClientLiveTest.java b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/ElasticStackClientLiveTest.java index 4863c9468d..ecd3b1276a 100644 --- a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/ElasticStackClientLiveTest.java +++ b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/ElasticStackClientLiveTest.java @@ -25,7 +25,6 @@ import java.io.IOException; import org.jclouds.elasticstack.domain.CreateDriveRequest; import org.jclouds.elasticstack.domain.DriveInfo; -import org.jclouds.elasticstack.options.ReadDriveOptions; import org.jclouds.io.Payloads; import org.jclouds.util.Utils; import org.testng.annotations.Test; @@ -44,8 +43,7 @@ public class ElasticStackClientLiveTest extends public void testWeCanReadAndWriteToDrive() throws IOException { drive2 = client.createDrive(new CreateDriveRequest.Builder().name(prefix + "2").size(1 * 1024 * 1024l).build()); client.writeDrive(drive2.getUuid(), Payloads.newStringPayload("foo")); - assertEquals(Utils.toStringAndClose(client - .readDrive(drive2.getUuid(), ReadDriveOptions.Builder.offset(0).size(3)).getInput()), "foo"); + assertEquals(Utils.toStringAndClose(client.readDrive(drive2.getUuid(), 0, 3).getInput()), "foo"); } @Test(dependsOnMethods = "testWeCanReadAndWriteToDrive") @@ -60,8 +58,7 @@ public class ElasticStackClientLiveTest extends assert driveNotClaimed.apply(drive2) : client.getDriveInfo(drive2.getUuid()); System.err.println("after image; drive 2" + client.getDriveInfo(drive2.getUuid())); System.err.println("after image; drive 3" + client.getDriveInfo(drive3.getUuid())); - assertEquals(Utils.toStringAndClose(client.readDrive(drive3.getUuid(), - ReadDriveOptions.Builder.offset(0).size(3)).getInput()), "foo"); + assertEquals(Utils.toStringAndClose(client.readDrive(drive3.getUuid(), 0, 3).getInput()), "foo"); } finally { client.destroyDrive(drive2.getUuid()); client.destroyDrive(drive3.getUuid()); diff --git a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPathTest.java b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPathTest.java deleted file mode 100644 index d7d9701b6c..0000000000 --- a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/binders/BindReadDriveOptionsToPathTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * - * Copyright (C) 2010 Cloud Conscious, LLC. - * - * ==================================================================== - * Licensed 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.elasticstack.binders; - -import static org.testng.Assert.assertEquals; - -import java.net.URI; - -import org.jclouds.elasticstack.options.ReadDriveOptions; -import org.jclouds.http.HttpRequest; -import org.jclouds.logging.config.NullLoggingModule; -import org.jclouds.rest.BaseRestClientTest.MockModule; -import org.jclouds.rest.config.RestModule; -import org.testng.annotations.Test; - -import com.google.inject.Guice; - -/** - * - * @author Adrian Cole - */ -@Test(groups = { "unit" }) -public class BindReadDriveOptionsToPathTest { - - private static final BindReadDriveOptionsToPath FN = Guice.createInjector(new RestModule(), new MockModule(), - new NullLoggingModule()).getInstance(BindReadDriveOptionsToPath.class); - - public void testSimple() { - HttpRequest request = new HttpRequest("POST", URI.create("https://drives/read")); - FN.bindToRequest(request, new ReadDriveOptions().offset(1024l).size(2048l)); - assertEquals(request.getEndpoint().getPath(), "/read/1024/2048"); - } - -} \ No newline at end of file diff --git a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/options/ReadDriveOptionsTest.java b/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/options/ReadDriveOptionsTest.java deleted file mode 100644 index 3e4e4a3a38..0000000000 --- a/sandbox/elasticstack/src/test/java/org/jclouds/elasticstack/options/ReadDriveOptionsTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/** - * - * Copyright (C) 2010 Cloud Conscious, LLC. - * - * ==================================================================== - * Licensed 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.elasticstack.options; - -import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.offset; -import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.size; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; - -import org.testng.annotations.Test; - -/** - * Tests possible uses of ReadDriveOptions and ReadDriveOptions.Builder.* - * - * @author Adrian Cole - */ -@Test(groups = "unit") -public class ReadDriveOptionsTest { - - @Test - public void testNullOffset() { - ReadDriveOptions options = new ReadDriveOptions(); - assertNull(options.getOffset()); - } - - @Test - public void testOffset() { - ReadDriveOptions options = new ReadDriveOptions().offset(1024); - assertEquals(options.getOffset(), new Long(1024)); - } - - @Test - public void testOffsetStatic() { - ReadDriveOptions options = offset(1024); - assertEquals(options.getOffset(), new Long(1024)); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void testOffsetNegative() { - offset(-1); - } - - @Test - public void testNullSize() { - ReadDriveOptions options = new ReadDriveOptions(); - assertNull(options.getSize()); - } - - @Test - public void testSize() { - ReadDriveOptions options = new ReadDriveOptions().size(1024); - assertEquals(options.getSize(), new Long(1024)); - } - - @Test - public void testSizeStatic() { - ReadDriveOptions options = size(1024); - assertEquals(options.getSize(), new Long(1024)); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void testSizeNegative() { - size(-1); - } - -}