Search in sources :

Example 1 with RoutingRule

use of com.aliyun.oss.model.RoutingRule in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseBucketWebsite.

/**
 * Unmarshall get bucket website response body to corresponding result.
 */
@SuppressWarnings("unchecked")
public static BucketWebsiteResult parseBucketWebsite(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        BucketWebsiteResult result = new BucketWebsiteResult();
        if (root.getChild("IndexDocument") != null) {
            result.setIndexDocument(root.getChild("IndexDocument").getChildText("Suffix"));
        }
        if (root.getChild("ErrorDocument") != null) {
            result.setErrorDocument(root.getChild("ErrorDocument").getChildText("Key"));
        }
        if (root.getChild("RoutingRules") != null) {
            List<Element> ruleElements = root.getChild("RoutingRules").getChildren("RoutingRule");
            for (Element ruleElem : ruleElements) {
                RoutingRule rule = new RoutingRule();
                rule.setNumber(Integer.parseInt(ruleElem.getChildText("RuleNumber")));
                Element condElem = ruleElem.getChild("Condition");
                if (condElem != null) {
                    rule.getCondition().setKeyPrefixEquals(condElem.getChildText("KeyPrefixEquals"));
                    if (condElem.getChild("HttpErrorCodeReturnedEquals") != null) {
                        rule.getCondition().setHttpErrorCodeReturnedEquals(Integer.parseInt(condElem.getChildText("HttpErrorCodeReturnedEquals")));
                    }
                }
                Element redirectElem = ruleElem.getChild("Redirect");
                if (redirectElem.getChild("RedirectType") != null) {
                    rule.getRedirect().setRedirectType(RoutingRule.RedirectType.parse(redirectElem.getChildText("RedirectType")));
                }
                rule.getRedirect().setHostName(redirectElem.getChildText("HostName"));
                if (redirectElem.getChild("Protocol") != null) {
                    rule.getRedirect().setProtocol(RoutingRule.Protocol.parse(redirectElem.getChildText("Protocol")));
                }
                rule.getRedirect().setReplaceKeyPrefixWith(redirectElem.getChildText("ReplaceKeyPrefixWith"));
                rule.getRedirect().setReplaceKeyWith(redirectElem.getChildText("ReplaceKeyWith"));
                if (redirectElem.getChild("HttpRedirectCode") != null) {
                    rule.getRedirect().setHttpRedirectCode(Integer.parseInt(redirectElem.getChildText("HttpRedirectCode")));
                }
                rule.getRedirect().setMirrorURL(redirectElem.getChildText("MirrorURL"));
                rule.getRedirect().setMirrorSecondaryURL(redirectElem.getChildText("MirrorURLSlave"));
                rule.getRedirect().setMirrorProbeURL(redirectElem.getChildText("MirrorURLProbe"));
                if (redirectElem.getChildText("MirrorPassQueryString") != null) {
                    rule.getRedirect().setPassQueryString(Boolean.valueOf(redirectElem.getChildText("MirrorPassQueryString")));
                }
                if (redirectElem.getChildText("MirrorPassOriginalSlashes") != null) {
                    rule.getRedirect().setPassOriginalSlashes(Boolean.valueOf(redirectElem.getChildText("MirrorPassOriginalSlashes")));
                }
                result.AddRoutingRule(rule);
            }
        }
        return result;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) RoutingRule(com.aliyun.oss.model.RoutingRule) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 2 with RoutingRule

use of com.aliyun.oss.model.RoutingRule in project aliyun-oss-java-sdk by aliyun.

the class BucketWebsiteTest method testNormalSetBucketWebsiteWithCDNRedirect.

@Test
public void testNormalSetBucketWebsiteWithCDNRedirect() {
    final String bucketName = "normal-set-bucket-website-redirect-cdn";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        // Set RoutingRule
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.AliCDN);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        RoutingRule rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 1);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.AliCDN);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        ossClient.deleteBucketWebsite(bucketName);
        // Set RoutingRule
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.AliCDN);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.AddRoutingRule(rule);
        rule = new RoutingRule();
        rule.setNumber(5);
        rule.getCondition().setHttpErrorCodeReturnedEquals(403);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Http);
        rule.getRedirect().setReplaceKeyPrefixWith("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getRedirect().setHttpRedirectCode(303);
        request.AddRoutingRule(rule);
        request.setIndexDocument(indexDocument);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 2);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.AliCDN);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        rr = result.getRoutingRules().get(1);
        Assert.assertEquals(rr.getNumber().intValue(), 5);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 403);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Http);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyPrefixWith(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 303);
        ossClient.deleteBucketWebsite(bucketName);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 3 with RoutingRule

use of com.aliyun.oss.model.RoutingRule in project aliyun-oss-java-sdk by aliyun.

the class BucketWebsiteTest method testUnormalSetBucketWebsiteWithRedirect.

@Test
public void testUnormalSetBucketWebsiteWithRedirect() {
    final String bucketName = "unormal-set-bucket-website-redirect";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(-1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test-source/");
        request.setIndexDocument(indexDocument);
        // rule Number invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        request.setIndexDocument(indexDocument);
        // rule value invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // rule ReplaceKeyPrefixWith&ReplaceKeyWith invalid
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setReplaceKeyWith("");
        rule.getRedirect().setReplaceKeyPrefixWith("");
        request.setIndexDocument(indexDocument);
        // rule value invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 4 with RoutingRule

use of com.aliyun.oss.model.RoutingRule in project aliyun-oss-java-sdk by aliyun.

the class BucketWebsiteTest method testNormalSetBucketWebsiteWithRedirect.

@Test
public void testNormalSetBucketWebsiteWithRedirect() {
    final String bucketName = "normal-set-bucket-website-redirect";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        // Set RoutingRule
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        RoutingRule rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 1);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
        // Set RoutingRule
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.AddRoutingRule(rule);
        rule = new RoutingRule();
        rule.setNumber(5);
        rule.getCondition().setHttpErrorCodeReturnedEquals(403);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Http);
        rule.getRedirect().setReplaceKeyPrefixWith("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getRedirect().setHttpRedirectCode(303);
        request.AddRoutingRule(rule);
        request.setIndexDocument(indexDocument);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 2);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        rr = result.getRoutingRules().get(1);
        Assert.assertEquals(rr.getNumber().intValue(), 5);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 403);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Http);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyPrefixWith(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 303);
        ossClient.deleteBucketWebsite(bucketName);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 5 with RoutingRule

use of com.aliyun.oss.model.RoutingRule in project aliyun-oss-java-sdk by aliyun.

the class BucketWebsiteTest method testNormalSetBucketWebsiteWithMirror.

@Test
public void testNormalSetBucketWebsiteWithMirror() {
    final String bucketName = "normal-set-bucket-website-mirror";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        // Set index document and mirror
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test-source/");
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        // check
        BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        RoutingRule rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 1);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.Mirror);
        Assert.assertEquals(rr.getRedirect().getMirrorURL(), "http://oss-test.aliyun-inc.com/mirror-test-source/");
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
        // set mirror with key prefix
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test/");
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        // check
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.Mirror);
        Assert.assertEquals(rr.getRedirect().getMirrorURL(), "http://oss-test.aliyun-inc.com/mirror-test/");
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
        // set mirror with secondary default mirror
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test/");
        rule.getRedirect().setMirrorSecondaryURL(null);
        rule.getRedirect().setMirrorProbeURL(null);
        rule.getRedirect().setPassQueryString(null);
        rule.getRedirect().setPassOriginalSlashes(null);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        // check
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.Mirror);
        Assert.assertEquals(rr.getRedirect().getMirrorURL(), "http://oss-test.aliyun-inc.com/mirror-test/");
        Assert.assertNull(rr.getRedirect().getMirrorSecondaryURL());
        Assert.assertNull(rr.getRedirect().getMirrorProbeURL());
        Assert.assertFalse(rr.getRedirect().isPassQueryString());
        Assert.assertFalse(rr.getRedirect().isPassOriginalSlashes());
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
        // set mirror with secondary mirror
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test/");
        rule.getRedirect().setMirrorSecondaryURL("http://oss-test.aliyun-inc.com/mirror-secodary/");
        rule.getRedirect().setMirrorProbeURL("http://oss-test.aliyun-inc.com/mirror-probe/");
        rule.getRedirect().setPassQueryString(true);
        rule.getRedirect().setPassOriginalSlashes(true);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        // check
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.Mirror);
        Assert.assertEquals(rr.getRedirect().getMirrorURL(), "http://oss-test.aliyun-inc.com/mirror-test/");
        Assert.assertEquals(rr.getRedirect().getMirrorSecondaryURL(), "http://oss-test.aliyun-inc.com/mirror-secodary/");
        Assert.assertEquals(rr.getRedirect().getMirrorProbeURL(), "http://oss-test.aliyun-inc.com/mirror-probe/");
        Assert.assertTrue(rr.getRedirect().isPassQueryString());
        Assert.assertTrue(rr.getRedirect().isPassOriginalSlashes());
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

RoutingRule (com.aliyun.oss.model.RoutingRule)6 OSSException (com.aliyun.oss.OSSException)5 SetBucketWebsiteRequest (com.aliyun.oss.model.SetBucketWebsiteRequest)5 Test (org.junit.Test)5 BucketWebsiteResult (com.aliyun.oss.model.BucketWebsiteResult)4 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 ParseException (java.text.ParseException)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1