Added sign-blob-request method, and some tests (broken)

This commit is contained in:
Hugo Duncan 2010-09-13 23:30:13 -04:00
parent f068a06354
commit 754a511ec2
2 changed files with 46 additions and 8 deletions

View File

@ -44,11 +44,11 @@ See http://code.google.com/p/jclouds for details."
AsyncBlobStore BlobStore BlobStoreContext BlobStoreContextFactory AsyncBlobStore BlobStore BlobStoreContext BlobStoreContextFactory
domain.BlobMetadata domain.StorageMetadata domain.Blob domain.BlobMetadata domain.StorageMetadata domain.Blob
options.ListContainerOptions] options.ListContainerOptions]
[org.jclouds.io Payloads] org.jclouds.io.Payloads
[org.jclouds.io.payloads PhantomPayload] org.jclouds.io.payloads.PhantomPayload
[java.util Arrays] java.util.Arrays
[java.security DigestOutputStream MessageDigest] [java.security DigestOutputStream MessageDigest]
[com.google.common.collect ImmutableSet])) com.google.common.collect.ImmutableSet))
(try (try
(require '[clojure.contrib.io :as io]) (require '[clojure.contrib.io :as io])
@ -240,12 +240,12 @@ Options can also be specified for extension modules
(.getBlob blobstore container-name path))) (.getBlob blobstore container-name path)))
(defn sign-get-blob-request (defn sign-get-blob-request
"Get a signed http request for a blob, so that you can retrieve it "Get a signed http request for a blob, so that you can retrieve it
in another application. ex. curl" in another application. ex. curl"
([container-name path] ([container-name path]
(sign-get-blob-request container-name path *blobstore*)) (sign-get-blob-request container-name path *blobstore*))
([container-name path #^BlobStore blobstore] ([container-name path #^BlobStore blobstore]
(.signGetBlob (.getContext blobstore) container-name path))) (.signGetBlob (.. blobstore getContext getSigner) container-name path)))
(defn sign-remove-blob-request (defn sign-remove-blob-request
"Get a signed http request for deleting a blob in another application. "Get a signed http request for deleting a blob in another application.
@ -253,7 +253,7 @@ in another application. ex. curl"
([container-name path] ([container-name path]
(sign-remove-blob-request container-name path *blobstore*)) (sign-remove-blob-request container-name path *blobstore*))
([container-name path #^BlobStore blobstore] ([container-name path #^BlobStore blobstore]
(.signRemoveBlob (.getContext blobstore) container-name path))) (.signRemoveBlob (.. blobstore getContext getSigner) container-name path)))
(defn sign-put-blob-request (defn sign-put-blob-request
"Get a signed http request for uploading a blob in another application. "Get a signed http request for uploading a blob in another application.
@ -261,13 +261,32 @@ in another application. ex. curl"
([container-name path content-type size] ([container-name path content-type size]
(sign-put-blob-request container-name path content-type size *blobstore*)) (sign-put-blob-request container-name path content-type size *blobstore*))
([container-name path content-type size #^BlobStore blobstore] ([container-name path content-type size #^BlobStore blobstore]
(.signPutBlob (.getContext blobstore) container-name (.signPutBlob (.. blobstore getContext getSigner) container-name
(doto (.newBlob blobstore path) (doto (.newBlob blobstore path)
(.setPayload (doto (.setPayload (doto
;; until we pass content md5 ;; until we pass content md5
(PhantomPayload. size nil) (PhantomPayload. size nil)
(.setContentType content-type))))))) (.setContentType content-type)))))))
(defn sign-blob-request
"Get a signed http request for manipulating a blob in another application.
ex. curl"
([container-name path
{:keys [method content-type content-length content-md5] :as request}]
(sign-blob-request container-name path request *blobstore*))
([container-name path
{:keys [method content-type content-length content-md5]} blobstore]
{:pre [(or content-length (#{:delete :get} method))]}
(case method
:delete (sign-remove-blob-request container-name path blobstore)
:get (sign-get-blob-request container-name path blobstore)
:put (.signPutBlob
(.. blobstore getContext getSigner) container-name
(doto (.newBlob blobstore path)
(.setPayload
(doto (PhantomPayload. content-length content-md5)
(.setContentType content-type))))))))
(defn get-blob-stream (defn get-blob-stream
"Get an inputstream from the blob at a given path" "Get an inputstream from the blob at a given path"
([container-name path] ([container-name path]

View File

@ -109,6 +109,25 @@
(download-blob container-name name data-file))) (download-blob container-name name data-file)))
(finally (.delete data-file)))))) (finally (.delete data-file))))))
;; this will fail until transient provider handles signing
(deftest sing-put-blob-request-test
(let [request (sign-put-blob-request "container" "path" "text/plain" 10)]
(is (= "PUT" (.getMethod request)))
(is (= "10" (get "Content-Length" (.getHeaders request))))
(is (= "text/plain" (get "Content-Type" (.getHeaders request))))))
;; this will fail until transient provider handles signing
(deftest sing-blob-request-test
(let [request (sign-blob-request "container" "path" {:method :delete})]
(is (= "DELETE" (.getMethod request))))
(let [request (sign-blob-request "container" "path" {:method :get})]
(is (= "GET" (.getMethod request))))
(let [request (sign-blob-request
"container" "path" {:method :put :content-length 10})]
(is (= "PUT" (.getMethod request)))
(is (= "10" (get "Content-Length" (.getHeaders request))))
(is (= "text/plain" (get "Content-Type" (.getHeaders request))))))
;; TODO: more tests involving blob-specific functions ;; TODO: more tests involving blob-specific functions
(deftest corruption-hunt (deftest corruption-hunt