@Override protected void logout() { context.getSendingRequestEventHandler().removeListener(listener); }
/** * Binds a new lister to the operation context so the WASB file system can * appropriately intercept sends and allow concurrent OOB I/Os. This * by-passes the blob immutability check when reading streams. * * @param opContext the operation context assocated with this request. */ public static void bind(OperationContext opContext) { opContext.getSendingRequestEventHandler().addListener(new SendRequestIntercept()); }
private void setDelay(final OperationContext ctx, final int timeInMs) { ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { try { Thread.sleep(timeInMs); } catch (InterruptedException e) { // do nothing } } }); } }
/** * Fires events representing that a request will be sent. */ private static void fireSendingRequestEvent(OperationContext opContext, HttpURLConnection request, RequestResult result) { if (opContext.getSendingRequestEventHandler().hasListeners() || OperationContext.getGlobalSendingRequestEventHandler().hasListeners()) { SendingRequestEvent event = new SendingRequestEvent(opContext, request, result); opContext.getSendingRequestEventHandler().fireEvent(event); OperationContext.getGlobalSendingRequestEventHandler().fireEvent(event); } }
/** * Fires events representing that a request will be sent. */ private static void fireSendingRequestEvent(OperationContext opContext, HttpURLConnection request, RequestResult result) { if (opContext.getSendingRequestEventHandler().hasListeners() || OperationContext.getGlobalSendingRequestEventHandler().hasListeners()) { SendingRequestEvent event = new SendingRequestEvent(opContext, request, result); opContext.getSendingRequestEventHandler().fireEvent(event); OperationContext.getGlobalSendingRequestEventHandler().fireEvent(event); } }
@Override public OperationContext modifyOperationContext(OperationContext original) { original.getSendingRequestEventHandler().addListener( new TransientErrorInjector(connectionRecognizer)); return original; } });
@Override public OperationContext modifyOperationContext(OperationContext original) { original.getSendingRequestEventHandler().addListener( new TransientErrorInjector(connectionRecognizer)); return original; } });
public static void hook(OperationContext operationContext, float readFactor, float writeFactor) { SelfThrottlingIntercept throttler = new SelfThrottlingIntercept( operationContext, readFactor, writeFactor); ResponseReceivedListener responseListener = throttler.new ResponseReceivedListener(); SendingRequestListener sendingListener = throttler.new SendingRequestListener(); operationContext.getResponseReceivedEventHandler().addListener( responseListener); operationContext.getSendingRequestEventHandler().addListener( sendingListener); }
static void hook(OperationContext context) { context.getErrorReceivingResponseEventHandler().addListener( new ErrorReceivingResponseEventHandler()); context.getSendingRequestEventHandler().addListener( new SendingRequestEventHandler()); context.getResponseReceivedEventHandler().addListener( new ResponseReceivedEventHandler()); }
public MultiLocationTestHelper(final StorageUri storageUri, final StorageLocation initialLocation, final List<RetryContext> retryContextList, final List<RetryInfo> retryInfoList) { this.initialLocation = initialLocation; this.retryContextList = retryContextList; this.retryInfoList = retryInfoList; this.operationContext = new OperationContext(); this.operationContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { if (MultiLocationTestHelper.this.error == null) { StorageLocation location = (MultiLocationTestHelper.this.requestCounter == 0) ? initialLocation : retryInfoList.get( MultiLocationTestHelper.this.requestCounter - 1).getTargetLocation(); if (!eventArg.getRequestResult().getTargetLocation().equals(location)) { MultiLocationTestHelper.this.error = String.format("Request %s was sent to %s while the location should have been %s", MultiLocationTestHelper.this.requestCounter, eventArg.getRequestResult().getTargetLocation(), location); } } MultiLocationTestHelper.this.requestCounter++; } }); this.retryPolicy = new AlwaysRetry(this.retryContextList, this.retryInfoList); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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(); } }
@Test public void testCloudBlobContainerDeleteIfExistsErrorCode() throws StorageException { try { container.delete(); fail("Container should not already exist."); } catch (StorageException e) { assertEquals(StorageErrorCodeStrings.CONTAINER_NOT_FOUND, e.getErrorCode()); } OperationContext ctx = new OperationContext(); ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) { try { container.delete(); assertFalse(container.exists()); } catch (StorageException e) { fail("Delete should succeed."); } } } }); container.create(); // Container deletes succeed before garbage collection occurs. assertTrue(container.deleteIfExists(null, null, ctx)); }
@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")); }
@Test public void testAppendBlockOperations() throws Exception { CloudBlobContainer container = getTestAccount().getRealContainer(); OperationContext context = new OperationContext(); context.getResponseReceivedEventHandler().addListener( new ResponseReceivedEventHandler()); context.getSendingRequestEventHandler().addListener( new SendingRequestEventHandler()); CloudAppendBlob appendBlob = container.getAppendBlobReference( "testAppendBlockOperations"); assertNull(lastOperationTypeSent); assertNull(lastOperationTypeReceived); assertEquals(0, lastContentLengthReceived); try ( BlobOutputStream output = appendBlob.openWriteNew(null, null, context); ) { assertEquals(BlobOperationDescriptor.OperationType.CreateBlob, lastOperationTypeReceived); assertEquals(0, lastContentLengthReceived); String message = "this is a test"; output.write(message.getBytes("UTF-8")); output.flush(); assertEquals(BlobOperationDescriptor.OperationType.AppendBlock, lastOperationTypeSent); assertEquals(BlobOperationDescriptor.OperationType.AppendBlock, lastOperationTypeReceived); assertEquals(message.length(), lastContentLengthReceived); } }
@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); }