Move copy(MutableBlobMetadata) to BlobStoreUtils

This commit is contained in:
Andrew Gaul 2012-07-22 17:28:10 -07:00 committed by Andrew Gaul
parent b517bc79c7
commit 0bc5c69a4f
4 changed files with 53 additions and 88 deletions

View File

@ -26,24 +26,16 @@ import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.size;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.filter;
import static com.google.common.collect.Sets.newTreeSet;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.ExecutorService;
@ -75,6 +67,7 @@ import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.options.PutOptions;
import org.jclouds.blobstore.strategy.IfDirectoryReturnNameStrategy;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.blobstore.util.BlobUtils;
import org.jclouds.collect.Memoized;
import org.jclouds.crypto.Crypto;
@ -91,7 +84,6 @@ import org.jclouds.io.ContentMetadata;
import org.jclouds.io.ContentMetadataCodec;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.BaseMutableContentMetadata;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.ParamValidators;
@ -166,7 +158,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
checkState(oldBlob != null, "blob " + key + " is not present although it was in the list of "
+ container);
checkState(oldBlob.getMetadata() != null, "blob " + container + "/" + key + " has no metadata");
MutableBlobMetadata md = copy(oldBlob.getMetadata());
MutableBlobMetadata md = BlobStoreUtils.copy(oldBlob.getMetadata());
String directoryName = ifDirectoryReturnName.execute(md);
if (directoryName != null) {
md.setName(directoryName);
@ -245,37 +237,6 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
storageStrategy.getAllContainerNames()));
}
public static MutableBlobMetadata copy(MutableBlobMetadata in) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutput os;
try {
os = new ObjectOutputStream(bout);
os.writeObject(in);
ObjectInput is = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
MutableBlobMetadata metadata = (MutableBlobMetadata) is.readObject();
convertUserMetadataKeysToLowercase(metadata);
metadata.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(in.getContentMetadata().toBuilder()
.build()));
return metadata;
} catch (Exception e) {
throw propagate(e);
}
}
private static void convertUserMetadataKeysToLowercase(MutableBlobMetadata metadata) {
Map<String, String> lowerCaseUserMetadata = newHashMap();
for (Entry<String, String> entry : metadata.getUserMetadata().entrySet()) {
lowerCaseUserMetadata.put(entry.getKey().toLowerCase(), entry.getValue());
}
metadata.setUserMetadata(lowerCaseUserMetadata);
}
public static MutableBlobMetadata copy(MutableBlobMetadata in, String newKey) {
MutableBlobMetadata newMd = copy(in);
newMd.setName(newKey);
return newMd;
}
/**
* {@inheritDoc}
*/
@ -581,7 +542,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
public ListenableFuture<BlobMetadata> blobMetadata(final String container, final String key) {
try {
Blob blob = getBlob(container, key).get();
return immediateFuture(blob != null ? (BlobMetadata) copy(blob.getMetadata()) : null);
return immediateFuture(blob != null ? (BlobMetadata) BlobStoreUtils.copy(blob.getMetadata()) : null);
} catch (Exception e) {
if (size(filter(getCausalChain(e), KeyNotFoundException.class)) >= 1)
return immediateFuture(null);
@ -608,7 +569,7 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
}
private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
Blob returnVal = blobFactory.create(BlobStoreUtils.copy(blob.getMetadata()));
returnVal.setPayload(blob.getPayload());
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
return returnVal;

View File

@ -43,6 +43,7 @@ import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.functions.HttpGetOptionsListToGetOptions;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.concurrent.Futures;
import org.jclouds.date.DateService;
import org.jclouds.domain.Location;
@ -175,7 +176,7 @@ public class StubS3AsyncClient implements S3AsyncClient {
return immediateFailedFuture(TransientAsyncBlobStore.returnResponseException(412));
}
Blob sourceS3 = source.get(sourceObject);
MutableBlobMetadata newMd = TransientAsyncBlobStore.copy(sourceS3.getMetadata(), destinationObject);
MutableBlobMetadata newMd = BlobStoreUtils.copy(sourceS3.getMetadata(), destinationObject);
if (options.getAcl() != null)
keyToAcl.put(destinationBucket + "/" + destinationObject, options.getAcl());
@ -183,7 +184,7 @@ public class StubS3AsyncClient implements S3AsyncClient {
Blob newBlob = blobProvider.create(newMd);
newBlob.setPayload(sourceS3.getPayload());
dest.put(destinationObject, newBlob);
return immediateFuture((ObjectMetadata) blob2ObjectMetadata.apply(TransientAsyncBlobStore.copy(newMd)));
return immediateFuture((ObjectMetadata) blob2ObjectMetadata.apply(BlobStoreUtils.copy(newMd)));
}
return immediateFailedFuture(new KeyNotFoundException(sourceBucket, sourceObject, sourceBucket + "/"
+ sourceObject));

View File

@ -26,24 +26,16 @@ import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.size;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.filter;
import static com.google.common.collect.Sets.newTreeSet;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.ExecutorService;
@ -72,6 +64,7 @@ import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.options.PutOptions;
import org.jclouds.blobstore.strategy.IfDirectoryReturnNameStrategy;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.blobstore.util.BlobUtils;
import org.jclouds.collect.Memoized;
import org.jclouds.crypto.Crypto;
@ -167,7 +160,7 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
checkState(oldBlob != null, "blob " + key + " is not present although it was in the list of "
+ container);
checkState(oldBlob.getMetadata() != null, "blob " + container + "/" + key + " has no metadata");
MutableBlobMetadata md = copy(oldBlob.getMetadata());
MutableBlobMetadata md = BlobStoreUtils.copy(oldBlob.getMetadata());
String directoryName = ifDirectoryReturnName.execute(md);
if (directoryName != null) {
md.setName(directoryName);
@ -246,36 +239,6 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
storageStrategy.getAllContainerNames()));
}
public static MutableBlobMetadata copy(MutableBlobMetadata in) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutput os;
try {
os = new ObjectOutputStream(bout);
os.writeObject(in);
ObjectInput is = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
MutableBlobMetadata metadata = (MutableBlobMetadata) is.readObject();
convertUserMetadataKeysToLowercase(metadata);
HttpUtils.copy(in.getContentMetadata(), metadata.getContentMetadata());
return metadata;
} catch (Exception e) {
throw propagate(e);
}
}
private static void convertUserMetadataKeysToLowercase(MutableBlobMetadata metadata) {
Map<String, String> lowerCaseUserMetadata = newHashMap();
for (Entry<String, String> entry : metadata.getUserMetadata().entrySet()) {
lowerCaseUserMetadata.put(entry.getKey().toLowerCase(), entry.getValue());
}
metadata.setUserMetadata(lowerCaseUserMetadata);
}
public static MutableBlobMetadata copy(MutableBlobMetadata in, String newKey) {
MutableBlobMetadata newMd = copy(in);
newMd.setName(newKey);
return newMd;
}
/**
* {@inheritDoc}
*/
@ -513,7 +476,7 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
} catch (IOException e) {
Throwables.propagate(e);
}
Blob blob = blobFactory.create(copy(in.getMetadata()));
Blob blob = blobFactory.create(BlobStoreUtils.copy(in.getMetadata()));
blob.setPayload(payload);
blob.getMetadata().setContainer(containerName);
blob.getMetadata().setUri(
@ -642,7 +605,7 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
public ListenableFuture<BlobMetadata> blobMetadata(final String container, final String key) {
try {
Blob blob = getBlob(container, key).get();
return immediateFuture(blob != null ? (BlobMetadata) copy(blob.getMetadata()) : null);
return immediateFuture(blob != null ? (BlobMetadata) BlobStoreUtils.copy(blob.getMetadata()) : null);
} catch (Exception e) {
if (size(filter(getCausalChain(e), KeyNotFoundException.class)) >= 1)
return immediateFuture(null);
@ -669,7 +632,7 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
}
private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
Blob returnVal = blobFactory.create(BlobStoreUtils.copy(blob.getMetadata()));
returnVal.setPayload(blob.getPayload());
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
return returnVal;

View File

@ -20,8 +20,15 @@ package org.jclouds.blobstore.util;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -32,6 +39,7 @@ import org.jclouds.blobstore.ContainerNotFoundException;
import org.jclouds.blobstore.KeyNotFoundException;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.functions.BlobName;
import org.jclouds.functions.ExceptionToValueOrPropagate;
@ -41,6 +49,8 @@ import org.jclouds.http.HttpUtils;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.util.Strings2;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@ -159,4 +169,34 @@ public class BlobStoreUtils {
public static Iterable<String> getSupportedProviders() {
return org.jclouds.rest.Providers.getSupportedProvidersOfType(TypeToken.of(BlobStoreContext.class));
}
}
public static MutableBlobMetadata copy(MutableBlobMetadata in) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutput os;
try {
os = new ObjectOutputStream(bout);
os.writeObject(in);
ObjectInput is = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
MutableBlobMetadata metadata = (MutableBlobMetadata) is.readObject();
convertUserMetadataKeysToLowercase(metadata);
HttpUtils.copy(in.getContentMetadata(), metadata.getContentMetadata());
return metadata;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
public static MutableBlobMetadata copy(MutableBlobMetadata in, String newKey) {
MutableBlobMetadata newMd = BlobStoreUtils.copy(in);
newMd.setName(newKey);
return newMd;
}
private static void convertUserMetadataKeysToLowercase(MutableBlobMetadata metadata) {
Map<String, String> lowerCaseUserMetadata = Maps.newHashMap();
for (Map.Entry<String, String> entry : metadata.getUserMetadata().entrySet()) {
lowerCaseUserMetadata.put(entry.getKey().toLowerCase(), entry.getValue());
}
metadata.setUserMetadata(lowerCaseUserMetadata);
}
}