com.microsoft.azure.storage.OperationContext Java Examples

The following examples show how to use com.microsoft.azure.storage.OperationContext. 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: FileRequest.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a web request to abort a copy operation.
 * 
 * @param uri
 *            A <code>java.net.URI</code> object that specifies the absolute URI.
 * @param fileOptions
 *            A {@link FileRequestOptions} object that specifies execution options such as retry policy and timeout
 *            settings for the operation. Specify <code>null</code> to use the request options specified on the
 *            {@link CloudFileClient}.
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * @param accessCondition
 *            The access condition to apply to the request. Only lease conditions are supported for this operation.
 * @param copyId
 *            A <code>String</code> object that identifying the copy operation.
 * @return a <code>HttpURLConnection</code> configured for the operation.
 * @throws StorageException
 *             An exception representing any error which occurred during the operation.
 * @throws IllegalArgumentException
 * @throws IOException
 * @throws URISyntaxException
 */
public static HttpURLConnection abortCopy(final URI uri, final FileRequestOptions fileOptions,
        final OperationContext opContext, final AccessCondition accessCondition, final String copyId)
        throws StorageException, IOException, URISyntaxException {

    final UriQueryBuilder builder = new UriQueryBuilder();

    builder.add(Constants.QueryConstants.COMPONENT, Constants.QueryConstants.COPY);
    builder.add(Constants.QueryConstants.COPY_ID, copyId);

    final HttpURLConnection request = BaseRequest.createURLConnection(uri, fileOptions, builder, opContext);

    request.setFixedLengthStreamingMode(0);
    request.setDoOutput(true);
    request.setRequestMethod(Constants.HTTP_PUT);

    request.setRequestProperty(Constants.HeaderConstants.COPY_ACTION_HEADER,
            Constants.HeaderConstants.COPY_ACTION_ABORT);

    if (accessCondition != null) {
        accessCondition.applyConditionToRequest(request);
    }

    return request;
}
 
Example #2
Source File: LeaseTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobLeaseAcquireAndRelease() throws URISyntaxException, StorageException, IOException {
    final int length = 128;
    final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null);

    // Get Lease 
    OperationContext operationContext = new OperationContext();
    final String leaseID = blobRef.acquireLease(15, null /*proposed lease id */, null /*access condition*/,
            null/* BlobRequestOptions */, operationContext);
    final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID);
    assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_CREATED);

    tryUploadWithBadLease(length, blobRef, null, StorageErrorCodeStrings.LEASE_ID_MISSING);

    // Try to upload with lease
    blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1, leaseCondition, null, null);

    // Release lease
    blobRef.releaseLease(leaseCondition);

    // now upload with no lease specified.
    blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1);
}
 
Example #3
Source File: CloudFileDirectory.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the directory if it exists using the specified request options and operation context.
 * 
 * @param accessCondition
 *            An {@link AccessCondition} object that represents the access conditions for the directory.
 * @param options
 *            A {@link FileRequestOptions} object that specifies any additional options for the request. Specifying
 *            <code>null</code> will use the default request options from the associated service client (
 *            {@link CloudFileClient}).
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @return <code>true</code> if the directory existed and was deleted; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
@DoesServiceRequest
public boolean deleteIfExists(AccessCondition accessCondition, FileRequestOptions options,
        OperationContext opContext) throws StorageException, URISyntaxException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, accessCondition, options, opContext);
    if (exists) {
        try {
            this.delete(accessCondition, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
    else {
        return false;
    }
}
 
Example #4
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 #5
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the queue if it exists, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link QueueRequestOptions} object that specifies any additional options for the request. Specifying
 *            <code>null</code> will use the default request options from the associated service client (
 *            {@link CloudQueueClient}).
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @return A value of <code>true</code> if the queue existed in the storage service and has been deleted, otherwise
 *         <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean deleteIfExists(QueueRequestOptions options, OperationContext opContext) throws StorageException {
    options = QueueRequestOptions.populateAndApplyDefaults(options, this.queueServiceClient);

    boolean exists = this.exists(true, options, opContext);
    if (exists) {
        try {
            this.delete(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.QUEUE_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }

    }
    else {
        return false;
    }
}
 
Example #6
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 #7
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the queue if it does not already exist, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link QueueRequestOptions} object that specifies any additional options for the request. Specifying
 *            <code>null</code> will use the default request options from the associated service client (
 *            {@link CloudQueueClient}).
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @return A value of <code>true</code> if the queue is created in the storage service, otherwise <code>false</code>
 *         .
 * 
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean createIfNotExists(QueueRequestOptions options, OperationContext opContext) throws StorageException {
    options = QueueRequestOptions.populateAndApplyDefaults(options, this.queueServiceClient);

    boolean exists = this.exists(true, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.QUEUE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #8
Source File: StorageCredentialsHelper.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Signs a request using the specified operation context under the Shared Key authentication scheme.
 * 
 * @param request
 *            An <code>HttpURLConnection</code> object that represents the request to sign.
 * @param contentLength
 *            The length of the content written to the output stream. If unknown, specify -1.
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @throws InvalidKeyException
 *             If the given key is invalid.
 * @throws StorageException
 *             If a storage service error occurred.
 */
public static void signTableRequest(final StorageCredentials creds, final java.net.HttpURLConnection request,
        final long contentLength, OperationContext opContext) throws InvalidKeyException, StorageException {
    if (creds.getClass().equals(StorageCredentialsAccountAndKey.class)) {
        opContext = opContext == null ? new OperationContext() : opContext;
        request.setRequestProperty(Constants.HeaderConstants.DATE, Utility.getGMTTime());

        final Canonicalizer canonicalizer = CanonicalizerFactory.getTableCanonicalizer(request);

        final String stringToSign = canonicalizer.canonicalize(request, creds.getAccountName(), contentLength);

        final String computedBase64Signature = StorageCredentialsHelper.computeHmac256(creds, stringToSign);
        
        Logger.debug(opContext, LogConstants.SIGNING, stringToSign);

        request.setRequestProperty(Constants.HeaderConstants.AUTHORIZATION,
                String.format("%s %s:%s", "SharedKey", creds.getAccountName(), computedBase64Signature));
    }
}
 
Example #9
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Get the message request base address (Used internally only).
 * 
 * @return The message request <code>URI</code>.
 * 
 * @throws URISyntaxException
 *             If the resource URI is invalid.
 * @throws StorageException
 */
private StorageUri getMessageRequestAddress(final OperationContext opContext) throws URISyntaxException,
        StorageException {
    if (this.messageRequestAddress == null) {
        this.messageRequestAddress = PathUtility.appendPathToUri(this.getTransformedAddress(opContext),
                QueueConstants.MESSAGES);
    }

    return this.messageRequestAddress;
}
 
Example #10
Source File: CloudFileDirectory.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the directory if it does not exist, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link FileRequestOptions} object that specifies any additional options for the request.
 *            Specifying <code>null</code> will use the default request options from the associated service client
 *            ({@link CloudFileClient}).
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @return <code>true</code> if the directory did not already exist and was created; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
@DoesServiceRequest
public boolean createIfNotExists(FileRequestOptions options, OperationContext opContext) throws StorageException, URISyntaxException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);

    this.getShare().assertNoSnapshot();

    boolean exists = this.exists(true /* primaryOnly */, null /* accessCondition */, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.RESOURCE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #11
Source File: TestBlobDataValidation.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void testCheckBlockMd5(final boolean expectMd5Checked)
    throws Exception {
  assumeNotNull(testAccount);
  Path testFilePath = new Path("/testFile");

  // Add a hook to check that for GET/PUT requests we set/don't set
  // the block-level MD5 field as configured. I tried to do clever
  // testing by also messing with the raw data to see if we actually
  // validate the data as expected, but the HttpURLConnection wasn't
  // pluggable enough for me to do that.
  testAccount.getFileSystem().getStore()
  .addTestHookToOperationContext(new TestHookOperationContext() {
  @Override
        public OperationContext modifyOperationContext(
            OperationContext original) {
    original.getResponseReceivedEventHandler().addListener(
        new ContentMD5Checker(expectMd5Checked));
    return original;
        }
      });

  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  byte[] inBuf = new byte[100];
  while (inStream.read(inBuf) > 0){
    //nothing;
  }
  inStream.close();
}
 
Example #12
Source File: HttpUtil.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
public static void setProxyForAzure(OperationContext opContext)
{
  if (useProxy)
  {
    // currently, only host and port are supported. Username and password are not supported.
    Proxy azProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    opContext.setProxy(azProxy);
  }
}
 
Example #13
Source File: AzureNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the given blob, taking special care that if we get a
 * blob-not-found exception upon retrying the operation, we just
 * swallow the error since what most probably happened is that
 * the first operation succeeded on the server.
 * @param blob The blob to delete.
 * @param lease Azure blob lease, or null if no lease is to be used.
 * @throws StorageException
 */
private void safeDelete(CloudBlobWrapper blob, SelfRenewingLease lease) throws StorageException {
  OperationContext operationContext = getInstrumentedContext();
  try {
    blob.delete(operationContext, lease);
  } catch (StorageException e) {
    // On exception, check that if:
    // 1. It's a BlobNotFound exception AND
    // 2. It got there after one-or-more retries THEN
    // we swallow the exception.
    if (e.getErrorCode() != null &&
        e.getErrorCode().equals("BlobNotFound") &&
        operationContext.getRequestResults().size() > 1 &&
        operationContext.getRequestResults().get(0).getException() != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Swallowing delete exception on retry: " + e.getMessage());
      }
      return;
    } else {
      throw e;
    }
  } finally {
    if (lease != null) {
      lease.free();
    }
  }
}
 
Example #14
Source File: CloudPageBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * @throws URISyntaxException
 * @throws StorageException
 * @throws IOException
 */
@Test
public void testPageBlobInputStream() throws URISyntaxException, StorageException, IOException {
    final int blobLength = 16 * 1024;
    final Random randGenerator = new Random();
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);

    final byte[] buff = new byte[blobLength];
    randGenerator.nextBytes(buff);
    buff[0] = -1;
    buff[1] = -128;
    final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff);

    final BlobRequestOptions options = new BlobRequestOptions();
    final OperationContext operationContext = new OperationContext();
    options.setTimeoutIntervalInMs(90000);
    options.setRetryPolicyFactory(new RetryNoRetry());
    blobRef.upload(sourceStream, blobLength, null, options, operationContext);

    BlobInputStream blobStream = blobRef.openInputStream();

    for (int i = 0; i < blobLength; i++) {
        int data = blobStream.read();
        assertTrue(data >= 0);
        assertEquals(buff[i], (byte) data);
    }

    assertEquals(-1, blobStream.read());

    blobRef.delete();
}
 
Example #15
Source File: MimeHelper.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Reserved for internal use. Reads the response stream from a batch operation into an <code>ArrayList</code> of
 * {@link MimePart} objects.
 * 
 * @param inStream
 *            An {@link InputStream} containing the operation response stream.
 * @param expectedBundaryName
 *            A <code>String</code> containing the MIME part boundary string.
 * @param opContext
 *            An {@link OperationContext} object for tracking the current operation. Specify <code>null</code> to
 *            safely ignore operation context.
 * @param format
 *            The {@link TablePayloadFormat} that will be used for parsing
 * @return
 *         An <code>ArrayList</code> of {@link MimePart} objects parsed from the input stream.
 * @throws StorageException
 *             if a storage service error occurs.
 * @throws IOException
 *             if an error occurs while accessing the stream with Json.
 */
static ArrayList<MimePart> readBatchResponseStream(final InputStream inStream, final String expectedBundaryName,
        final OperationContext opContext, TablePayloadFormat format) throws IOException, StorageException {
    final ArrayList<MimePart> result = new ArrayList<MimePart>();
    final InputStreamReader streamReader = new InputStreamReader(inStream, Constants.UTF8_CHARSET);
    final BufferedReader reader = new BufferedReader(streamReader);
    final String mungedExpectedBoundaryName = "--".concat(expectedBundaryName);

    final MimeHeader docHeader = readMimeHeader(reader, opContext);
    if (docHeader.boundary == null || !docHeader.boundary.equals(mungedExpectedBoundaryName)) {
        throw generateMimeParseException();
    }

    MimeHeader currHeader = null;

    // No explicit changeset present
    if (docHeader.subBoundary == null) {
        do {
            result.add(readMimePart(reader, docHeader.boundary, opContext));
            currHeader = readMimeHeader(reader, opContext);
        } while (currHeader != null);
    }
    else {
        // explicit changeset present.
        currHeader = readMimeHeader(reader, opContext);
        if (currHeader == null) {
            throw generateMimeParseException();
        }
        else {
            do {
                result.add(readMimePart(reader, docHeader.subBoundary, opContext));
                currHeader = readMimeHeader(reader, opContext);
            } while (currHeader != null);
        }
    }

    return result;
}
 
Example #16
Source File: Utility.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Logs the HttpURLConnection response. If an exception is encountered, logs nothing.
 * 
 * @param conn
 *            The HttpURLConnection to serialize.
 * @param opContext 
 *            The operation context which provides the logger.
 */
public static void logHttpResponse(HttpURLConnection conn, OperationContext opContext) throws IOException {
    if (Logger.shouldLog(opContext, Log.VERBOSE)) {
        try {
            StringBuilder bld = new StringBuilder();

            // This map's null key will contain the response code and message
            for (Map.Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
                if (header.getKey() != null) {
                    bld.append(header.getKey());
                    bld.append(": ");
                }

                for (int i = 0; i < header.getValue().size(); i++) {
                    bld.append(header.getValue().get(i));
                    if (i < header.getValue().size() - 1) {
                        bld.append(",");
                    }
                }
                bld.append('\n');
            }

            Logger.verbose(opContext, bld.toString());
        } catch (Exception e) {
            // Do nothing
        }
    }
}
 
Example #17
Source File: StorageInterfaceImpl.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void downloadRange(long offset, long length, OutputStream outStream,
    BlobRequestOptions options, OperationContext opContext)
        throws StorageException, IOException {

  getBlob().downloadRange(offset, length, outStream, null, options, opContext);
}
 
Example #18
Source File: BlobOutputStream.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes a new instance of the BlobOutputStream class.
 * 
 * @param parentBlob
 *            A {@link CloudBlob} object which represents the blob that this stream is associated with.
 * @param accessCondition
 *            An {@link AccessCondition} object which represents the access conditions for the blob.
 * @param options
 *            A {@link BlobRequestOptions} object which specifies any additional options for the request.
 * @param opContext
 *            An {@link OperationContext} object which is used to track the execution of the operation.
 * 
 * @throws StorageException
 *             An exception representing any error which occurred during the operation.
 */
private BlobOutputStream(final CloudBlob parentBlob, final AccessCondition accessCondition,
        final BlobRequestOptions options, final OperationContext opContext) throws StorageException {
    this.accessCondition = accessCondition;
    this.parentBlobRef = parentBlob;
    this.parentBlobRef.assertCorrectBlobType();
    this.options = new BlobRequestOptions(options);
    this.outBuffer = new ByteArrayOutputStream();
    this.opContext = opContext;

    if (this.options.getConcurrentRequestCount() < 1) {
        throw new IllegalArgumentException("ConcurrentRequestCount");
    }
    
    this.futureSet = Collections.newSetFromMap(new ConcurrentHashMap<Future<Void>, Boolean>(
            this.options.getConcurrentRequestCount() == null ? 1 : this.options.getConcurrentRequestCount() * 2));

    if (this.options.getStoreBlobContentMD5()) {
        try {
            this.md5Digest = MessageDigest.getInstance("MD5");
        }
        catch (final NoSuchAlgorithmException e) {
            // This wont happen, throw fatal.
            throw Utility.generateNewUnexpectedStorageException(e);
        }
    }

    // V2 cachedThreadPool for perf.        
    this.threadExecutor = new ThreadPoolExecutor(
            this.options.getConcurrentRequestCount(),
            this.options.getConcurrentRequestCount(),
            10, 
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    this.completionService = new ExecutorCompletionService<Void>(this.threadExecutor);
}
 
Example #19
Source File: PageBlobInputStream.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a stream over the given page blob.
 */
public PageBlobInputStream(CloudPageBlobWrapper blob,
    OperationContext opContext)
    throws IOException {
  this.blob = blob;
  this.opContext = opContext;
  ArrayList<PageRange> allRanges;
  try {
    allRanges =
        blob.downloadPageRanges(new BlobRequestOptions(), opContext);
  } catch (StorageException e) {
    throw new IOException(e);
  }
  if (allRanges.size() > 0) {
    if (allRanges.get(0).getStartOffset() != 0) {
      throw badStartRangeException(blob, allRanges.get(0));
    }
    if (allRanges.size() > 1) {
      LOG.warn(String.format(
          "Blob %s has %d page ranges beyond the first range. " 
          + "Only reading the first range.",
          blob.getUri(), allRanges.size() - 1));
    }
    numberOfPagesRemaining =
        (allRanges.get(0).getEndOffset() + 1) / PAGE_SIZE;
  } else {
    numberOfPagesRemaining = 0;
  }
}
 
Example #20
Source File: CloudQueueTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class })
public void testUpdateMessageFullPass() throws  StorageException, InterruptedException {
    CloudQueueMessage message = new CloudQueueMessage("message");
    this.queue.addMessage(message, 20, 0, null, null);
    CloudQueueMessage message1 = this.queue.retrieveMessage();
    String popreceipt1 = message1.getPopReceipt();
    Date NextVisibleTim1 = message1.getNextVisibleTime();
    this.queue.updateMessage(message1, 100, EnumSet.of(MessageUpdateFields.VISIBILITY), null, null);
    String popreceipt2 = message1.getPopReceipt();
    Date NextVisibleTim2 = message1.getNextVisibleTime();
    assertTrue(popreceipt2 != popreceipt1);
    assertTrue(NextVisibleTim1.before(NextVisibleTim2));

    Thread.sleep(2000);

    String newMesage = message.getMessageContentAsString() + "updated";
    message.setMessageContent(newMesage);
    OperationContext oc = new OperationContext();
    this.queue.updateMessage(message1, 100, EnumSet.of(MessageUpdateFields.CONTENT), null, oc);
    assertEquals(oc.getLastResult().getStatusCode(), HttpURLConnection.HTTP_NO_CONTENT);
    String popreceipt3 = message1.getPopReceipt();
    Date NextVisibleTim3 = message1.getNextVisibleTime();
    assertTrue(popreceipt3 != popreceipt2);
    assertTrue(NextVisibleTim2.before(NextVisibleTim3));

    assertTrue(this.queue.retrieveMessage() == null);

    this.queue.updateMessage(message1, 0, EnumSet.of(MessageUpdateFields.VISIBILITY), null, null);

    CloudQueueMessage messageFromGet = this.queue.retrieveMessage();
    assertEquals(messageFromGet.getMessageContentAsString(), message1.getMessageContentAsString());
}
 
Example #21
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 #22
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 #23
Source File: BlockEntryListSerializer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a Block List and returns the corresponding UTF8 bytes.
 * 
 * @param blockList
 *            the Iterable of BlockEntry to write
 * @param opContext
 *            a tracking object for the request
 * @return a byte array of the UTF8 bytes representing the serialized block list.
 * @throws IOException
 *             if there is an error writing the block list.
 * @throws IllegalStateException
 *             if there is an error writing the block list.
 * @throws IllegalArgumentException
 *             if there is an error writing the block list.
 */
public static byte[] writeBlockListToStream(final Iterable<BlockEntry> blockList, final OperationContext opContext)
        throws IllegalArgumentException, IllegalStateException, IOException {

    final StringWriter outWriter = new StringWriter();
    final XmlSerializer xmlw = Utility.getXmlSerializer(outWriter);

    // default is UTF8
    xmlw.startDocument(Constants.UTF8_CHARSET, true);
    xmlw.startTag(Constants.EMPTY_STRING, BlobConstants.BLOCK_LIST_ELEMENT);

    for (final BlockEntry block : blockList) {

        if (block.getSearchMode() == BlockSearchMode.COMMITTED) {
            Utility.serializeElement(xmlw, BlobConstants.COMMITTED_ELEMENT, block.getId());
        }
        else if (block.getSearchMode() == BlockSearchMode.UNCOMMITTED) {
            Utility.serializeElement(xmlw, BlobConstants.UNCOMMITTED_ELEMENT, block.getId());
        }
        else if (block.getSearchMode() == BlockSearchMode.LATEST) {
            Utility.serializeElement(xmlw, BlobConstants.LATEST_ELEMENT, block.getId());
        }
    }

    // end BlockListElement
    xmlw.endTag(Constants.EMPTY_STRING, BlobConstants.BLOCK_LIST_ELEMENT);

    // end doc
    xmlw.endDocument();

    return outWriter.toString().getBytes(Constants.UTF8_CHARSET);
}
 
Example #24
Source File: Logger.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
public static void verbose(OperationContext opContext, String format) {
    if (shouldLog(opContext, Log.VERBOSE)) {
        Log.v(Constants.LOG_TAG, formatLogEntry(opContext, format));
    }
}
 
Example #25
Source File: FileTestHelper.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
public static CloudFile uploadNewFile(CloudFileShare share, int length, OperationContext context)
        throws StorageException, IOException, URISyntaxException {
    
    return uploadNewFile(share, getRandomDataStream(length), length, context);
}
 
Example #26
Source File: MockStorageInterface.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void create(OperationContext opContext) throws StorageException {
  created = true;
  backingStore.setContainerMetadata(metadata);
}
 
Example #27
Source File: Logger.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
public static void error(OperationContext opContext, String format, Object args1, Object args2) {
    if (shouldLog(opContext, Log.ERROR)) {
        Log.e(Constants.LOG_TAG, formatLogEntry(opContext, format, args1, args2));
    }
}
 
Example #28
Source File: StorageInterface.java    From hadoop with Apache License 2.0 4 votes vote down vote up
void uploadMetadata(OperationContext opContext)
throws StorageException;
 
Example #29
Source File: StorageInterfaceImpl.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void create(OperationContext opContext) throws StorageException {
  container.create(null, opContext);
}
 
Example #30
Source File: MockStorageInterface.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void create(long length, BlobRequestOptions options,
    OperationContext opContext) throws StorageException {
  throw new NotImplementedException();
}