mirror of https://github.com/apache/jclouds.git
Issue 301: added Closeable to payload so that it works better in clojure
This commit is contained in:
parent
541ccdae51
commit
1274a25e17
|
@ -0,0 +1,41 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* 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"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.http;
|
package org.jclouds.http;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
@ -29,7 +30,7 @@ import com.google.common.io.InputSupplier;
|
||||||
/**
|
/**
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
public interface Payload extends InputSupplier<InputStream> {
|
public interface Payload extends InputSupplier<InputStream>, Closeable{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new InputStream object of the payload.
|
* Creates a new InputStream object of the payload.
|
||||||
|
|
|
@ -41,10 +41,11 @@ public abstract class BasePayload<V> implements Payload {
|
||||||
protected byte[] contentMD5;
|
protected byte[] contentMD5;
|
||||||
protected transient volatile boolean written;
|
protected transient volatile boolean written;
|
||||||
|
|
||||||
protected BasePayload(V content, @Nullable String contentType, @Nullable Long contentLength,
|
protected BasePayload(V content, @Nullable String contentType,
|
||||||
@Nullable byte[] contentMD5) {
|
@Nullable Long contentLength, @Nullable byte[] contentMD5) {
|
||||||
this.content = checkNotNull(content, "content");
|
this.content = checkNotNull(content, "content");
|
||||||
this.contentType = contentType == null ? "application/unknown" : contentType;
|
this.contentType = contentType == null ? "application/unknown"
|
||||||
|
: contentType;
|
||||||
this.contentLength = contentLength;
|
this.contentLength = contentLength;
|
||||||
if (contentMD5 != null)
|
if (contentMD5 != null)
|
||||||
setContentMD5(contentMD5);
|
setContentMD5(contentMD5);
|
||||||
|
@ -121,7 +122,8 @@ public abstract class BasePayload<V> implements Payload {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void writeTo(OutputStream outstream) throws IOException {
|
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;
|
written = true;
|
||||||
InputStream in = getInput();
|
InputStream in = getInput();
|
||||||
try {
|
try {
|
||||||
|
@ -158,9 +160,9 @@ public abstract class BasePayload<V> implements Payload {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[content=" + (content != null) + ", contentLength=" + contentLength + ", contentMD5="
|
return "[content=" + (content != null) + ", contentLength="
|
||||||
+ (contentMD5 != null) + ", contentType=" + contentType + ", written=" + written
|
+ contentLength + ", contentMD5=" + (contentMD5 != null)
|
||||||
+ "]";
|
+ ", contentType=" + contentType + ", written=" + written + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -178,4 +180,11 @@ public abstract class BasePayload<V> implements Payload {
|
||||||
public void release() {
|
public void release() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delegates to release()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
release();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -143,4 +143,9 @@ public class DelegatingPayload implements Payload {
|
||||||
public void release() {
|
public void release() {
|
||||||
delegate.release();
|
delegate.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
delegate.close();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue