From 02203afb07c9037009a4c2ee59f6eb53d4254b23 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 16 Jul 2012 22:00:11 -0700 Subject: [PATCH] Harmonize filesystem and transient putBlob --- .../filesystem/FilesystemAsyncBlobStore.java | 23 +++++++++++-------- .../strategy/FilesystemStorageStrategy.java | 8 +++---- .../FilesystemStorageStrategyImpl.java | 16 ++++--------- .../FilesystemAsyncBlobStoreTest.java | 1 + .../FilesystemStorageStrategyImplTest.java | 4 +++- .../blobstore/TransientAsyncBlobStore.java | 21 ++++++++++++++++- 6 files changed, 47 insertions(+), 26 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 81b93f980f..aad185533a 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/FilesystemAsyncBlobStore.java @@ -457,17 +457,22 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { String blobKey = blob.getMetadata().getName(); logger.debug("Put blob with key [%s] to container [%s]", blobKey, containerName); - String eTag = getEtag(blob); + if (!storageStrategy.containerExists(containerName)) { + return Futures.immediateFailedFuture(new IllegalStateException("containerName not found: " + containerName)); + } + try { // TODO // must override existing file? - storageStrategy.writePayloadOnFile(containerName, blobKey, blob.getPayload()); + storageStrategy.putBlob(containerName, blob); } catch (IOException e) { logger.error(e, "An error occurred storing the new blob with name [%s] to container [%s].", blobKey, containerName); Throwables.propagate(e); } + + String eTag = getEtag(blob); return immediateFuture(eTag); } @@ -591,13 +596,6 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { } } - private Blob copyBlob(Blob blob) { - Blob returnVal = blobFactory.create(copy(blob.getMetadata())); - returnVal.setPayload(blob.getPayload()); - copyPayloadHeadersToBlob(blob.getPayload(), returnVal); - return returnVal; - } - /** * Calculates the object MD5 and returns it as eTag * @@ -616,6 +614,13 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore { return eTag; } + private Blob copyBlob(Blob blob) { + Blob returnVal = blobFactory.create(copy(blob.getMetadata())); + returnVal.setPayload(blob.getPayload()); + copyPayloadHeadersToBlob(blob.getPayload(), returnVal); + return returnVal; + } + @Override protected boolean deleteAndVerifyContainerGone(final String container) { storageStrategy.deleteContainer(container); diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java index a0c8662b63..fc648f1092 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/FilesystemStorageStrategy.java @@ -169,11 +169,11 @@ public interface FilesystemStorageStrategy { void removeBlob(String container, String key); /** - * Write a {@link Blob} {@link Payload} into a file - * @param fileName - * @param payload + * Write a {@link Blob} into a file + * @param container + * @param blob * @throws IOException */ - void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException; + void putBlob(String containerName, Blob blob) throws IOException; } diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java index b3ea8bf275..918f9d21c2 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java @@ -204,19 +204,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy return blobFile; } - /** - * Write a {@link Blob} {@link Payload} into a file - * - * @param container - * @param blobKey - * @param payload - * @throws IOException - */ @Override - public void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException { - filesystemContainerNameValidator.validate(container); + public void putBlob(final String containerName, final Blob blob) throws IOException { + String blobKey = blob.getMetadata().getName(); + Payload payload = blob.getPayload(); + filesystemContainerNameValidator.validate(containerName); filesystemBlobKeyValidator.validate(blobKey); - File outputFile = getFileForBlobKey(container, blobKey); + File outputFile = getFileForBlobKey(containerName, blobKey); FileOutputStream output = null; try { Files.createParentDirs(outputFile); diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java index c8d3b0d844..aa6bbabcc0 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/FilesystemAsyncBlobStoreTest.java @@ -740,6 +740,7 @@ public class FilesystemAsyncBlobStoreTest { * can't be deleted. See http://code.google.com/p/jclouds/issues/detail?id=737 */ final String containerName = "containerWithRanges"; + blobStore.createContainerInLocation(null, containerName); String payload = "abcdefgh"; InputStream is; Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(payload)).build(); diff --git a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java index dd248ee802..69c6ce1a05 100644 --- a/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java +++ b/apis/filesystem/src/test/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImplTest.java @@ -371,8 +371,10 @@ public class FilesystemStorageStrategyImplTest { blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img"); sourceFile = TestUtils.getImageForBlobPayload(); filePayload = new FilePayload(sourceFile); + Blob blob = storageStrategy.newBlob(blobKey); + blob.setPayload(filePayload); // write files - storageStrategy.writePayloadOnFile(CONTAINER_NAME, blobKey, filePayload); + storageStrategy.putBlob(CONTAINER_NAME, blob); // verify that the files is equal File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); InputSupplier expectedInput = diff --git a/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java b/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java index e8fcd51b1b..430c205866 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/TransientAsyncBlobStore.java @@ -472,7 +472,8 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore { storageStrategy.putBlob(containerName, blob); - return immediateFuture(Iterables.getOnlyElement(blob.getAllHeaders().get(HttpHeaders.ETAG))); + String eTag = getEtag(blob); + return immediateFuture(eTag); } private Blob createUpdatedCopyOfBlobInContainer(String containerName, Blob in) { @@ -635,6 +636,24 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore { } } + /** + * Calculates the object MD5 and returns it as eTag + * + * @param object + * @return + */ + private String getEtag(Blob object) { + try { + Payloads.calculateMD5(object, crypto.md5()); + } catch (IOException ex) { + logger.error(ex, "An error occurred calculating MD5 for object with name %s.", object.getMetadata().getName()); + Throwables.propagate(ex); + } + + String eTag = CryptoStreams.hex(object.getPayload().getContentMetadata().getContentMD5()); + return eTag; + } + private Blob copyBlob(Blob blob) { Blob returnVal = blobFactory.create(copy(blob.getMetadata())); returnVal.setPayload(blob.getPayload());