From 3459df3653326213eac0226b6f1f5f92bfeb180d Mon Sep 17 00:00:00 2001 From: David Santiago Date: Sat, 2 Apr 2011 02:26:45 -0500 Subject: [PATCH] Additional refinements to blobstore.clj updates. * Added an option to conditionally call calculateMD5() in blob2. * Fixed a bug in md5-blob (which predated these changes). * Added a direct test for blob2 and calculateMD5. --- .../src/main/clojure/org/jclouds/blobstore.clj | 17 +++++++++++------ .../test/clojure/org/jclouds/blobstore_test.clj | 7 +++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/blobstore/src/main/clojure/org/jclouds/blobstore.clj b/blobstore/src/main/clojure/org/jclouds/blobstore.clj index 6cf7d3b1af..91bc83f417 100644 --- a/blobstore/src/main/clojure/org/jclouds/blobstore.clj +++ b/blobstore/src/main/clojure/org/jclouds/blobstore.clj @@ -380,19 +380,24 @@ example: ([^String name option-map] (blob2 name option-map *blobstore*)) ([^String name - {:keys [payload content-type content-length content-md5 - content-disposition content-encoding content-language metadata] - :or {for-signing false}} + {:keys [payload content-type content-length content-md5 calculate-md5 + content-disposition content-encoding content-language metadata]} ^BlobStore blobstore] + {:pre [(not (and content-md5 calculate-md5)) + (not (and (nil? payload) calculate-md5))]} (let [blob-builder (if payload (.payload (.blobBuilder blobstore name) payload) (.forSigning (.blobBuilder blobstore name))) blob-builder (if content-length ;; Special case, arg is prim. (.contentLength blob-builder content-length) - blob-builder)] + blob-builder) + blob-builder (if calculate-md5 ;; Only do calculateMD5 OR contentMD5. + (.calculateMD5 blob-builder) + (if content-md5 + (.contentMD5 blob-builder content-md5) + blob-builder))] (doto blob-builder (.contentType content-type) - (.contentMD5 content-md5) (.contentDisposition content-disposition) (.contentEncoding content-encoding) (.contentLanguage content-language) @@ -405,7 +410,7 @@ note that this implies rebuffering, if the blob's payload isn't repeatable" ([#^Blob blob] (Payloads/calculateMD5 blob)) ([#^String name payload] - (blob2 name {:payload payload} *blobstore*)) + (md5-blob name payload *blobstore*)) ([#^String name payload #^BlobStore blobstore] (md5-blob (blob2 name {:payload payload} blobstore)))) diff --git a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj index 9c571f88b8..1a347a7d1c 100644 --- a/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj +++ b/blobstore/src/test/clojure/org/jclouds/blobstore_test.clj @@ -21,6 +21,7 @@ (:use [org.jclouds.blobstore] :reload-all) (:use [clojure.test]) (:import [org.jclouds.blobstore BlobStoreContextFactory] + [org.jclouds.crypto CryptoStreams] [java.io ByteArrayOutputStream] [org.jclouds.util Strings2])) @@ -180,6 +181,12 @@ (is (= "http://localhost/container/path" (str (.getEndpoint request)))) (is (= "DELETE" (.getMethod request))))) +(deftest blob2-test + (let [a-blob (blob2 "test-name" {:payload (.getBytes "test-payload") + :calculate-md5 true})] + (is (= (seq (.. a-blob (getPayload) (getContentMetadata) (getContentMD5))) + (seq (CryptoStreams/md5 (.getBytes "test-payload"))))))) + ;; TODO: more tests involving blob-specific functions (deftest corruption-hunt