use of com.aliyun.oss.model.SetBucketWebsiteRequest 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);
}
}
use of com.aliyun.oss.model.SetBucketWebsiteRequest in project aliyun-oss-java-sdk by aliyun.
the class BucketWebsiteTest method testNormalSetBucketWebsite.
@Test
public void testNormalSetBucketWebsite() {
final String bucketName = "normal-set-bucket-website";
final String indexDocument = "index.html";
final String errorDocument = "error.html";
try {
ossClient.createBucket(bucketName);
// Set both index document and error document
SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
request.setIndexDocument(indexDocument);
request.setErrorDocument(errorDocument);
ossClient.setBucketWebsite(request);
waitForCacheExpiration(5);
BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
Assert.assertEquals(indexDocument, result.getIndexDocument());
Assert.assertEquals(errorDocument, result.getErrorDocument());
Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
ossClient.deleteBucketWebsite(bucketName);
// Set index document only
request = new SetBucketWebsiteRequest(bucketName);
request.setIndexDocument(indexDocument);
request.setErrorDocument(null);
ossClient.setBucketWebsite(request);
waitForCacheExpiration(5);
result = ossClient.getBucketWebsite(bucketName);
Assert.assertEquals(indexDocument, result.getIndexDocument());
Assert.assertTrue(result.getErrorDocument() == null);
Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
ossClient.deleteBucketWebsite(bucketName);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.SetBucketWebsiteRequest in project aliyun-oss-java-sdk by aliyun.
the class BucketWebsiteTest method testUnormalSetBucketWebsite.
@Test
public void testUnormalSetBucketWebsite() {
final String bucketName = "unormal-set-bucket-website";
final String indexDocument = "index.html";
final String errorDocument = "error.html";
try {
ossClient.createBucket(bucketName);
// Set non-existent bucket
final String nonexistentBucket = "nonexistent-bucket";
try {
SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(nonexistentBucket);
request.setIndexDocument(indexDocument);
request.setErrorDocument(errorDocument);
ossClient.setBucketWebsite(request);
Assert.fail("Set bucket website should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
}
// Set index document null
try {
SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(nonexistentBucket);
request.setIndexDocument(null);
request.setErrorDocument(null);
ossClient.setBucketWebsite(request);
Assert.fail("Set bucket website should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
} finally {
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.SetBucketWebsiteRequest in project aliyun-oss-java-sdk by aliyun.
the class BucketWebsiteTest method testUnormalSetBucketWebsiteWithMirror.
@Test
public void testUnormalSetBucketWebsiteWithMirror() {
final String bucketName = "unormal-set-bucket-website-mirror";
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.Mirror);
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.Mirror);
request.setIndexDocument(indexDocument);
// rule MirrorURL invalid
try {
request.AddRoutingRule(rule);
Assert.fail("Add routing rule should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// rule MirrorURL invalid
request = new SetBucketWebsiteRequest(bucketName);
rule = new RoutingRule();
rule.setNumber(1);
rule.getCondition().setHttpErrorCodeReturnedEquals(404);
rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
rule.getRedirect().setMirrorURL("oss-test.aliyun-inc.com/mirror-test-source/");
request.setIndexDocument(indexDocument);
try {
request.AddRoutingRule(rule);
Assert.fail("Add routing rule should not be successful");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
// rule http error code invalid
request = new SetBucketWebsiteRequest(bucketName);
rule = new RoutingRule();
rule.setNumber(1);
rule.getCondition().setHttpErrorCodeReturnedEquals(403);
rule.getRedirect().setRedirectType(RoutingRule.RedirectType.Mirror);
rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test-source/");
request.setIndexDocument(indexDocument);
request.AddRoutingRule(rule);
try {
ossClient.setBucketWebsite(request);
Assert.fail("Set bucket website should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
ossClient.deleteBucket(bucketName);
}
}
Aggregations