From c299b8ff61421a19479410c9c4d6253bae3405e5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 20 Jun 2013 19:45:45 -0700 Subject: [PATCH] Add Swift-specific MD5 checksum header Tested against cloudfiles-us, hpcloud-objectstorage, and swift-keystone. Reference for ETag: https://answers.launchpad.net/swift/+question/157064 --- .../blobstore/CloudFilesBlobSignerExpectTest.java | 1 + .../binders/BindSwiftObjectMetadataToRequest.java | 14 ++++++++++++++ .../swift/blobstore/SwiftBlobSignerExpectTest.java | 1 + .../SwiftKeystoneBlobSignerExpectTest.java | 1 + .../HPCloudObjectStorageBlobSignerExpectTest.java | 1 + 5 files changed, 18 insertions(+) diff --git a/apis/cloudfiles/src/test/java/org/jclouds/cloudfiles/blobstore/CloudFilesBlobSignerExpectTest.java b/apis/cloudfiles/src/test/java/org/jclouds/cloudfiles/blobstore/CloudFilesBlobSignerExpectTest.java index 08ba1b7e0e..9659ec3774 100644 --- a/apis/cloudfiles/src/test/java/org/jclouds/cloudfiles/blobstore/CloudFilesBlobSignerExpectTest.java +++ b/apis/cloudfiles/src/test/java/org/jclouds/cloudfiles/blobstore/CloudFilesBlobSignerExpectTest.java @@ -78,6 +78,7 @@ public class CloudFilesBlobSignerExpectTest extends BaseBlobSignerExpectTest { protected HttpRequest putBlob() { return HttpRequest.builder().method("PUT") .endpoint("https://storage101.lon3.clouddrive.com/v1/MossoCloudFS_83a9d536-2e25-4166-bd3b-a503a934f953/container/name") + .addHeader("ETag", "00020408") .addHeader("Expect", "100-continue") .addHeader("X-Auth-Token", authToken).build(); } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/binders/BindSwiftObjectMetadataToRequest.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/binders/BindSwiftObjectMetadataToRequest.java index 62f07b3369..e4eb4c1cc1 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/binders/BindSwiftObjectMetadataToRequest.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/binders/BindSwiftObjectMetadataToRequest.java @@ -23,6 +23,9 @@ import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.core.MediaType; +import com.google.common.io.BaseEncoding; +import com.google.common.net.HttpHeaders; + import org.jclouds.blobstore.binders.BindUserMetadataToHeadersWithPrefix; import org.jclouds.http.HttpRequest; import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlob; @@ -59,6 +62,17 @@ public class BindSwiftObjectMetadataToRequest implements Binder { // Enable "chunked"/"streamed" data, where the size needn't be known in advance. request = (R) request.toBuilder().replaceHeader("Transfer-Encoding", "chunked").build(); } + + byte[] contentMD5 = object.getInfo().getHash(); + if (contentMD5 != null) { + // Swizzle hash to ETag + object.getInfo().setHash(null); + request = (R) request.toBuilder() + .addHeader(HttpHeaders.ETAG, + BaseEncoding.base16().lowerCase().encode(contentMD5)) + .build(); + } + request = mdBinder.bindToRequest(request, object2Blob.apply(object)); return request; } diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSignerExpectTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSignerExpectTest.java index 90220b9670..b2be6eb015 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSignerExpectTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftBlobSignerExpectTest.java @@ -76,6 +76,7 @@ public class SwiftBlobSignerExpectTest extends BaseBlobSignerExpectTest { protected HttpRequest putBlob() { return HttpRequest.builder().method("PUT") .endpoint("http://storage/container/name") + .addHeader("ETag", "00020408") .addHeader("Expect", "100-continue") .addHeader("X-Auth-Token", "testtoken").build(); } diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftKeystoneBlobSignerExpectTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftKeystoneBlobSignerExpectTest.java index 791d512681..01c21a77d6 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftKeystoneBlobSignerExpectTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/blobstore/SwiftKeystoneBlobSignerExpectTest.java @@ -71,6 +71,7 @@ public class SwiftKeystoneBlobSignerExpectTest extends BaseBlobSignerExpectTest protected HttpRequest putBlob() { return HttpRequest.builder().method("PUT") .endpoint("https://objects.jclouds.org/v1.0/40806637803162/container/name") + .addHeader("ETag", "00020408") .addHeader("Expect", "100-continue") .addHeader("X-Auth-Token", "Auth_4f173437e4b013bee56d1007").build(); } diff --git a/providers/hpcloud-objectstorage/src/test/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobSignerExpectTest.java b/providers/hpcloud-objectstorage/src/test/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobSignerExpectTest.java index 3bcc5cc1b6..1da4b72065 100644 --- a/providers/hpcloud-objectstorage/src/test/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobSignerExpectTest.java +++ b/providers/hpcloud-objectstorage/src/test/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobSignerExpectTest.java @@ -74,6 +74,7 @@ public class HPCloudObjectStorageBlobSignerExpectTest extends BaseBlobSignerExpe protected HttpRequest putBlob() { return HttpRequest.builder().method("PUT") .endpoint("https://objects.jclouds.org/v1.0/40806637803162/container/name") + .addHeader("ETag", "00020408") .addHeader("Expect", "100-continue") .addHeader("X-Auth-Token", "Auth_4f173437e4b013bee56d1007").build(); }