From 028e9f543fb49b0990e113db56db93263afc9273 Mon Sep 17 00:00:00 2001 From: Chris Tavares Date: Tue, 9 Oct 2012 16:22:12 -0700 Subject: [PATCH] Added error checking to list blob call to defend against missing headers --- .../blob/implementation/BlobRestProxy.java | 15 +++++++-- .../blob/BlobServiceIntegrationTest.java | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 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 1366704cf2d81..1a8d830c20dfc 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 @@ -976,8 +976,19 @@ else if (options.isUncommittedList()) { ListBlobBlocksResult result = response.getEntity(ListBlobBlocksResult.class); result.setEtag(response.getHeaders().getFirst("ETag")); result.setContentType(response.getHeaders().getFirst("Content-Type")); - result.setContentLength(Long.parseLong(response.getHeaders().getFirst("x-ms-blob-content-length"))); - result.setLastModified(dateMapper.parse(response.getHeaders().getFirst("Last-Modified"))); + + String blobContentLength = response.getHeaders().getFirst("x-ms-blob-content-length"); + if (blobContentLength != null) { + result.setContentLength(Long.parseLong(blobContentLength)); + } + else { + result.setContentLength(0); + } + + String lastModified = response.getHeaders().getFirst("Last-Modified"); + if (lastModified != null) { + result.setLastModified(dateMapper.parse(lastModified)); + } return result; } 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 28e5d990ec7ce..23ebb100ed1c5 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 @@ -650,6 +650,38 @@ public void listBlobsWithDelimiterWorks() throws Exception { assertEquals(0, results6.getBlobPrefixes().size()); } + // Repro case for https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/295 + @Test + public void listBlockBlobWithNoCommittedBlocksWorks() throws Exception { + Configuration config = createConfiguration(); + BlobContract service = BlobService.create(config); + + String container = "mysample2"; + String blob = "test14"; + + try { + service.createContainer(container); + service.listBlobs(container); + service.createBlockBlob(container, blob, null); + service.createBlobBlock(container, blob, "01", new ByteArrayInputStream(new byte[] { 0x00 })); + service.listBlobBlocks(container, blob, new ListBlobBlocksOptions().setCommittedList(true) + .setUncommittedList(true)); + service.listBlobs(container); + service.deleteContainer(container); + Thread.sleep(40000); + service.createContainer(container); + service.listBlobs(container); + service.createBlobBlock(container, blob, "01", new ByteArrayInputStream(new byte[] { 0x00 })); + // The next line throws + service.listBlobBlocks(container, blob, new ListBlobBlocksOptions().setCommittedList(true) + .setUncommittedList(true)); + service.listBlobs(container); + } + finally { + deleteContainers(service, null, new String[] { container }); + } + } + @Test public void createPageBlobWorks() throws Exception { // Arrange