mirror of https://github.com/apache/jclouds.git
JCLOUDS-410. Add support for ByteSource Payloads
Also deprecate byte[], File, InputSupplier<InputStream>, and String Payloads. Callers should instead provide a ByteSource via ByteSource.wrap(byte[]) and Files.asByteSource(File)
This commit is contained in:
parent
f3dcd3fe86
commit
e48d4985ea
|
@ -25,6 +25,7 @@ import java.util.Map;
|
|||
import org.jclouds.blobstore.domain.internal.BlobBuilderImpl;
|
||||
import org.jclouds.io.Payload;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.ImplementedBy;
|
||||
|
||||
/**
|
||||
|
@ -77,21 +78,32 @@ public interface BlobBuilder {
|
|||
*
|
||||
* @param payload
|
||||
* payload you wish to construct the {@link Blob} with.
|
||||
* @deprecated see payload(ByteSource.wrap(byte[])
|
||||
*/
|
||||
@Deprecated
|
||||
PayloadBlobBuilder payload(byte[] payload);
|
||||
|
||||
/**
|
||||
* @param payload payload you wish to construct the {@link Blob} with.
|
||||
*/
|
||||
PayloadBlobBuilder payload(ByteSource byteSource);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param payload
|
||||
* payload you wish to construct the {@link Blob} with.
|
||||
* @deprecated see payload(ByteSource.wrap(String.getBytes()))
|
||||
*/
|
||||
@Deprecated
|
||||
PayloadBlobBuilder payload(String payload);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param payload
|
||||
* payload you wish to construct the {@link Blob} with.
|
||||
* @deprecated see payload(Files.asByteSource(File))
|
||||
*/
|
||||
@Deprecated
|
||||
PayloadBlobBuilder payload(File payload);
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.jclouds.io.Payloads;
|
|||
import org.jclouds.io.payloads.PhantomPayload;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
|
@ -89,6 +90,11 @@ public class BlobBuilderImpl implements BlobBuilder {
|
|||
return payload(newPayload(checkNotNull(data, "data")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayloadBlobBuilder payload(ByteSource data) {
|
||||
return payload(newPayload(checkNotNull(data, "data")));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -161,6 +167,11 @@ public class BlobBuilderImpl implements BlobBuilder {
|
|||
return builder.payload(payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayloadBlobBuilder payload(ByteSource payload) {
|
||||
return builder.payload(payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayloadBlobBuilder payload(String payload) {
|
||||
return builder.payload(payload);
|
||||
|
|
|
@ -33,6 +33,7 @@ import com.google.common.base.Objects;
|
|||
import com.google.common.base.Objects.ToStringHelper;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
* Represents a request that can be executed within {@link HttpCommandExecutorService}
|
||||
|
@ -65,15 +66,28 @@ public class HttpMessage extends PayloadEnclosingImpl {
|
|||
|
||||
/**
|
||||
* @see HttpMessage#getPayload()
|
||||
* @deprecated see payload(ByteSource.wrap(byte[]))
|
||||
*/
|
||||
@Deprecated
|
||||
public T payload(byte [] payload) {
|
||||
this.payload = Payloads.newByteArrayPayload(checkNotNull(payload, "payload"));
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see HttpMessage#getPayload()
|
||||
*/
|
||||
@Deprecated
|
||||
public T payload(ByteSource payload) {
|
||||
this.payload = Payloads.newByteSourcePayload(checkNotNull(payload, "payload"));
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see HttpMessage#getPayload()
|
||||
* @deprecated see payload(Files.asByteSource(File))
|
||||
*/
|
||||
@Deprecated
|
||||
public T payload(File payload) {
|
||||
this.payload = Payloads.newFilePayload(checkNotNull(payload, "payload"));
|
||||
return self();
|
||||
|
@ -89,7 +103,9 @@ public class HttpMessage extends PayloadEnclosingImpl {
|
|||
|
||||
/**
|
||||
* @see HttpMessage#getPayload()
|
||||
* @deprecated see payload(ByteSource.wrap(String.getBytes()))
|
||||
*/
|
||||
@Deprecated
|
||||
public T payload(String payload) {
|
||||
this.payload = Payloads.newStringPayload(checkNotNull(payload, "payload"));
|
||||
return self();
|
||||
|
|
|
@ -64,6 +64,7 @@ import com.google.common.collect.ImmutableMultimap;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.ImmutableSet.Builder;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.reflect.Invokable;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
|
@ -161,7 +162,7 @@ public class HttpUtils {
|
|||
public static byte[] closeClientButKeepContentStream(PayloadEnclosing response) {
|
||||
byte[] returnVal = toByteArrayOrNull(response);
|
||||
if (returnVal != null && !response.getPayload().isRepeatable()) {
|
||||
Payload newPayload = Payloads.newByteArrayPayload(returnVal);
|
||||
Payload newPayload = Payloads.newByteSourcePayload(ByteSource.wrap(returnVal));
|
||||
MutableContentMetadata fromMd = response.getPayload().getContentMetadata();
|
||||
MutableContentMetadata toMd = newPayload.getContentMetadata();
|
||||
copy(fromMd, toMd);
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
|
||||
import org.jclouds.io.payloads.ByteArrayPayload;
|
||||
import org.jclouds.io.payloads.ByteSourcePayload;
|
||||
import org.jclouds.io.payloads.FilePayload;
|
||||
import org.jclouds.io.payloads.InputStreamPayload;
|
||||
import org.jclouds.io.payloads.StringPayload;
|
||||
|
@ -31,6 +32,7 @@ import org.jclouds.io.payloads.UrlEncodedFormPayload;
|
|||
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -40,6 +42,10 @@ public class Payloads {
|
|||
private Payloads() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated see newPayload(ByteSource) or newPayload(InputStream)
|
||||
*/
|
||||
@Deprecated
|
||||
public static Payload newPayload(Object data) {
|
||||
checkNotNull(data, "data");
|
||||
if (data instanceof Payload) {
|
||||
|
@ -48,6 +54,8 @@ public class Payloads {
|
|||
return newInputStreamPayload((InputStream) data);
|
||||
} else if (data instanceof byte[]) {
|
||||
return newByteArrayPayload((byte[]) data);
|
||||
} else if (data instanceof ByteSource) {
|
||||
return newByteSourcePayload((ByteSource) data);
|
||||
} else if (data instanceof String) {
|
||||
return newStringPayload((String) data);
|
||||
} else if (data instanceof File) {
|
||||
|
@ -61,14 +69,30 @@ public class Payloads {
|
|||
return new InputStreamPayload(checkNotNull(data, "data"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated see newPayload(ByteSource)
|
||||
*/
|
||||
@Deprecated
|
||||
public static ByteArrayPayload newByteArrayPayload(byte[] data) {
|
||||
return new ByteArrayPayload(checkNotNull(data, "data"));
|
||||
}
|
||||
|
||||
public static ByteSourcePayload newByteSourcePayload(ByteSource data) {
|
||||
return new ByteSourcePayload(checkNotNull(data, "data"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated see newPayload(ByteSource)
|
||||
*/
|
||||
@Deprecated
|
||||
public static StringPayload newStringPayload(String data) {
|
||||
return new StringPayload(checkNotNull(data, "data"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated see newPayload(ByteSource)
|
||||
*/
|
||||
@Deprecated
|
||||
public static FilePayload newFilePayload(File data) {
|
||||
return new FilePayload(checkNotNull(data, "data"));
|
||||
}
|
||||
|
|
|
@ -24,7 +24,9 @@ import java.io.InputStream;
|
|||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
* @deprecated see ByteSourcePayload
|
||||
*/
|
||||
@Deprecated
|
||||
public class ByteArrayPayload extends BasePayload<byte[]> {
|
||||
public ByteArrayPayload(byte[] content) {
|
||||
this(content, null);
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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.io.payloads;
|
||||
|
||||
import static com.google.common.io.Closeables.closeQuietly;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.Closer;
|
||||
|
||||
/**
|
||||
* A repeatable, ByteSource-backed Payload.
|
||||
*
|
||||
* @author Andrew Gaul
|
||||
*/
|
||||
public class ByteSourcePayload extends BasePayload<ByteSource> {
|
||||
private final Closer closer = Closer.create();
|
||||
|
||||
public ByteSourcePayload(ByteSource content) {
|
||||
super(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openStream() throws IOException {
|
||||
return closer.register(content.openStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRepeatable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* if we created the stream, then it is already consumed on close.
|
||||
*/
|
||||
@Override
|
||||
public void release() {
|
||||
closeQuietly(closer);
|
||||
}
|
||||
}
|
|
@ -26,7 +26,9 @@ import java.io.IOException;
|
|||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
* @deprecated see ByteSourcePayload
|
||||
*/
|
||||
@Deprecated
|
||||
public class FilePayload extends BasePayload<File> {
|
||||
|
||||
public FilePayload(File content) {
|
||||
|
|
|
@ -20,16 +20,17 @@ import static com.google.common.io.Closeables.closeQuietly;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.io.Closer;
|
||||
import com.google.common.io.InputSupplier;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
* @deprecated see ByteSourcePayload
|
||||
*/
|
||||
@Deprecated
|
||||
public class InputStreamSupplierPayload extends BasePayload<InputSupplier<? extends InputStream>> {
|
||||
private List<InputStream> toClose = Lists.newArrayList();
|
||||
private final Closer closer = Closer.create();
|
||||
|
||||
public InputStreamSupplierPayload(InputSupplier<? extends InputStream> content) {
|
||||
super(content);
|
||||
|
@ -40,9 +41,7 @@ public class InputStreamSupplierPayload extends BasePayload<InputSupplier<? exte
|
|||
*/
|
||||
@Override
|
||||
public InputStream openStream() throws IOException {
|
||||
InputStream returnVal = content.getInput();
|
||||
toClose.add(returnVal);
|
||||
return returnVal;
|
||||
return closer.register(content.getInput());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,9 +57,6 @@ public class InputStreamSupplierPayload extends BasePayload<InputSupplier<? exte
|
|||
*/
|
||||
@Override
|
||||
public void release() {
|
||||
if (toClose.size() > 0)
|
||||
for (InputStream content = toClose.remove(0); toClose.size() > 0; content = toClose.remove(0))
|
||||
closeQuietly(content);
|
||||
closeQuietly(closer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,9 @@ import com.google.common.base.Charsets;
|
|||
* a different encoding, please use {@link ByteArrayPayload}.
|
||||
*
|
||||
* @author Adrian Cole
|
||||
* @deprecated see ByteSourcePayload
|
||||
*/
|
||||
@Deprecated
|
||||
public class StringPayload extends BasePayload<String> {
|
||||
|
||||
private final byte[] bytes;
|
||||
|
|
Loading…
Reference in New Issue