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

The following examples show how to use com.microsoft.azure.storage.core.Utility#serializeElement() . 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: QueueMessageSerializer.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Generates the message request body from a string containing the message.
 * The message must be encodable as UTF-8. To be included in a web request,
 * this message request body must be written to the output stream of the web
 * request.
 * 
 * @param message
 *            A <code>String<code> containing the message to wrap in a message request body.
 * 
 * @return An array of <code>byte</code> containing the message request body
 *         encoded as UTF-8.
 * @throws IOException
 *             if there is an error writing the queue message.
 * @throws IllegalStateException
 *             if there is an error writing the queue message.
 * @throws IllegalArgumentException
 *             if there is an error writing the queue message.
 */
public static byte[] generateMessageRequestBody(final String message) 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, QueueConstants.QUEUE_MESSAGE_ELEMENT);

    Utility.serializeElement(xmlw, QueueConstants.MESSAGE_TEXT_ELEMENT, message);

    // end QueueMessage_ELEMENT
    xmlw.endTag(Constants.EMPTY_STRING, QueueConstants.QUEUE_MESSAGE_ELEMENT);

    // end doc
    xmlw.endDocument();

    return outWriter.toString().getBytes(Constants.UTF8_CHARSET);
}
 
Example 2
Source File: ServicePropertiesSerializer.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the retention policy to the XMLStreamWriter.
 * 
 * @param xmlw
 *            the XMLStreamWriter to write to.
 * @param val
 *            the nullable Integer indicating if the retention policy is enabled, and how long
 * @throws IOException
 *             if there is an error writing the service properties.
 * @throws IllegalStateException
 *             if there is an error writing the service properties.
 * @throws IllegalArgumentException
 *             if there is an error writing the service properties.
 */
private static void writeRetentionPolicy(final XmlSerializer xmlw, final Integer val)
        throws IllegalArgumentException, IllegalStateException, IOException {
    xmlw.startTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.RETENTION_POLICY_ELEMENT);

    // Enabled
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.ENABLED_ELEMENT, val != null ? Constants.TRUE
            : Constants.FALSE);

    if (val != null) {
        // Days
        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.DAYS_ELEMENT, val.toString());
    }

    // End Retention Policy
    xmlw.endTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.RETENTION_POLICY_ELEMENT);
}
 
Example 3
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 4
Source File: ServicePropertiesSerializer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given CORS properties to the XMLStreamWriter.
 * 
 * @param xmlw
 *            the XMLStreamWriter to write to.
 * @param cors
 *            the CORS Properties to be written.
 * @throws IOException
 *             if there is an error writing the service properties.
 * @throws IllegalStateException
 *             if there is an error writing the service properties.
 * @throws IllegalArgumentException
 *             if there is an error writing the service properties.
 */
private static void writeCorsProperties(final XmlSerializer xmlw, final CorsProperties cors)
        throws IllegalArgumentException, IllegalStateException, IOException {
    Utility.assertNotNull("CorsRules", cors.getCorsRules());

    // CORS
    xmlw.startTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.CORS_ELEMENT);

    for (CorsRule rule : cors.getCorsRules()) {
        if (rule.getAllowedOrigins().isEmpty() || rule.getAllowedMethods().isEmpty()
                || rule.getMaxAgeInSeconds() < 0) {
            throw new IllegalArgumentException(SR.INVALID_CORS_RULE);
        }

        xmlw.startTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.CORS_RULE_ELEMENT);

        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.ALLOWED_ORIGINS_ELEMENT,
                joinToString(rule.getAllowedOrigins(), ","));

        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.ALLOWED_METHODS_ELEMENT,
                joinToString(rule.getAllowedMethods(), ","));

        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.EXPOSED_HEADERS_ELEMENT,
                joinToString(rule.getExposedHeaders(), ","));

        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.ALLOWED_HEADERS_ELEMENT,
                joinToString(rule.getAllowedHeaders(), ","));

        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.MAX_AGE_IN_SECONDS_ELEMENT,
                Integer.toString(rule.getMaxAgeInSeconds()));

        xmlw.endTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.CORS_RULE_ELEMENT);
    }

    // end CORS
    xmlw.endTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.CORS_ELEMENT);
}
 
Example 5
Source File: ServicePropertiesSerializer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given metrics properties to the XMLStreamWriter.
 * 
 * @param xmlw
 *            the XMLStreamWriter to write to.
 * @param metrics
 *            the metrics properties to be written.
 * @param metricsName
 *            the type of metrics properties to be written (Hour or Minute)
 * @throws IOException
 *             if there is an error writing the service properties.
 * @throws IllegalStateException
 *             if there is an error writing the service properties.
 * @throws IllegalArgumentException
 *             if there is an error writing the service properties.
 */
private static void writeMetricsProperties(final XmlSerializer xmlw, final MetricsProperties metrics,
        final String metricsName) throws IllegalArgumentException, IllegalStateException, IOException {
    Utility.assertNotNull("metrics.Configuration", metrics.getMetricsLevel());

    // Metrics
    xmlw.startTag(Constants.EMPTY_STRING, metricsName);

    // Version
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.VERSION_ELEMENT, metrics.getVersion());

    // Enabled
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.ENABLED_ELEMENT,
            metrics.getMetricsLevel() != MetricsLevel.DISABLED ? Constants.TRUE : Constants.FALSE);

    if (metrics.getMetricsLevel() != MetricsLevel.DISABLED) {
        // Include APIs
        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.INCLUDE_APIS_ELEMENT,
                metrics.getMetricsLevel() == MetricsLevel.SERVICE_AND_API ? Constants.TRUE : Constants.FALSE);
    }

    // Retention Policy
    writeRetentionPolicy(xmlw, metrics.getRetentionIntervalInDays());

    // end Metrics
    xmlw.endTag(Constants.EMPTY_STRING, metricsName);
}
 
Example 6
Source File: ServicePropertiesSerializer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given logging properties to the XMLStreamWriter.
 * 
 * @param xmlw
 *            the XMLStreamWriter to write to.
 * @param cors
 *            the logging properties to be written.
 * @throws IOException
 *             if there is an error writing the service properties.
 * @throws IllegalStateException
 *             if there is an error writing the service properties.
 * @throws IllegalArgumentException
 *             if there is an error writing the service properties.
 */
private static void writeLoggingProperties(final XmlSerializer xmlw, final LoggingProperties logging)
        throws IllegalArgumentException, IllegalStateException, IOException {
    Utility.assertNotNull("logging.LogOperationTypes", logging.getLogOperationTypes());

    // Logging
    xmlw.startTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.LOGGING_ELEMENT);

    // Version
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.VERSION_ELEMENT, logging.getVersion());

    // Delete
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.DELETE_ELEMENT, logging.getLogOperationTypes()
            .contains(LoggingOperations.DELETE) ? Constants.TRUE : Constants.FALSE);

    // Read
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.READ_ELEMENT, logging.getLogOperationTypes()
            .contains(LoggingOperations.READ) ? Constants.TRUE : Constants.FALSE);

    // Write
    Utility.serializeElement(xmlw, Constants.AnalyticsConstants.WRITE_ELEMENT, logging.getLogOperationTypes()
            .contains(LoggingOperations.WRITE) ? Constants.TRUE : Constants.FALSE);

    // Retention Policy
    writeRetentionPolicy(xmlw, logging.getRetentionIntervalInDays());

    // end Logging
    xmlw.endTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.LOGGING_ELEMENT);
}
 
Example 7
Source File: SharedAccessPolicySerializer.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * RESERVED FOR INTERNAL USE. Writes a collection of shared access policies to the specified stream in XML format.
 *
 * @param <T>
 *
 * @param sharedAccessPolicies
 *            A collection of shared access policies
 * @param outWriter
 *            a sink to write the output to.
 * @throws IOException
 *             if there is an error writing the shared access identifiers.
 * @throws IllegalStateException
 *             if there is an error writing the shared access identifiers.
 * @throws IllegalArgumentException
 *             if there is an error writing the shared access identifiers.
 */
public static <T extends SharedAccessPolicy> void writeSharedAccessIdentifiersToStream(
        final HashMap<String, T> sharedAccessPolicies, final StringWriter outWriter)
        throws IllegalArgumentException, IllegalStateException, IOException {
    Utility.assertNotNull("sharedAccessPolicies", sharedAccessPolicies);
    Utility.assertNotNull("outWriter", outWriter);

    final XmlSerializer xmlw = Utility.getXmlSerializer(outWriter);

    if (sharedAccessPolicies.keySet().size() > Constants.MAX_SHARED_ACCESS_POLICY_IDENTIFIERS) {
        final String errorMessage = String.format(SR.TOO_MANY_SHARED_ACCESS_POLICY_IDENTIFIERS,
                sharedAccessPolicies.keySet().size(), Constants.MAX_SHARED_ACCESS_POLICY_IDENTIFIERS);

        throw new IllegalArgumentException(errorMessage);
    }

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

    for (final Entry<String, T> entry : sharedAccessPolicies.entrySet()) {
        final SharedAccessPolicy policy = entry.getValue();
        xmlw.startTag(Constants.EMPTY_STRING, Constants.SIGNED_IDENTIFIER_ELEMENT);

        // Set the identifier
        Utility.serializeElement(xmlw, Constants.ID, entry.getKey());

        xmlw.startTag(Constants.EMPTY_STRING, Constants.ACCESS_POLICY);

        // Set the Start Time
        Utility.serializeElement(xmlw, Constants.START, Utility
                .getUTCTimeOrEmpty(policy.getSharedAccessStartTime()).toString());

        // Set the Expiry Time
        Utility.serializeElement(xmlw, Constants.EXPIRY,
                Utility.getUTCTimeOrEmpty(policy.getSharedAccessExpiryTime()).toString());

        // Set the Permissions
        Utility.serializeElement(xmlw, Constants.PERMISSION, policy.permissionsToString());

        // end AccessPolicy
        xmlw.endTag(Constants.EMPTY_STRING, Constants.ACCESS_POLICY);
        // end SignedIdentifier
        xmlw.endTag(Constants.EMPTY_STRING, Constants.SIGNED_IDENTIFIER_ELEMENT);
    }

    // end SignedIdentifiers
    xmlw.endTag(Constants.EMPTY_STRING, Constants.SIGNED_IDENTIFIERS_ELEMENT);
    // end doc
    xmlw.endDocument();
}
 
Example 8
Source File: ServicePropertiesSerializer.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Writes the contents of the ServiceProperties to the stream in xml format.
 * 
 * @return a byte array of the content to write to the stream.
 * @throws IOException
 *             if there is an error writing the service properties.
 * @throws IllegalStateException
 *             if there is an error writing the service properties.
 * @throws IllegalArgumentException
 *             if there is an error writing the service properties.
 */
public static byte[] serializeToByteArray(final ServiceProperties properties) 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, Constants.AnalyticsConstants.STORAGE_SERVICE_PROPERTIES_ELEMENT);

    if (properties.getLogging() != null) {
        // Logging Properties
        writeLoggingProperties(xmlw, properties.getLogging());
    }

    if (properties.getHourMetrics() != null) {
        // Hour Metrics
        writeMetricsProperties(xmlw, properties.getHourMetrics(), Constants.AnalyticsConstants.HOUR_METRICS_ELEMENT);
    }

    if (properties.getMinuteMetrics() != null) {
        // Minute Metrics
        writeMetricsProperties(xmlw, properties.getMinuteMetrics(),
                Constants.AnalyticsConstants.MINUTE_METRICS_ELEMENT);
    }

    if (properties.getCors() != null) {
        // CORS Properties
        writeCorsProperties(xmlw, properties.getCors());
    }

    // Default Service Version
    if (properties.getDefaultServiceVersion() != null) {
        Utility.serializeElement(xmlw, Constants.AnalyticsConstants.DEFAULT_SERVICE_VERSION,
                properties.getDefaultServiceVersion());
    }

    // end StorageServiceProperties
    xmlw.endTag(Constants.EMPTY_STRING, Constants.AnalyticsConstants.STORAGE_SERVICE_PROPERTIES_ELEMENT);

    // end doc
    xmlw.endDocument();

    return outWriter.toString().getBytes(Constants.UTF8_CHARSET);
}