From 1274a25e1708e9eb2e0097cbfa48c11aad0fd352 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 13 Jul 2010 18:27:45 -0500 Subject: [PATCH] Issue 301: added Closeable to payload so that it works better in clojure --- .../functions/BlobToHttpGetOptionsTest.java | 41 +++++++++++++++++++ .../main/java/org/jclouds/http/Payload.java | 3 +- .../jclouds/http/payloads/BasePayload.java | 23 +++++++---- .../http/payloads/DelegatingPayload.java | 5 +++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 aws/core/src/test/java/org/jclouds/aws/s3/blobstore/functions/BlobToHttpGetOptionsTest.java diff --git a/aws/core/src/test/java/org/jclouds/aws/s3/blobstore/functions/BlobToHttpGetOptionsTest.java b/aws/core/src/test/java/org/jclouds/aws/s3/blobstore/functions/BlobToHttpGetOptionsTest.java new file mode 100644 index 0000000000..ab56342f17 --- /dev/null +++ b/aws/core/src/test/java/org/jclouds/aws/s3/blobstore/functions/BlobToHttpGetOptionsTest.java @@ -0,0 +1,41 @@ +/** + * + * Copyright (C) 2009 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.aws.s3.blobstore.functions; +import static org.testng.Assert.assertEquals; + +import org.jclouds.blobstore.functions.BlobToHttpGetOptions; +import org.jclouds.http.options.GetOptions; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +@Test(groups = "unit", testName = "s3.BlobToHttpGetOptionsTest") +public class BlobToHttpGetOptionsTest { + + @Test + void testOneRange() { + BlobToHttpGetOptions converter = new BlobToHttpGetOptions(); + org.jclouds.blobstore.options.GetOptions blobGet = new org.jclouds.blobstore.options.GetOptions() + .range(2, 5); + GetOptions httpGet = converter.apply(blobGet); + assertEquals(httpGet.buildRequestHeaders().get("Range"), ImmutableSet + .of("bytes=2-5")); + + } +} \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/http/Payload.java b/core/src/main/java/org/jclouds/http/Payload.java index 9eb2cc6d55..d7014109f4 100644 --- a/core/src/main/java/org/jclouds/http/Payload.java +++ b/core/src/main/java/org/jclouds/http/Payload.java @@ -18,6 +18,7 @@ */ package org.jclouds.http; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -29,7 +30,7 @@ import com.google.common.io.InputSupplier; /** * @author Adrian Cole */ -public interface Payload extends InputSupplier { +public interface Payload extends InputSupplier, Closeable{ /** * Creates a new InputStream object of the payload. diff --git a/core/src/main/java/org/jclouds/http/payloads/BasePayload.java b/core/src/main/java/org/jclouds/http/payloads/BasePayload.java index 1dd8b47913..ca1c07a071 100644 --- a/core/src/main/java/org/jclouds/http/payloads/BasePayload.java +++ b/core/src/main/java/org/jclouds/http/payloads/BasePayload.java @@ -41,10 +41,11 @@ public abstract class BasePayload implements Payload { protected byte[] contentMD5; protected transient volatile boolean written; - protected BasePayload(V content, @Nullable String contentType, @Nullable Long contentLength, - @Nullable byte[] contentMD5) { + protected BasePayload(V content, @Nullable String contentType, + @Nullable Long contentLength, @Nullable byte[] contentMD5) { this.content = checkNotNull(content, "content"); - this.contentType = contentType == null ? "application/unknown" : contentType; + this.contentType = contentType == null ? "application/unknown" + : contentType; this.contentLength = contentLength; if (contentMD5 != null) setContentMD5(contentMD5); @@ -121,7 +122,8 @@ public abstract class BasePayload implements Payload { */ @Override public void writeTo(OutputStream outstream) throws IOException { - checkState(!written || isRepeatable(), "can only be writted to an outputstream once"); + checkState(!written || isRepeatable(), + "can only be writted to an outputstream once"); written = true; InputStream in = getInput(); try { @@ -158,9 +160,9 @@ public abstract class BasePayload implements Payload { @Override public String toString() { - return "[content=" + (content != null) + ", contentLength=" + contentLength + ", contentMD5=" - + (contentMD5 != null) + ", contentType=" + contentType + ", written=" + written - + "]"; + return "[content=" + (content != null) + ", contentLength=" + + contentLength + ", contentMD5=" + (contentMD5 != null) + + ", contentType=" + contentType + ", written=" + written + "]"; } /** @@ -178,4 +180,11 @@ public abstract class BasePayload implements Payload { public void release() { } + /** + * Delegates to release() + */ + @Override + public void close() { + release(); + } } \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/http/payloads/DelegatingPayload.java b/core/src/main/java/org/jclouds/http/payloads/DelegatingPayload.java index 53b67339df..4d1c9e698e 100644 --- a/core/src/main/java/org/jclouds/http/payloads/DelegatingPayload.java +++ b/core/src/main/java/org/jclouds/http/payloads/DelegatingPayload.java @@ -143,4 +143,9 @@ public class DelegatingPayload implements Payload { public void release() { delegate.release(); } + + @Override + public void close() throws IOException { + delegate.close(); + } } \ No newline at end of file