Search in sources :

Example 1 with RoutingRule

use of com.amazonaws.services.s3.model.RoutingRule in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method writeRule.

private void writeRule(XmlWriter xml, RoutingRule rule) {
    xml.start("RoutingRule");
    final RoutingRuleCondition condition = rule.getCondition();
    if (condition != null) {
        xml.start("Condition");
        xml.start("KeyPrefixEquals");
        if (condition.getKeyPrefixEquals() != null) {
            xml.value(condition.getKeyPrefixEquals());
        }
        // </KeyPrefixEquals">
        xml.end();
        if (condition.getHttpErrorCodeReturnedEquals() != null) {
            xml.start("HttpErrorCodeReturnedEquals ").value(condition.getHttpErrorCodeReturnedEquals()).end();
        }
        // </Condition>
        xml.end();
    }
    xml.start("Redirect");
    final 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();
        }
    }
    // </Redirect>
    xml.end();
    // </CORSRule>
    xml.end();
}
Also used : RedirectRule(com.amazonaws.services.s3.model.RedirectRule) RoutingRuleCondition(com.amazonaws.services.s3.model.RoutingRuleCondition)

Example 2 with RoutingRule

use of com.amazonaws.services.s3.model.RoutingRule in project aws-sdk-android by aws-amplify.

the class BucketWebsiteIntegrationTest method testBucketWebsites.

/**
 * Tests that we can get, set, and delete a bucket's website configuration.
 */
@Test
public void testBucketWebsites() throws Exception {
    // create a test bucket
    s3.createBucket(bucketName);
    // get website bucketWebsiteConfiguration for new bucket
    BucketWebsiteConfiguration bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    System.out.println("bucketWebsiteConfiguration: " + bucketWebsiteConfiguration);
    // set website configuration
    bucketWebsiteConfiguration = new BucketWebsiteConfiguration(INDEX_DOCUMENT_SUFFIX, ERROR_DOCUMENT);
    List<RoutingRule> rules = new LinkedList<RoutingRule>();
    RoutingRule rule = new RoutingRule();
    rule.setCondition(new RoutingRuleCondition().withKeyPrefixEquals(PREFIX).withHttpErrorCodeReturnedEquals(ERROR_CODE));
    rule.setRedirect(new RedirectRule().withProtocol(PROTOCOL).withHostName(HOST_NAME).withHttpRedirectCode(REDIRECT_CODE).withReplaceKeyPrefixWith(NEW_PREFIX));
    rules.add(rule);
    rule = new RoutingRule();
    rule.setRedirect(new RedirectRule().withHostName(HOST_NAME));
    rules.add(rule);
    rule = new RoutingRule();
    rule.setCondition(new RoutingRuleCondition().withHttpErrorCodeReturnedEquals(ERROR_CODE));
    rule.setRedirect(new RedirectRule().withReplaceKeyWith(NEW_KEY));
    rules.add(rule);
    bucketWebsiteConfiguration.setRoutingRules(rules);
    s3.setBucketWebsiteConfiguration(bucketName, bucketWebsiteConfiguration);
    // get again
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertEquals(INDEX_DOCUMENT_SUFFIX, bucketWebsiteConfiguration.getIndexDocumentSuffix());
    assertEquals(ERROR_DOCUMENT, bucketWebsiteConfiguration.getErrorDocument());
    assertEquals(3, bucketWebsiteConfiguration.getRoutingRules().size());
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getCondition().getKeyPrefixEquals(), PREFIX);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getCondition().getHttpErrorCodeReturnedEquals(), ERROR_CODE);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getprotocol(), PROTOCOL);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getHostName(), HOST_NAME);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getHttpRedirectCode(), REDIRECT_CODE);
    assertEquals(bucketWebsiteConfiguration.getRoutingRules().get(0).getRedirect().getReplaceKeyPrefixWith(), NEW_PREFIX);
    assertNull(bucketWebsiteConfiguration.getRoutingRules().get(1).getCondition());
    assertEquals(HOST_NAME, bucketWebsiteConfiguration.getRoutingRules().get(1).getRedirect().getHostName());
    assertEquals("", bucketWebsiteConfiguration.getRoutingRules().get(2).getCondition().getKeyPrefixEquals());
    assertEquals(NEW_KEY, bucketWebsiteConfiguration.getRoutingRules().get(2).getRedirect().getReplaceKeyWith());
    // Test RedirectAllRequestsTo
    bucketWebsiteConfiguration = new BucketWebsiteConfiguration();
    bucketWebsiteConfiguration.setRedirectAllRequestsTo(new RedirectRule().withHostName(HOST_NAME).withProtocol(PROTOCOL));
    s3.setBucketWebsiteConfiguration(bucketName, bucketWebsiteConfiguration);
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertNotNull(bucketWebsiteConfiguration.getRedirectAllRequestsTo());
    assertEquals(HOST_NAME, bucketWebsiteConfiguration.getRedirectAllRequestsTo().getHostName());
    assertEquals(PROTOCOL, bucketWebsiteConfiguration.getRedirectAllRequestsTo().getprotocol());
    // delete
    s3.deleteBucketWebsiteConfiguration(bucketName);
    Thread.sleep(1000 * 60 * 1);
    // get again
    bucketWebsiteConfiguration = s3.getBucketWebsiteConfiguration(bucketName);
    assertNull(bucketWebsiteConfiguration);
}
Also used : RedirectRule(com.amazonaws.services.s3.model.RedirectRule) BucketWebsiteConfiguration(com.amazonaws.services.s3.model.BucketWebsiteConfiguration) RoutingRuleCondition(com.amazonaws.services.s3.model.RoutingRuleCondition) RoutingRule(com.amazonaws.services.s3.model.RoutingRule) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with RoutingRule

use of com.amazonaws.services.s3.model.RoutingRule in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

/**
 * 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>
 *
 * @param websiteConfiguration The configuration to convert.
 * @return The XML byte array representation.
 */
public byte[] convertToXmlByteArray(BucketWebsiteConfiguration websiteConfiguration) {
    final XmlWriter xml = new XmlWriter();
    xml.start("WebsiteConfiguration", "xmlns", Constants.XML_NAMESPACE);
    if (websiteConfiguration.getIndexDocumentSuffix() != null) {
        final XmlWriter indexDocumentElement = xml.start("IndexDocument");
        indexDocumentElement.start("Suffix").value(websiteConfiguration.getIndexDocumentSuffix()).end();
        indexDocumentElement.end();
    }
    if (websiteConfiguration.getErrorDocument() != null) {
        final XmlWriter errorDocumentElement = xml.start("ErrorDocument");
        errorDocumentElement.start("Key").value(websiteConfiguration.getErrorDocument()).end();
        errorDocumentElement.end();
    }
    final RedirectRule redirectAllRequestsTo = websiteConfiguration.getRedirectAllRequestsTo();
    if (redirectAllRequestsTo != null) {
        final 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) {
        final XmlWriter routingRules = xml.start("RoutingRules");
        for (final RoutingRule rule : websiteConfiguration.getRoutingRules()) {
            writeRule(routingRules, rule);
        }
        routingRules.end();
    }
    xml.end();
    return xml.getBytes();
}
Also used : RedirectRule(com.amazonaws.services.s3.model.RedirectRule) RoutingRule(com.amazonaws.services.s3.model.RoutingRule) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Aggregations

RedirectRule (com.amazonaws.services.s3.model.RedirectRule)3 RoutingRule (com.amazonaws.services.s3.model.RoutingRule)2 RoutingRuleCondition (com.amazonaws.services.s3.model.RoutingRuleCondition)2 XmlWriter (com.amazonaws.services.s3.internal.XmlWriter)1 BucketWebsiteConfiguration (com.amazonaws.services.s3.model.BucketWebsiteConfiguration)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1