From 0bc5c69a4ff2ab42b5ab07bf9c57d1fd3d44fb79 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 22 Jul 2012 17:28:10 -0700 Subject: [PATCH] Move copy(MutableBlobMetadata) to BlobStoreUtils --- .../filesystem/FilesystemAsyncBlobStore.java | 47 ++----------------- .../s3/internal/StubS3AsyncClient.java | 5 +- .../blobstore/TransientAsyncBlobStore.java | 47 ++----------------- .../blobstore/util/BlobStoreUtils.java | 42 ++++++++++++++++- 4 files changed, 53 insertions(+), 88 deletions(-) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java index 1b5a50b8e3..7f37ff977b 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java @@ -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 lowerCaseUserMetadata = newHashMap(); - for (Entry 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(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; diff --git a/apis/s3/src/test/java/org/jclouds/s3/internal/StubS3AsyncClient.java b/apis/s3/src/test/java/org/jclouds/s3/internal/StubS3AsyncClient.java index 1f80a5e04c..566380ed23 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/internal/StubS3AsyncClient.java +++ b/apis/s3/src/test/java/org/jclouds/s3/internal/StubS3AsyncClient.java @@ -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)); diff --git a/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java b/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java index 70f52fd522..ca11cc5f99 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java @@ -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 lowerCaseUserMetadata = newHashMap(); - for (Entry 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(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; diff --git a/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java b/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java index 15e3554864..bc1bd98f41 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/util/BlobStoreUtils.java @@ -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 getSupportedProviders() { return org.jclouds.rest.Providers.getSupportedProvidersOfType(TypeToken.of(BlobStoreContext.class)); } -} \ No newline at end of file + + 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 lowerCaseUserMetadata = Maps.newHashMap(); + for (Map.Entry entry : metadata.getUserMetadata().entrySet()) { + lowerCaseUserMetadata.put(entry.getKey().toLowerCase(), entry.getValue()); + } + metadata.setUserMetadata(lowerCaseUserMetadata); + } +}