Java Code Examples for com.microsoft.azure.storage.core.Utility#isNullOrEmpty()

The following examples show how to use com.microsoft.azure.storage.core.Utility#isNullOrEmpty() . 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: CloudQueueMessage.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the message as a byte array.
 * 
 * @return A <code>byte</code> array which contains the content of the message.
 * 
 * @throws StorageException
 *         If a storage service error occurred.
 */
public final byte[] getMessageContentAsByte() throws StorageException {
    if (Utility.isNullOrEmpty(this.messageContent)) {
        return new byte[0];
    }

    if (this.messageType == QueueMessageType.RAW_STRING) {
        try {
            return this.messageContent.getBytes(Constants.UTF8_CHARSET);
        }
        catch (final UnsupportedEncodingException e) {
            throw Utility.generateNewUnexpectedStorageException(e);
        }
    }
    else {
        return Base64.decode(this.messageContent);
    }
}
 
Example 2
Source File: AccessCondition.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * RESERVED FOR INTERNAL USE. Applies the access conditions to the request.
 * 
 * @param request
 *            A <code>java.net.HttpURLConnection</code> object that represents the request 
 *            to which the condition is being applied.
 */
public void applyConditionToRequest(final HttpURLConnection request) {
    applyLeaseConditionToRequest(request);

    if (this.ifModifiedSinceDate != null) {
        request.setRequestProperty(Constants.HeaderConstants.IF_MODIFIED_SINCE,
                Utility.getGMTTime(this.ifModifiedSinceDate));
    }

    if (this.ifUnmodifiedSinceDate != null) {
        request.setRequestProperty(Constants.HeaderConstants.IF_UNMODIFIED_SINCE,
                Utility.getGMTTime(this.ifUnmodifiedSinceDate));
    }

    if (!Utility.isNullOrEmpty(this.ifMatchETag)) {
        request.setRequestProperty(Constants.HeaderConstants.IF_MATCH, this.ifMatchETag);
    }
    
    if (!Utility.isNullOrEmpty(this.ifNoneMatchETag)) {
        request.setRequestProperty(Constants.HeaderConstants.IF_NONE_MATCH, this.ifNoneMatchETag);
    }
}
 
Example 3
Source File: AccessCondition.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * RESERVED FOR INTERNAL USE. Verifies the condition is satisfied.
 * 
 * @param etag
 *            A <code>String</code> that represents the ETag to check.
 * @param lastModified
 *            A <code>java.util.Date</code> object that represents the last modified date/time.
 * 
 * @return <code>true</code> if the condition is satisfied; otherwise, <code>false</code>.
 * 
 */
public boolean verifyConditional(final String etag, final Date lastModified) {
    if (this.ifModifiedSinceDate != null) {
        // The IfModifiedSince has a special helper in HttpURLConnection, use it instead of manually setting the
        // header.
        if (!lastModified.after(this.ifModifiedSinceDate)) {
            return false;
        }
    }

    if (this.ifUnmodifiedSinceDate != null) {
        if (lastModified.after(this.ifUnmodifiedSinceDate)) {
            return false;
        }
    }
    
    if (!Utility.isNullOrEmpty(this.ifMatchETag)) {
        if (!this.ifMatchETag.equals(etag) && !this.ifMatchETag.equals("*")) {
            return false;
        }
    }
    
    if (!Utility.isNullOrEmpty(this.ifNoneMatchETag)) {
        if (this.ifNoneMatchETag.equals(etag)) {
            return false;
        }
    }
    
    return true;
}
 
Example 4
Source File: PropertyPair.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Reserved for internal use. A utility function that returns <code>true</code> if this property is accessible
 * through reflection.
 * 
 * @return
 */
protected boolean shouldProcess() {
    if (Utility.isNullOrEmpty(this.name) || this.getter == null || this.getter.isAnnotationPresent(Ignore.class)
            || this.setter == null || this.setter.isAnnotationPresent(Ignore.class)
            || (!this.getter.getReturnType().equals(this.setter.getParameterTypes()[0]))) {
        return false;
    }

    return true;
}
 
Example 5
Source File: LogRecordStreamReader.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Read a Double from the stream.
 * 
 * @return
 *         the Double read.
 * @throws IOException
 */
public Double readDouble() throws IOException {
    String temp = this.readField(false /* isQuotedString */);

    if (Utility.isNullOrEmpty(temp)) {
        return null;
    }
    else {
        return Double.parseDouble(temp);
    }
}
 
Example 6
Source File: LogRecordStreamReader.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Read a UUID from the stream.
 * 
 * @return
 *         the UUID read.
 * @throws IOException
 */
public UUID readUuid() throws IOException {
    String temp = this.readField(false /* isQuotedString */);

    if (Utility.isNullOrEmpty(temp)) {
        return null;
    }
    else {
        return UUID.fromString(temp);
    }
}
 
Example 7
Source File: BlobResponse.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the LeaseStatus
 *
 * @param request
 *            The response from server.
 * @return The Etag.
 */
public static LeaseStatus getLeaseStatus(final HttpURLConnection request) {
    final String leaseStatus = request.getHeaderField(Constants.HeaderConstants.LEASE_STATUS);
    if (!Utility.isNullOrEmpty(leaseStatus)) {
        return LeaseStatus.parse(leaseStatus);
    }

    return LeaseStatus.UNSPECIFIED;
}
 
Example 8
Source File: TableRequest.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Reserved for internal use. Constructs the core <code>HttpURLConnection</code> to perform an operation.
 * 
 * @param rootUri
 *            A <code>java.net.URI</code> containing an absolute URI to the resource.
 * @param queryBuilder
 *            The <code>UriQueryBuilder</code> for the request.
 * @param opContext
 *            An {@link OperationContext} object for tracking the current operation.
 * @param tableName
 *            The name of the table.
 * @param identity
 *            The identity of the entity, to pass in the Service Managment REST operation URI as
 *            <code><em>tableName</em>(<em>identity</em>)</code>. If <code>null</code>, only the <em>tableName</em>
 *            value will be passed.
 * @param requestMethod
 *            The HTTP request method to set.
 * @param options
 *            A {@link TableRequestOptions} object that specifies execution options such as retry policy and timeout
 *            settings for the operation. This parameter is unused.
 * @return
 *         An <code>HttpURLConnection</code> to use to perform the operation.
 * 
 * @throws IOException
 *             if there is an error opening the connection.
 * @throws URISyntaxException
 *             if the resource URI is invalid.
 * @throws StorageException
 *             if a storage service error occurred during the operation.
 */
private static HttpURLConnection coreCreate(final URI rootUri, final TableRequestOptions tableOptions,
        final UriQueryBuilder queryBuilder, final OperationContext opContext, final String tableName,
        final String eTag, final String identity, final String requestMethod) throws IOException,
        URISyntaxException, StorageException {

    URI queryUri = null;

    // Do point query / delete etc.
    if (!Utility.isNullOrEmpty(identity)) {
        queryUri = PathUtility.appendPathToSingleUri(rootUri, tableName.concat(String.format("(%s)", identity)));
    }
    else {
        queryUri = PathUtility.appendPathToSingleUri(rootUri, tableName);
    }

    final HttpURLConnection retConnection = BaseRequest.createURLConnection(queryUri, tableOptions, queryBuilder,
            opContext);

    setAcceptHeaderForHttpWebRequest(retConnection, tableOptions.getTablePayloadFormat());

    retConnection.setRequestProperty(Constants.HeaderConstants.CONTENT_TYPE,
            TableConstants.HeaderConstants.JSON_CONTENT_TYPE);

    retConnection.setRequestProperty(TableConstants.HeaderConstants.MAX_DATA_SERVICE_VERSION,
            TableConstants.HeaderConstants.MAX_DATA_SERVICE_VERSION_VALUE);

    if (!Utility.isNullOrEmpty(eTag)) {
        retConnection.setRequestProperty(Constants.HeaderConstants.IF_MATCH, eTag);
    }

    retConnection.setRequestMethod(requestMethod);
    return retConnection;
}
 
Example 9
Source File: BlobResponse.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the public access type for the container
 *
 * @param request
 *            The response from server.
 * @return the blob container public access type from the request header.
 */
public static BlobContainerPublicAccessType getPublicAccessLevel(
        final HttpURLConnection request) {
    final String publicAccess = request
            .getHeaderField(BlobConstants.BLOB_PUBLIC_ACCESS_HEADER);
    if (!Utility.isNullOrEmpty(publicAccess)) {
        return BlobContainerPublicAccessType.parse(publicAccess);
    }

    return BlobContainerPublicAccessType.OFF;
}
 
Example 10
Source File: AccessCondition.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Normalizes an ETag to be quoted, unless it is *.
 * 
 * @param inTag
 *            The ETag to normalize.
 * @return The quoted ETag.
 */
private static String normalizeEtag(String inTag) {
    if (Utility.isNullOrEmpty(inTag) || inTag.equals("*")) {
        return inTag;
    }
    else if (inTag.startsWith("\"") && inTag.endsWith("\"")) {
        return inTag;
    }
    else {
        return String.format("\"%s\"", inTag);
    }
}
 
Example 11
Source File: CloudAppendBlob.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the blob's committed block count from the web request.
 * 
 * @param request
 *            The web request from which to parse the committed block count.
 */
private void updateCommittedBlockCountFromResponse(HttpURLConnection request) {
    final String comittedBlockCount = request.getHeaderField(Constants.HeaderConstants.BLOB_COMMITTED_BLOCK_COUNT);
    if (!Utility.isNullOrEmpty(comittedBlockCount))
    {
        this.getProperties().setAppendBlobCommittedBlockCount(Integer.parseInt(comittedBlockCount));
    }
}
 
Example 12
Source File: BlobResponse.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the LeaseDuration
 *
 * @param request
 *            The response from server.
 * @return The LeaseDuration.
 */
public static LeaseDuration getLeaseDuration(final HttpURLConnection request) {
    final String leaseDuration = request.getHeaderField(Constants.HeaderConstants.LEASE_DURATION);
    if (!Utility.isNullOrEmpty(leaseDuration)) {
        return LeaseDuration.parse(leaseDuration);
    }

    return LeaseDuration.UNSPECIFIED;
}
 
Example 13
Source File: CloudTableClient.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Reserved for internal use. Generates a query to list table names with the given prefix.
 * 
 * @param prefix
 *            A <code>String</code> containing the prefix to match on table names to return.
 * @return
 *         A {@link TableQuery} instance for listing table names with the specified prefix.
 */
private TableQuery<TableServiceEntity> generateListTablesQuery(final String prefix) {
    TableQuery<TableServiceEntity> listQuery = TableQuery.<TableServiceEntity> from(TableServiceEntity.class);
    listQuery.setSourceTableName(TableConstants.TABLES_SERVICE_TABLES_NAME);

    if (!Utility.isNullOrEmpty(prefix)) {
        // Append Max char to end '{' is 1 + 'z' in AsciiTable > uppperBound = prefix + '{'
        final String prefixFilter = String.format("(%s ge '%s') and (%s lt '%s{')", TableConstants.TABLE_NAME,
                prefix, TableConstants.TABLE_NAME, prefix);

        listQuery = listQuery.where(prefixFilter);
    }

    return listQuery;
}
 
Example 14
Source File: FileRequest.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a request to return a listing of all files and directories in this storage account. Sign with no
 * length specified.
 * 
 * @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 listingContext
 *            A set of parameters for the listing operation.
 * @param snapshotVersion
 *            the snapshot version to the query builder.
 * @return a HttpURLConnection configured for the operation.
 * @throws IOException
 * @throws URISyntaxException
 * @throws StorageException
 * @throws IllegalArgumentException
 */
public static HttpURLConnection listFilesAndDirectories(final URI uri, final FileRequestOptions fileOptions,
        final OperationContext opContext, final ListingContext listingContext, String snapshotVersion) throws URISyntaxException,
        IOException, StorageException {

    final UriQueryBuilder builder = getDirectoryUriQueryBuilder();
    addShareSnapshot(builder, snapshotVersion);
    builder.add(Constants.QueryConstants.COMPONENT, Constants.QueryConstants.LIST);

    if (listingContext != null) {
        if (!Utility.isNullOrEmpty(listingContext.getMarker())) {
            builder.add(Constants.QueryConstants.MARKER, listingContext.getMarker());
        }

        if (listingContext.getMaxResults() != null && listingContext.getMaxResults() > 0) {
            builder.add(Constants.QueryConstants.MAX_RESULTS, listingContext.getMaxResults().toString());
        }

        if (!Utility.isNullOrEmpty(listingContext.getPrefix())) {
            builder.add(Constants.QueryConstants.PREFIX, listingContext.getPrefix().toString());
        }
    }

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

    request.setRequestMethod(Constants.HTTP_GET);

    return request;
}
 
Example 15
Source File: FileRequest.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a web request to return the ACL for this share. Sign with no length specified.
 * 
 * @param uri
 *            The absolute URI to the share.
 * @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 accessCondition
 *            An {@link AccessCondition} object that represents the access conditions for the share.
 * @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 HttpURLConnection configured for the operation.
 * @throws StorageException
 */
public static HttpURLConnection getAcl(final URI uri, final FileRequestOptions fileOptions,
        final AccessCondition accessCondition, final OperationContext opContext) throws IOException,
        URISyntaxException, StorageException {
    final UriQueryBuilder builder = getShareUriQueryBuilder();
    builder.add(Constants.QueryConstants.COMPONENT, Constants.QueryConstants.ACL);

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

    request.setRequestMethod(Constants.HTTP_GET);

    if (accessCondition != null && !Utility.isNullOrEmpty(accessCondition.getLeaseID())) {
        accessCondition.applyLeaseConditionToRequest(request);
    }

    return request;
}
 
Example 16
Source File: CloudFile.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
protected void updateLengthFromResponse(HttpURLConnection request) {
    final String xContentLengthHeader = request.getHeaderField(FileConstants.CONTENT_LENGTH_HEADER);
    if (!Utility.isNullOrEmpty(xContentLengthHeader)) {
        this.getProperties().setLength(Long.parseLong(xContentLengthHeader));
    }
}
 
Example 17
Source File: CloudBlob.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
protected void updateLengthFromResponse(HttpURLConnection request) {
    final String xContentLengthHeader = request.getHeaderField(BlobConstants.CONTENT_LENGTH_HEADER);
    if (!Utility.isNullOrEmpty(xContentLengthHeader)) {
        this.getProperties().setLength(Long.parseLong(xContentLengthHeader));
    }
}
 
Example 18
Source File: CloudPageBlob.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
protected void updateSequenceNumberFromResponse(HttpURLConnection request) {
    final String sequenceNumber = request.getHeaderField(Constants.HeaderConstants.BLOB_SEQUENCE_NUMBER);
    if (!Utility.isNullOrEmpty(sequenceNumber)) {
        this.getProperties().setPageBlobSequenceNumber(Long.parseLong(sequenceNumber));
    }
}
 
Example 19
Source File: FileInputStream.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a new instance of the FileInputStream class.
 * 
 * @param parentFile
 *            A {@link CloudFile} object which represents the file that this stream is associated with.
 * @param accessCondition
 *            An {@link AccessCondition} object which represents the access conditions for the file.
 * @param options
 *            A {@link FileRequestOptions} object which represents that 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.
 */
@DoesServiceRequest
protected FileInputStream(final CloudFile parentFile, final AccessCondition accessCondition,
        final FileRequestOptions options, final OperationContext opContext) throws StorageException {
    this.parentFileRef = parentFile;
    this.options = new FileRequestOptions(options);
    this.opContext = opContext;
    this.streamFaulted = false;
    this.currentAbsoluteReadPosition = 0;
    this.readSize = parentFile.getStreamMinimumReadSizeInBytes();

    if (options.getUseTransactionalContentMD5() && this.readSize > 4 * Constants.MB) {
        throw new IllegalArgumentException(SR.INVALID_RANGE_CONTENT_MD5_HEADER);
    }

    parentFile.downloadAttributes(accessCondition, this.options, this.opContext);

    this.retrievedContentMD5Value = parentFile.getProperties().getContentMD5();

    // Will validate it if it was returned
    this.validateFileMd5 = !options.getDisableContentMD5Validation()
            && !Utility.isNullOrEmpty(this.retrievedContentMD5Value);

    String previousLeaseId = null;
    if (accessCondition != null) {
        previousLeaseId = accessCondition.getLeaseID();
    }

    this.accessCondition = AccessCondition.generateIfMatchCondition(this.parentFileRef.getProperties().getEtag());
    this.accessCondition.setLeaseID(previousLeaseId);

    this.streamLength = parentFile.getProperties().getLength();

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

    this.reposition(0);
}
 
Example 20
Source File: CloudStorageAccount.java    From azure-storage-android with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an instance of the <code>CloudStorageAccount</code> class using the specified
 * account credentials.
 * <p>
 * With this constructor, the <code>CloudStorageAccount</code> object is constructed using the
 * given HTTP storage service endpoint suffix (if any, otherwise the default is used).
 * 
 * The credentials provided when constructing the <code>CloudStorageAccount</code> object
 * are used to authenticate all further requests against resources that are accessed via
 * the <code>CloudStorageAccount</code> object or a client object created from it.
 * A client object may be a {@link CloudBlobClient} object.
 * 
 * @param storageCredentials
 *            A {@link StorageCredentials} object that represents the storage credentials
 *            to use to authenticate this account.
 * @param useHttps
 *            <code>true</code> to use HTTPS to connect to the storage service endpoints;
 *            otherwise, <code>false</code>.
 * @param endpointSuffix
 *            A String that represents the endpointSuffix to use, if any.
 * @param accountName
 *            A <code>String</code> that contains the account name.  This will be used in place of a
 *            <code>null</code> {@link StorageCredentials#getAccountName()}, but the two must match if
 *            both are not <code>null</code>.
 * 
 * @throws URISyntaxException
 *             If <code>storageCredentials</code> specify an invalid account name.
 */
public CloudStorageAccount(
        final StorageCredentials storageCredentials, final boolean useHttps, final String endpointSuffix, String accountName)
        throws URISyntaxException {
    Utility.assertNotNull("storageCredentials", storageCredentials);
    if (Utility.isNullOrEmpty(accountName)) {
        accountName = storageCredentials.getAccountName();
    }
    else if (!Utility.isNullOrEmpty(storageCredentials.getAccountName()) &&
            !accountName.equals(storageCredentials.getAccountName())) {

        throw new IllegalArgumentException(SR.ACCOUNT_NAME_MISMATCH);
    }
    
    String protocol = useHttps ? Constants.HTTPS : Constants.HTTP;
    
    this.credentials = storageCredentials;
    this.blobStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.BLOB, endpointSuffix));
    this.fileStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.FILE, endpointSuffix));
    this.queueStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.QUEUE, endpointSuffix));
    this.tableStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.TABLE, endpointSuffix));
    this.endpointSuffix = endpointSuffix;
    
    this.isBlobEndpointDefault = true;
    this.isFileEndpointDefault = true;
    this.isQueueEndpointDefault = true;
    this.isTableEndpointDefault = true;
}