From ce7d2a458e264745f84e4d01014822c1bfdd3df9 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Fri, 18 Nov 2011 18:17:51 -0800 Subject: [PATCH] Replace magic string with enum Issue #42 --- .../blob/implementation/BlobRestProxy.java | 18 ++++++++++++++++-- .../services/blob/models/ContainerACL.java | 10 +++++++--- .../blob/BlobServiceIntegrationTest.java | 5 +++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/implementation/BlobRestProxy.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/implementation/BlobRestProxy.java index aa4ab0da8e605..bc522234f974a 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/implementation/BlobRestProxy.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/implementation/BlobRestProxy.java @@ -20,6 +20,7 @@ import com.microsoft.windowsazure.services.blob.models.BlockList; import com.microsoft.windowsazure.services.blob.models.CommitBlobBlocksOptions; import com.microsoft.windowsazure.services.blob.models.ContainerACL; +import com.microsoft.windowsazure.services.blob.models.ContainerACL.PublicAccessType; import com.microsoft.windowsazure.services.blob.models.CopyBlobOptions; import com.microsoft.windowsazure.services.blob.models.CreateBlobBlockOptions; import com.microsoft.windowsazure.services.blob.models.CreateBlobOptions; @@ -345,7 +346,15 @@ public GetContainerACLResult getContainerACL(String container, BlobServiceOption ContainerACL.SignedIdentifiers si = response.getEntity(ContainerACL.SignedIdentifiers.class); ContainerACL acl = new ContainerACL(); acl.setSignedIdentifiers(si.getSignedIdentifiers()); - acl.setPublicAccess(response.getHeaders().getFirst("x-ms-blob-public-access")); + if ("container".equals(response.getHeaders().getFirst("x-ms-blob-public-access"))) { + acl.setPublicAccess(PublicAccessType.CONTAINER_AND_BLOBS); + } + else if ("blob".equals(response.getHeaders().getFirst("x-ms-blob-public-access"))) { + acl.setPublicAccess(PublicAccessType.BLOBS_ONLY); + } + else { + acl.setPublicAccess(PublicAccessType.NONE); + } acl.setEtag(response.getHeaders().getFirst("ETag")); acl.setLastModified(dateMapper.parse(response.getHeaders().getFirst("Last-Modified"))); @@ -363,7 +372,12 @@ public void setContainerACL(String container, ContainerACL acl, BlobServiceOptio .queryParam("comp", "acl"); Builder builder = webResource.header("x-ms-version", API_VERSION); - builder = addOptionalHeader(builder, "x-ms-blob-public-access", acl.getPublicAccess()); + if (acl.getPublicAccess() == PublicAccessType.BLOBS_ONLY) { + builder = addOptionalHeader(builder, "x-ms-blob-public-access", "blob"); + } + else if (acl.getPublicAccess() == PublicAccessType.CONTAINER_AND_BLOBS) { + builder = addOptionalHeader(builder, "x-ms-blob-public-access", "container"); + } ContainerACL.SignedIdentifiers si = new ContainerACL.SignedIdentifiers(); si.setSignedIdentifiers(acl.getSignedIdentifiers()); diff --git a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/models/ContainerACL.java b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/models/ContainerACL.java index 6f4dd818f30be..ce247cd084a8c 100644 --- a/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/models/ContainerACL.java +++ b/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/models/ContainerACL.java @@ -13,7 +13,7 @@ public class ContainerACL { private String etag; private Date lastModified; - private String publicAccess; // "blob", "container" or null + private PublicAccessType publicAccess; private List signedIdentifiers = new ArrayList(); public String getEtag() { @@ -32,11 +32,11 @@ public void setLastModified(Date lastModified) { this.lastModified = lastModified; } - public String getPublicAccess() { + public PublicAccessType getPublicAccess() { return publicAccess; } - public void setPublicAccess(String publicAccess) { + public void setPublicAccess(PublicAccessType publicAccess) { this.publicAccess = publicAccess; } @@ -132,4 +132,8 @@ public void setPermission(String permission) { this.permission = permission; } } + + public static enum PublicAccessType { + NONE, BLOBS_ONLY, CONTAINER_AND_BLOBS, + } } diff --git a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/blob/BlobServiceIntegrationTest.java b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/blob/BlobServiceIntegrationTest.java index d4e20814401da..89288c84eb78f 100644 --- a/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/blob/BlobServiceIntegrationTest.java +++ b/microsoft-azure-api/src/test/java/com/microsoft/windowsazure/services/blob/BlobServiceIntegrationTest.java @@ -32,6 +32,7 @@ import com.microsoft.windowsazure.services.blob.models.BlobProperties; import com.microsoft.windowsazure.services.blob.models.BlockList; import com.microsoft.windowsazure.services.blob.models.ContainerACL; +import com.microsoft.windowsazure.services.blob.models.ContainerACL.PublicAccessType; import com.microsoft.windowsazure.services.blob.models.CreateBlobOptions; import com.microsoft.windowsazure.services.blob.models.CreateBlobPagesResult; import com.microsoft.windowsazure.services.blob.models.CreateBlobSnapshotOptions; @@ -285,7 +286,7 @@ public void setContainerACLWorks() throws Exception { service.createContainer(container); ContainerACL acl = new ContainerACL(); - acl.setPublicAccess("blob"); + acl.setPublicAccess(PublicAccessType.BLOBS_ONLY); acl.addSignedIdentifier("test", expiryStartDate, expiryEndDate, "rwd"); service.setContainerACL(container, acl); @@ -297,7 +298,7 @@ public void setContainerACLWorks() throws Exception { assertNotNull(acl2.getEtag()); assertNotNull(acl2.getLastModified()); assertNotNull(acl2.getPublicAccess()); - assertEquals("blob", acl2.getPublicAccess()); + assertEquals(PublicAccessType.BLOBS_ONLY, acl2.getPublicAccess()); assertEquals(1, acl2.getSignedIdentifiers().size()); assertEquals("test", acl2.getSignedIdentifiers().get(0).getId()); assertEquals(expiryStartDate, acl2.getSignedIdentifiers().get(0).getAccessPolicy().getStart());