com.microsoft.azure.storage.StorageEvent Java Examples

The following examples show how to use com.microsoft.azure.storage.StorageEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SasTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testApiVersion() throws InvalidKeyException, StorageException, URISyntaxException {
    SharedAccessBlobPolicy sp1 = createSharedAccessPolicy(
            EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE,
                    SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 3600);
    String sas = this.blob.generateSharedAccessSignature(sp1, null);

    // should not be appended before signing
    assertEquals(-1, sas.indexOf(Constants.QueryConstants.API_VERSION));

    OperationContext ctx = new OperationContext();
    ctx.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() {

        @Override
        public void eventOccurred(ResponseReceivedEvent eventArg) {
            // should be appended after signing
            HttpURLConnection conn = (HttpURLConnection) eventArg.getConnectionObject();
            assertTrue(conn.getURL().toString().indexOf(Constants.QueryConstants.API_VERSION) != -1);
        }
    });

    CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?" + sas));
    sasBlob.uploadMetadata(null, null, ctx);
}
 
Example #2
Source File: CloudPageBlobTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    pageBlobRef.upload(srcStream, length, null, null, context);
    pageBlobRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #3
Source File: CloudBlobServerEncryptionTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobEncryption() throws URISyntaxException, StorageException, IOException {
    this.requestFound = false;
    
    OperationContext ctxt = new OperationContext();
    ctxt.getRequestCompletedEventHandler().addListener(new StorageEvent<RequestCompletedEvent>() {
        @Override
        public void eventOccurred(RequestCompletedEvent eventArg) {
            assertTrue(eventArg.getRequestResult().isRequestServiceEncrypted());
            CloudBlobServerEncryptionTests.this.requestFound = true;
        }
    });

    this.blob.uploadText("test", null, null, null, ctxt);
    assertTrue(this.requestFound);
}
 
Example #4
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testApiVersion() throws InvalidKeyException, StorageException, URISyntaxException {
    SharedAccessFilePolicy policy = createSharedAccessPolicy(
            EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.WRITE,
                    SharedAccessFilePermissions.LIST, SharedAccessFilePermissions.DELETE), 300);
    String sas = this.file.generateSharedAccessSignature(policy, null);
    
    // should not be appended before signing
    assertEquals(-1, sas.indexOf(Constants.QueryConstants.API_VERSION));
    
    OperationContext ctx = new OperationContext();
    ctx.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() {

        @Override
        public void eventOccurred(ResponseReceivedEvent eventArg) {
            // should be appended after signing
            HttpURLConnection conn = (HttpURLConnection) eventArg.getConnectionObject();
            assertTrue(conn.getURL().toString().indexOf(Constants.QueryConstants.API_VERSION) != -1);
        }
    });

    CloudFile sasFile = new CloudFile(new URI(this.file.getUri().toString() + "?" + sas));
    sasFile.uploadMetadata(null, null, ctx);
}
 
Example #5
Source File: CloudFileServerEncryptionTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudFileDirectoryEncryption() throws URISyntaxException, StorageException, IOException {
    this.requestFound = false;

    OperationContext ctxt = new OperationContext();
    ctxt.getRequestCompletedEventHandler().addListener(new StorageEvent<RequestCompletedEvent>() {
        @Override
        public void eventOccurred(RequestCompletedEvent eventArg) {
            assertTrue(eventArg.getRequestResult().isRequestServiceEncrypted());
            CloudFileServerEncryptionTests.this.requestFound = true;
        }
    });

    this.dir.uploadMetadata(null, null, ctxt);
    assertTrue(this.requestFound);

    this.requestFound = false;
    CloudFileDirectory dir2 = this.share.getRootDirectoryReference().getDirectoryReference("dir2");
    dir2.create(null, ctxt);
    assertTrue(this.requestFound);
}
 
Example #6
Source File: CloudFileServerEncryptionTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudFileUploadEncryption() throws URISyntaxException, StorageException, IOException {
    this.requestFound = false;

    OperationContext ctxt = new OperationContext();
    ctxt.getRequestCompletedEventHandler().addListener(new StorageEvent<RequestCompletedEvent>() {
        @Override
        public void eventOccurred(RequestCompletedEvent eventArg) {
            assertTrue(eventArg.getRequestResult().isRequestServiceEncrypted());
            CloudFileServerEncryptionTests.this.requestFound = true;
        }
    });

    this.file.uploadText("test", null, null, null, ctxt);
    assertTrue(this.requestFound);

    this.requestFound = false;
    this.file.uploadProperties(null, null, ctxt);
    assertTrue(this.requestFound);

    this.requestFound = false;
    this.file.uploadMetadata(null, null, ctxt);
    assertTrue(this.requestFound);
}
 
Example #7
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String fileName = FileTestHelper.generateRandomFileName();
    final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    fileRef.upload(srcStream, length, null, null, context);
    fileRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #8
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    blockBlobRef.upload(srcStream, -1, null, null, context);
    blockBlobRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #9
Source File: TableQueryTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
private void testSelectOnlySendsReservedColumnsOnce(TableRequestOptions options, boolean usePropertyResolver)
         {
    // Create entity to use property resolver
    Class1 randEnt = TableTestHelper.generateRandomEntity(null);

    if (usePropertyResolver) {
        options.setPropertyResolver(randEnt);
    }

    OperationContext opContext = new OperationContext();
    opContext.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() {

        @Override
        public void eventOccurred(ResponseReceivedEvent eventArg) {
            HttpURLConnection conn = (HttpURLConnection) eventArg.getConnectionObject();

            String urlString = conn.getURL().toString();

            assertEquals(urlString.indexOf("PartitionKey"), urlString.lastIndexOf("PartitionKey"));
            assertEquals(urlString.indexOf("RowKey"), urlString.lastIndexOf("RowKey"));
            assertEquals(urlString.indexOf("Timestamp"), urlString.lastIndexOf("Timestamp"));
        }
    });

    final Iterable<Class1> result = table.execute(
            TableQuery.from(Class1.class).select(new String[] { "PartitionKey", "RowKey", "Timestamp" }), options,
            opContext);

    // Validate results
    for (Class1 ent : result) {
        assertEquals(ent.getA(), null);
        assertEquals(ent.getB(), null);
        assertEquals(ent.getC(), null);
        assertEquals(ent.getD(), null);
    }
}
 
Example #10
Source File: TableTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloudTableDeleteIfExistsErrorCode() throws StorageException, URISyntaxException {
    final CloudTable table = TableTestHelper.getRandomTableReference();

    try {
        assertFalse(table.deleteIfExists());
        OperationContext ctx = new OperationContext();
        ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

            @Override
            public void eventOccurred(SendingRequestEvent eventArg) {
                if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                    try {
                        table.delete();
                        assertFalse(table.exists());
                    } catch (StorageException e) {
                        fail("Delete should succeed.");
                    }
                }
            }
        });

        table.create();

        // The second delete of a table will return a 404
        assertFalse(table.deleteIfExists(null, ctx));
    } finally {
        table.deleteIfExists();
    }
}
 
Example #11
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileSASWithSharedAccessFileHeaders() throws InvalidKeyException, IllegalArgumentException,
        StorageException, URISyntaxException, InterruptedException {
    SharedAccessFilePolicy policy = createSharedAccessPolicy(EnumSet.of(SharedAccessFilePermissions.READ,
            SharedAccessFilePermissions.WRITE, SharedAccessFilePermissions.LIST), 300);
    FileSharePermissions perms = new FileSharePermissions();

    perms.getSharedAccessPolicies().put("rwperm", policy);
    this.share.uploadPermissions(perms);
    Thread.sleep(30000);

    SharedAccessFileHeaders headers = new SharedAccessFileHeaders();
    headers.setCacheControl("no-cache");
    headers.setContentDisposition("attachment; filename=\"fname.ext\"");
    headers.setContentEncoding("gzip");
    headers.setContentLanguage("da");
    headers.setContentType("text/html; charset=utf-8");

    CloudFile sasFile = new CloudFile(
            new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, headers, "rwperm")));
    OperationContext context = new OperationContext();

    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertEquals("no-cache", connection.getHeaderField(Constants.HeaderConstants.CACHE_CONTROL));
            assertEquals("attachment; filename=\"fname.ext\"",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_DISPOSITION));
            assertEquals("gzip", connection.getHeaderField(Constants.HeaderConstants.CONTENT_ENCODING));
            assertEquals("da", connection.getHeaderField(Constants.HeaderConstants.CONTENT_LANGUAGE));
            assertEquals("text/html; charset=utf-8",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_TYPE));
        }
    });

    sasFile.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #12
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlobMultiConditionHeaders() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    OperationContext context = new OperationContext();
    blockBlobRef.upload(srcStream, -1, null, null, context);

    AccessCondition condition = AccessCondition.generateIfMatchCondition(context.getLastResult().getEtag());
    condition.setIfUnmodifiedSinceDate(context.getLastResult().getStartDate());

    StorageEvent<SendingRequestEvent> event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertNotNull(connection.getRequestProperty("If-Unmodified-Since"));
            assertNotNull(connection.getRequestProperty("If-Match"));
        }
    };

    context.getSendingRequestEventHandler().addListener(event);

    blockBlobRef.upload(srcStream, -1, condition, null, context);
}
 
Example #13
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * @throws StorageException
 * @throws URISyntaxException
 * @throws IOException
 * @throws InterruptedException
 */
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testSendingRequestEventBlob() throws StorageException, URISyntaxException, IOException {
    final int length = 128;

    final ArrayList<Boolean> callList = new ArrayList<Boolean>();
    OperationContext sendingRequestEventContext = new OperationContext();
    sendingRequestEventContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            assertEquals(eventArg.getRequestResult(), eventArg.getOpContext().getLastResult());
            callList.add(true);
        }
    });

    assertEquals(0, callList.size());

    //Put blob
    CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "bb", length,
            sendingRequestEventContext);

    assertEquals(1, callList.size());

    //Get blob
    blob.download(new ByteArrayOutputStream(), null, null, sendingRequestEventContext);
    assertEquals(2, callList.size());

    //uploadMetadata
    blob.uploadMetadata(null, null, sendingRequestEventContext);
    assertEquals(3, callList.size());

    //uploadMetadata
    blob.downloadAttributes(null, null, sendingRequestEventContext);
    assertEquals(4, callList.size());

}
 
Example #14
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testDeleteBlobIfExists() throws URISyntaxException, StorageException, IOException {
    final CloudBlockBlob blob1 = this.container.getBlockBlobReference(BlobTestHelper
            .generateRandomBlobNameWithPrefix("testBlob"));

    assertFalse(blob1.exists());
    assertFalse(blob1.deleteIfExists());

    blob1.uploadText("test1");
    assertTrue(blob1.exists());

    assertTrue(blob1.deleteIfExists());
    assertFalse(blob1.deleteIfExists());

    // check if second condition works in delete if exists
    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                try {
                    blob1.delete();
                    assertFalse(blob1.exists());
                }
                catch (StorageException e) {
                    fail("Delete should succeed.");
                }
            }
        }
    });

    // The second delete of a blob will return a 404
    blob1.uploadText("test1");
    assertFalse(blob1.deleteIfExists(DeleteSnapshotsOption.NONE, null, null, ctx));
}
 
Example #15
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyWithChineseChars() throws StorageException, IOException, URISyntaxException {
    String data = "sample data chinese chars 阿䶵";
    CloudBlockBlob copySource = container.getBlockBlobReference("sourcechinescharsblob阿䶵.txt");
    copySource.uploadText(data);

    assertEquals(this.container.getUri() + "/sourcechinescharsblob阿䶵.txt", copySource.getUri().toString());
    assertEquals(this.container.getUri() + "/sourcechinescharsblob%E9%98%BF%E4%B6%B5.txt",
            copySource.getUri().toASCIIString());

    CloudBlockBlob copyDestination = container.getBlockBlobReference("destchinesecharsblob阿䶵.txt");

    assertEquals(this.container.getUri() + "/destchinesecharsblob阿䶵.txt", copyDestination.getUri().toString());
    assertEquals(this.container.getUri() + "/destchinesecharsblob%E9%98%BF%E4%B6%B5.txt",
            copyDestination.getUri().toASCIIString());

    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection con = (HttpURLConnection) eventArg.getConnectionObject();

            // Test the copy destination request url
            assertEquals(CloudBlockBlobTests.this.container.getUri() + "/destchinesecharsblob%E9%98%BF%E4%B6%B5.txt",
                    con.getURL().toString());

            // Test the copy source request property
            assertEquals(CloudBlockBlobTests.this.container.getUri() + "/sourcechinescharsblob%E9%98%BF%E4%B6%B5.txt",
                    con.getRequestProperty("x-ms-copy-source"));
        }
    });

    copyDestination.startCopy(copySource.getUri(), null, null, null, ctx);
    copyDestination.startCopy(copySource, null, null, null, ctx);
}
 
Example #16
Source File: CloudBlobContainerTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testCloudBlobContainerInvalidMetadata() throws StorageException{
    // test client-side fails correctly
    testMetadataFailures(this.container, null, "value1", true);
    testMetadataFailures(this.container, "", "value1", true);
    testMetadataFailures(this.container, " ", "value1", true);
    testMetadataFailures(this.container, "\n \t", "value1", true);

    testMetadataFailures(this.container, "key1", null, false);
    testMetadataFailures(this.container, "key1", "", false);
    testMetadataFailures(this.container, "key1", " ", false);
    testMetadataFailures(this.container, "key1", "\n \t", false);

    // test client can get empty metadata
    this.container.create();

    OperationContext opContext = new OperationContext();
    opContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        // insert a metadata element with an empty value
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection request = (HttpURLConnection) eventArg.getConnectionObject();
            request.setRequestProperty(Constants.HeaderConstants.PREFIX_FOR_STORAGE_METADATA + "key1", "");
        }
    });
    this.container.uploadMetadata(null, null, opContext);

    this.container.downloadAttributes();
    assertEquals(1, this.container.getMetadata().size());
    assertEquals("", this.container.getMetadata().get("key1"));
}
 
Example #17
Source File: CloudAppendBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException,
        StorageException, IOException {
    final String appendBlobName = BlobTestHelper
            .generateRandomBlobNameWithPrefix("testAppendBlob");
    final CloudAppendBlob appendBlobRef = this.container
            .getAppendBlobReference(appendBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper
            .getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(
            new StorageEvent<SendingRequestEvent>() {

                @Override
                public void eventOccurred(SendingRequestEvent eventArg) {
                    HttpURLConnection connection = (HttpURLConnection) eventArg
                            .getConnectionObject();
                    connection.setRequestProperty("x-ms-foo", "");
                }
            });

    appendBlobRef.upload(srcStream, length, null, null, context);
    appendBlobRef
            .download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #18
Source File: CloudAppendBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Delete an append blob if it exists.
 * 
 * @throws StorageException
 * @throws URISyntaxException 
 */
@Test
public void testAppendBlobDeleteIfExists() throws URISyntaxException, StorageException {
    final CloudAppendBlob blob = this.container.getAppendBlobReference(BlobTestHelper
            .generateRandomBlobNameWithPrefix("testBlob"));

    assertFalse(blob.exists());
    assertFalse(blob.deleteIfExists());

    blob.createOrReplace();
    assertTrue(blob.exists());

    assertTrue(blob.deleteIfExists());
    assertFalse(blob.deleteIfExists());

    // check if second condition works in delete if exists
    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                try {
                    blob.delete();
                    assertFalse(blob.exists());
                }
                catch (StorageException e) {
                    fail("Delete should succeed.");
                }
            }
        }
    });

    // The second delete of a blob will return a 404
    blob.createOrReplace();
    assertFalse(blob.deleteIfExists(DeleteSnapshotsOption.NONE, null, null, ctx));
}
 
Example #19
Source File: SasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testBlobSaSWithSharedAccessBlobHeaders() throws InvalidKeyException, IllegalArgumentException,
        StorageException, URISyntaxException,  InterruptedException {
    SharedAccessBlobPolicy sp = createSharedAccessPolicy(EnumSet.of(SharedAccessBlobPermissions.READ,
            SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.LIST), 3600);
    BlobContainerPermissions perms = new BlobContainerPermissions();

    perms.getSharedAccessPolicies().put("readperm", sp);
    this.container.uploadPermissions(perms);
    Thread.sleep(30000);

    SharedAccessBlobHeaders headers = new SharedAccessBlobHeaders();
    headers.setCacheControl("no-cache");
    headers.setContentDisposition("attachment; filename=\"fname.ext\"");
    headers.setContentEncoding("gzip");
    headers.setContentLanguage("da");
    headers.setContentType("text/html; charset=utf-8");

    CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?"
            + this.blob.generateSharedAccessSignature(null, headers, "readperm")));
    OperationContext context = new OperationContext();

    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertEquals("no-cache", connection.getHeaderField(Constants.HeaderConstants.CACHE_CONTROL));
            assertEquals("attachment; filename=\"fname.ext\"",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_DISPOSITION));
            assertEquals("gzip", connection.getHeaderField(Constants.HeaderConstants.CONTENT_ENCODING));
            assertEquals("da", connection.getHeaderField(Constants.HeaderConstants.CONTENT_LANGUAGE));
            assertEquals("text/html; charset=utf-8",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_TYPE));
        }
    });

    sasBlob.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #20
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlockBlobUploadContentMD5() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    int length = 16 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    final ArrayList<Boolean> callList = new ArrayList<Boolean>();
    OperationContext sendingRequestEventContext = new OperationContext();
    StorageEvent<SendingRequestEvent> event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            assertNotNull(((HttpURLConnection) eventArg.getConnectionObject())
                    .getRequestProperty(BlobConstants.BLOB_CONTENT_MD5_HEADER));
            callList.add(true);
        }
    };

    sendingRequestEventContext.getSendingRequestEventHandler().addListener(event);
    assertEquals(0, callList.size());

    // Upload with length less than single threshold. Make sure we calculate the contentMD5.
    blockBlobRef.upload(srcStream, length, null, null, sendingRequestEventContext);
    assertEquals(1, callList.size());

    sendingRequestEventContext.getSendingRequestEventHandler().removeListener(event);
    callList.clear();

    event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            callList.add(true);
        }
    };

    length = 33 * 1024 * 1024;
    srcStream = BlobTestHelper.getRandomDataStream(length);

    sendingRequestEventContext.getSendingRequestEventHandler().addListener(event);
    assertEquals(0, callList.size());

    // Upload with length greater than single threshold. This will do multiple block uploads.
    blockBlobRef.upload(srcStream, length, null, null, sendingRequestEventContext);
    assertTrue(callList.size() > 1);
}
 
Example #21
Source File: BlobOutputStreamTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testFlush() throws Exception {
    CloudBlockBlob blockBlob = this.container.getBlockBlobReference(
            BlobTestHelper.generateRandomBlobNameWithPrefix("flush"));
    
    OperationContext ctx = new OperationContext();
    ctx.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() {
        
        @Override
        public void eventOccurred(ResponseReceivedEvent eventArg) {
            try {
                HttpURLConnection con = (HttpURLConnection) eventArg.getConnectionObject();
                if ("511".equals(con.getRequestProperty(Constants.HeaderConstants.CONTENT_LENGTH))) {
                    Thread.sleep(3000);   
                }
            } catch (InterruptedException e) {
                // do nothing
            }
        }
    });
    
    BlobOutputStream blobOutputStream = blockBlob.openOutputStream(null, null, ctx);
    
    ExecutorService threadExecutor = Executors.newFixedThreadPool(1);
    
    byte[] buffer = BlobTestHelper.getRandomBuffer(511);
    blobOutputStream.write(buffer);
    
    Future<Void> future = threadExecutor.submit(new FlushTask(blobOutputStream));
    Thread.sleep(1000);
    
    buffer = BlobTestHelper.getRandomBuffer(513);
    blobOutputStream.write(buffer);
    
    // Writes complete when the upload is dispatched (not when the upload completes and flush must
    // wait for upload1 to complete. So, flush should finish last and writes should finish in order.
    while(!future.isDone()) {
        Thread.sleep(500);
    }
    
    // After flush we should see the first upload
    ArrayList<BlockEntry> blocks = blockBlob.downloadBlockList(BlockListingFilter.UNCOMMITTED, null, null, null);
    assertEquals(1, blocks.size());
    assertEquals(511, blocks.get(0).getSize());
    
    // After close we should see the second upload
    blobOutputStream.close();
    blocks = blockBlob.downloadBlockList(BlockListingFilter.COMMITTED, null, null, null);
    assertEquals(2, blocks.size());
    assertEquals(513, blocks.get(1).getSize());
}