Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.s3.model.transform;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Converts bucket configuration objects into XML byte arrays.
 
 public class BucketConfigurationXmlFactory {

    
Converts the specified versioning configuration into an XML byte array.

Parameters:
versioningConfiguration The configuration to convert.
Returns:
The XML byte array representation.
 
     public byte[] convertToXmlByteArray(BucketVersioningConfiguration versioningConfiguration) {
         XmlWriter xml = new XmlWriter();
         xml.start("VersioningConfiguration""xmlns".);
         xml.start("Status").value(versioningConfiguration.getStatus()).end();
 
         Boolean mfaDeleteEnabled = versioningConfiguration.isMfaDeleteEnabled();
         if (mfaDeleteEnabled != null) {
             if (mfaDeleteEnabled) {
                 xml.start("MfaDelete").value("Enabled").end();
             } else {
                 xml.start("MfaDelete").value("Disabled").end();
             }
         }
 
         xml.end();
 
         return xml.getBytes();
     }

    
Converts the specified logging configuration into an XML byte array.

Parameters:
loggingConfiguration The configuration to convert.
Returns:
The XML byte array representation.
 
     public byte[] convertToXmlByteArray(BucketLoggingConfiguration loggingConfiguration) {
         // Default log file prefix to the empty string if none is specified
         String logFilePrefix = loggingConfiguration.getLogFilePrefix();
         if (logFilePrefix == null)
             logFilePrefix = "";
 
         XmlWriter xml = new XmlWriter();
         xml.start("BucketLoggingStatus""xmlns".);
         if (loggingConfiguration.isLoggingEnabled()) {
             xml.start("LoggingEnabled");
            xml.start("TargetBucket").value(loggingConfiguration.getDestinationBucketName()).end();
            xml.start("TargetPrefix").value(loggingConfiguration.getLogFilePrefix()).end();
            xml.end();
        }
        xml.end();
        return xml.getBytes();
    }

    
Converts the specified notification configuration into an XML byte array.

Parameters:
notificationConfiguration The configuration to convert.
Returns:
The XML byte array representation.
    public byte[] convertToXmlByteArray(
            BucketNotificationConfiguration notificationConfiguration) {
        XmlWriter xml = new XmlWriter();
        xml.start("NotificationConfiguration""xmlns".);
        Map<StringNotificationConfigurationconfigurations = notificationConfiguration
                .getConfigurations();
        for (Map.Entry<StringNotificationConfigurationentry : configurations
                .entrySet()) {
            String configName = entry.getKey();
            NotificationConfiguration config = entry.getValue();
            if (config instanceof TopicConfiguration) {
                xml.start("TopicConfiguration");
                xml.start("Id").value(configName).end();
                xml.start("Topic")
                        .value(((TopicConfigurationconfig).getTopicARN())
                        .end();
                addPrefixesAndEvents(xmlconfig);
                xml.end();
            } else if (config instanceof QueueConfiguration) {
                xml.start("QueueConfiguration");
                xml.start("Id").value(configName).end();
                xml.start("Queue")
                        .value(((QueueConfigurationconfig).getQueueARN())
                        .end();
                addPrefixesAndEvents(xmlconfig);
                xml.end();
            } else if (config instanceof CloudFunctionConfiguration) {
                xml.start("CloudFunctionConfiguration");
                xml.start("Id").value(configName).end();
                xml.start("InvocationRole")
                        .value(((CloudFunctionConfigurationconfig)
                                .getInvocationRoleARN()).end();
                xml.start("CloudFunction")
                        .value(((CloudFunctionConfigurationconfig).getCloudFunctionARN())
                        .end();
                addPrefixesAndEvents(xmlconfig);
                xml.end();
            } else if (config instanceof LambdaConfiguration) {
                xml.start("CloudFunctionConfiguration");
                xml.start("Id").value(configName).end();
                xml.start("CloudFunction")
                        .value(((LambdaConfigurationconfig).getFunctionARN())
                        .end();
                addPrefixesAndEvents(xmlconfig);
                xml.end();
            }
        }
        xml.end();
        return xml.getBytes();
    }
    private void addPrefixesAndEvents(XmlWriter xml,
            NotificationConfiguration config) {
        Set<Stringevents = config.getEvents();
        for (String event : events) {
            xml.start("Event").value(event).end();
        }
        List<StringobjectPrefixes = config.getObjectPrefixes();
        for (String prefix : objectPrefixes) {
            xml.start("Prefix").value(prefix).end();
        }
    }
    public byte[] convertToXmlByteArray(
            BucketReplicationConfiguration replicationConfiguration) {
        XmlWriter xml = new XmlWriter();
        xml.start("ReplicationConfiguration");
        Map<StringReplicationRulerules = replicationConfiguration
                .getRules();
        final String role = replicationConfiguration.getRoleARN();
        xml.start("Role").value(role).end();
        for (Map.Entry<StringReplicationRuleentry : rules
                .entrySet()) {
            final String ruleId = entry.getKey();
            final ReplicationRule rule = entry.getValue();
            xml.start("Rule");
            xml.start("ID").value(ruleId).end();
            xml.start("Prefix").value(rule.getPrefix()).end();
            xml.start("Status").value(rule.getStatus()).end();
            final ReplicationDestinationConfig config = rule.getDestinationConfig();
            xml.start("Destination");
            xml.start("Bucket").value(config.getBucketARN()).end();
            xml.end();
            xml.end();
        }
        xml.end();
        return xml.getBytes();
    }

    
Converts the specified website configuration into an XML byte array to send to S3. Sample XML: <WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <IndexDocument> <Suffix>index.html</Suffix> </IndexDocument> <ErrorDocument> <Key>404.html</Key> </ErrorDocument> </WebsiteConfiguration>

Parameters:
websiteConfiguration The configuration to convert.
Returns:
The XML byte array representation.
    public byte[] convertToXmlByteArray(BucketWebsiteConfiguration websiteConfiguration) {
        XmlWriter xml = new XmlWriter();
        xml.start("WebsiteConfiguration""xmlns".);
        if (websiteConfiguration.getIndexDocumentSuffix() != null) {
            XmlWriter indexDocumentElement = xml.start("IndexDocument");
            indexDocumentElement.start("Suffix").value(websiteConfiguration.getIndexDocumentSuffix()).end();
            indexDocumentElement.end();
        }
        if (websiteConfiguration.getErrorDocument() != null) {
            XmlWriter errorDocumentElement = xml.start("ErrorDocument");
            errorDocumentElement.start("Key").value(websiteConfiguration.getErrorDocument()).end();
            errorDocumentElement.end();
        }
        RedirectRule redirectAllRequestsTo = websiteConfiguration.getRedirectAllRequestsTo();
        if (redirectAllRequestsTo != null) {
            XmlWriter redirectAllRequestsElement = xml.start("RedirectAllRequestsTo");
            if (redirectAllRequestsTo.getprotocol() != null) {
                xml.start("Protocol").value(redirectAllRequestsTo.getprotocol()).end();
            }
            if (redirectAllRequestsTo.getHostName() != null) {
                xml.start("HostName").value(redirectAllRequestsTo.getHostName()).end();
            }
            if (redirectAllRequestsTo.getReplaceKeyPrefixWith() != null) {
                xml.start("ReplaceKeyPrefixWith").value(redirectAllRequestsTo.getReplaceKeyPrefixWith()).end();
            }
            if (redirectAllRequestsTo.getReplaceKeyWith() != null) {
                xml.start("ReplaceKeyWith").value(redirectAllRequestsTo.getReplaceKeyWith()).end();
            }
            redirectAllRequestsElement.end();
        }
        if (websiteConfiguration.getRoutingRules() != null && websiteConfiguration.getRoutingRules().size() > 0) {
            XmlWriter routingRules = xml.start("RoutingRules");
            for (RoutingRule rule : websiteConfiguration.getRoutingRules()) {
                writeRule(routingRulesrule);
            }
            routingRules.end();
        }
        xml.end();
        return xml.getBytes();
    }

    
Converts the specified com.amazonaws.services.s3.model.BucketLifecycleConfiguration object to an XML fragment that can be sent to Amazon S3.

     /* <LifecycleConfiguration>
           <Rule>
               <ID>logs-rule</ID>
               <Prefix>logs/</Prefix>
               <Status>Enabled</Status>
               <Transition>
                   <Days>30</Days>
                   <StorageClass>GLACIER</StorageClass>
               </Transition>
               <Expiration>
                   <Days>365</Days>
               </Expiration>
               <NoncurrentVersionTransition>
                   <NoncurrentDays>7</NoncurrentDays>
                   <StorageClass>GLACIER</StorageClass>
               </NoncurrentVersionTransition>
               <NoncurrentVersionExpiration>
                   <NoncurrentDays>14</NoncurrentDays>
               </NoncurrentVersionExpiration>
           </Rule>
           <Rule>
               <ID>image-rule</ID>
               <Prefix>image/</Prefix>
               <Status>Enabled</Status>
               <Transition>
                   <Date>2012-12-31T00:00:00.000Z</Date>
                   <StorageClass>GLACIER</StorageClass>
               </Transition>
               <Expiration>
                   <Date>2020-12-31T00:00:00.000Z</Date>
               </Expiration>
          </Rule>
    </LifecycleConfiguration>
    */
        XmlWriter xml = new XmlWriter();
        xml.start("LifecycleConfiguration");
        for (Rule rule : config.getRules()) {
            writeRule(xmlrule);
        }
        xml.end();
        return xml.getBytes();
    }

    
Converts the specified com.amazonaws.services.s3.model.BucketCrossOriginConfiguration object to an XML fragment that can be sent to Amazon S3.

    /*
     * <CORSConfiguration>
             <CORSRule>
               <AllowedOrigin>http://www.foobar.com</AllowedOrigin>
               <AllowedMethod>GET</AllowedMethod>
               <MaxAgeSeconds>3000</MaxAgeSec>
               <ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
             </CORSRule>
       </CORSConfiguration>
     */
        XmlWriter xml = new XmlWriter();
        xml.start("CORSConfiguration""xmlns".);
        for (CORSRule rule : config.getRules()) {
            writeRule(xmlrule);
        }
        xml.end();
        return xml.getBytes();
    }
    private void writeRule(XmlWriter xmlRule rule) {
        xml.start("Rule");
        if (rule.getId() != null) {
            xml.start("ID").value(rule.getId()).end();
        }
        xml.start("Prefix").value(rule.getPrefix()).end();
        xml.start("Status").value(rule.getStatus()).end();
        Transition transition = rule.getTransition();
        if (transition != null) {
            xml.start("Transition");
            if (transition.getDate() != null) {
                xml.start("Date");
                xml.value(ServiceUtils.formatIso8601Date(transition.getDate()));
                xml.end();
            }
            if (transition.getDays() != -1) {
                xml.start("Days");
                xml.value(Integer.toString(transition.getDays()));
                xml.end();
            }
            xml.start("StorageClass");
            xml.value(transition.getStorageClass().toString());
            xml.end(); // <StorageClass>
            xml.end(); // </Transition>
        }
        NoncurrentVersionTransition ncvTransition =
            rule.getNoncurrentVersionTransition();
        if (ncvTransition != null) {
            xml.start("NoncurrentVersionTransition");
            if (ncvTransition.getDays() != -1) {
                xml.start("NoncurrentDays");
                xml.value(Integer.toString(ncvTransition.getDays()));
                xml.end();
            }
            xml.start("StorageClass");
            xml.value(ncvTransition.getStorageClass().toString());
            xml.end();  // </StorageClass>
            xml.end();  // </NoncurrentVersionTransition>
        }
        if (rule.getExpirationInDays() != -1) {
            xml.start("Expiration");
            xml.start("Days").value("" + rule.getExpirationInDays()).end();
            xml.end(); // </Expiration>
        }
        if (rule.getNoncurrentVersionExpirationInDays() != -1) {
            xml.start("NoncurrentVersionExpiration");
            xml.start("NoncurrentDays")
                .value(Integer.toString(
                    rule.getNoncurrentVersionExpirationInDays()))
                .end();
            xml.end(); // </NoncurrentVersionExpiration>
        }
        if (rule.getExpirationDate() != null) {
            xml.start("Expiration");
            xml.start("Date").value(ServiceUtils.formatIso8601Date(rule.getExpirationDate())).end();
            xml.end(); // </Expiration>
        }
        xml.end(); // </Rule>
    }
    private void writeRule(XmlWriter xmlCORSRule rule) {
        xml.start("CORSRule");
        if (rule.getId() != null) {
            xml.start("ID").value(rule.getId()).end();
        }
        if (rule.getAllowedOrigins() != null) {
            for (String origin : rule.getAllowedOrigins()) {
                xml.start("AllowedOrigin").value(origin).end();
            }
        }
        if (rule.getAllowedMethods() != null) {
            for (AllowedMethods method : rule.getAllowedMethods()) {
                xml.start("AllowedMethod").value(method.toString()).end();
            }
        }
        if(rule.getMaxAgeSeconds() != 0) {
            xml.start("MaxAgeSeconds").value(Integer.toString(rule.getMaxAgeSeconds())).end();
        }
        if (rule.getExposedHeaders() != null) {
            for (String header : rule.getExposedHeaders()) {
                xml.start("ExposeHeader").value(header).end();
            }
        }
        if (rule.getAllowedHeaders() != null) {
            for(String header : rule.getAllowedHeaders()) {
                xml.start("AllowedHeader").value(header).end();
            }
        }
        xml.end();//</CORSRule>
    }
    private void writeRule(XmlWriter xmlRoutingRule rule) {
        xml.start("RoutingRule");
        RoutingRuleCondition condition = rule.getCondition();
        if (condition != null) {
            xml.start("Condition");
            xml.start("KeyPrefixEquals");
            if (condition.getKeyPrefixEquals() != null) {
                xml.value(condition.getKeyPrefixEquals());
            }
            xml.end(); // </KeyPrefixEquals">
            if (condition.getHttpErrorCodeReturnedEquals() != null) {
                xml.start("HttpErrorCodeReturnedEquals ").value(condition.getHttpErrorCodeReturnedEquals()).end();
            }
            xml.end(); // </Condition>
        }
        xml.start("Redirect");
        RedirectRule redirect = rule.getRedirect();
        if (redirect != null) {
            if (redirect.getprotocol() != null) {
                xml.start("Protocol").value(redirect.getprotocol()).end();
            }
            if (redirect.getHostName() != null) {
                xml.start("HostName").value(redirect.getHostName()).end();
            }
            if (redirect.getReplaceKeyPrefixWith() != null) {
                xml.start("ReplaceKeyPrefixWith").value(redirect.getReplaceKeyPrefixWith()).end();
            }
            if (redirect.getReplaceKeyWith() != null) {
                xml.start("ReplaceKeyWith").value(redirect.getReplaceKeyWith()).end();
            }
            if (redirect.getHttpRedirectCode() != null) {
                xml.start("HttpRedirectCode").value(redirect.getHttpRedirectCode()).end();
            }
        }
        xml.end(); // </Redirect>
        xml.end();// </CORSRule>
    }


    
Converts the specified com.amazonaws.services.s3.model.BucketTaggingConfiguration object to an XML fragment that can be sent to Amazon S3.

    /*
     * <Tagging>
         <TagSet>
            <Tag>
                   <Key>Project</Key>
                   <Value>Foo</Value>
            </Tag>
            <Tag>
                   <Key>User</Key>
                   <Value>nschnarr</Value>
            </Tag>
         </TagSet>
        </Tagging>
    */
        XmlWriter xml = new XmlWriter();
        xml.start("Tagging");
        for (TagSet tagset : config.getAllTagSets()) {
            writeRule(xmltagset);
        }
        xml.end();
        return xml.getBytes();
    }
    private void writeRule(XmlWriter xmlTagSet tagset) {
        xml.start("TagSet");
        for ( String key : tagset.getAllTags().keySet() ) {
            xml.start("Tag");
            xml.start("Key").value(key).end();
            xml.start("Value").value(tagset.getTag(key)).end();
            xml.end(); // </Tag>
        }
        xml.end(); // </TagSet>
    }
New to GrepCode? Check out our FAQ X