Search in sources :

Example 1 with CnameConfiguration

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

the class BucketCnameTest method testNormalAddBucketCname.

@Test
@SuppressWarnings("deprecation")
public void testNormalAddBucketCname() {
    final String bucketName = "normal-add-bucket-cname";
    Date curDate;
    try {
        ossClient.createBucket(bucketName);
        // set cname
        ossClient.addBucketCname(new AddBucketCnameRequest(bucketName).withDomain(domains[0]));
        curDate = new Date(System.currentTimeMillis());
        waitForCacheExpiration(5);
        List<CnameConfiguration> cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 1);
        Assert.assertEquals(cnames.get(0).getDomain(), domains[0]);
        Assert.assertEquals(cnames.get(0).getStatus(), CnameConfiguration.CnameStatus.Enabled);
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getYear(), curDate.getYear());
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getMonth(), curDate.getMonth());
        Assert.assertEquals(cnames.get(0).getLastMofiedTime().getDay(), curDate.getDay());
        System.out.println(cnames.get(0));
        ossClient.deleteBucketCname(bucketName, domains[0]);
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 0);
        // set multi cname
        for (String domain : domains) {
            AddBucketCnameRequest request = new AddBucketCnameRequest(bucketName);
            request.setDomain(domain);
            ossClient.addBucketCname(request);
        }
        curDate = new Date(System.currentTimeMillis());
        waitForCacheExpiration(5);
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), domains.length);
        for (int i = 0; i < cnames.size(); i++) {
            System.out.println(cnames.get(i));
            Assert.assertEquals(cnames.get(i).getDomain(), domains[i]);
            Assert.assertEquals(cnames.get(0).getStatus(), CnameConfiguration.CnameStatus.Enabled);
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getYear(), curDate.getYear());
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getMonth(), curDate.getMonth());
            Assert.assertEquals(cnames.get(0).getLastMofiedTime().getDay(), curDate.getDay());
        }
        for (String domain : domains) {
            DeleteBucketCnameRequest req = new DeleteBucketCnameRequest(bucketName);
            req.setDomain(domain);
            ossClient.deleteBucketCname(req);
        }
        cnames = ossClient.getBucketCname(bucketName);
        Assert.assertEquals(cnames.size(), 0);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : DeleteBucketCnameRequest(com.aliyun.oss.model.DeleteBucketCnameRequest) CnameConfiguration(com.aliyun.oss.model.CnameConfiguration) AddBucketCnameRequest(com.aliyun.oss.model.AddBucketCnameRequest) Date(java.util.Date) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with CnameConfiguration

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

the class ResponseParsers method parseGetBucketCname.

/**
 * Unmarshall get bucket cname response body to cname configuration.
 */
@SuppressWarnings("unchecked")
public static List<CnameConfiguration> parseGetBucketCname(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        List<CnameConfiguration> cnames = new ArrayList<CnameConfiguration>();
        List<Element> cnameElements = root.getChildren("Cname");
        for (Element cnameElem : cnameElements) {
            CnameConfiguration cname = new CnameConfiguration();
            cname.setDomain(cnameElem.getChildText("Domain"));
            cname.setStatus(CnameConfiguration.CnameStatus.valueOf(cnameElem.getChildText("Status")));
            cname.setLastMofiedTime(DateUtil.parseIso8601Date(cnameElem.getChildText("LastModified")));
            if (cnameElem.getChildText("IsPurgeCdnCache") != null) {
                boolean purgeCdnCache = Boolean.valueOf(cnameElem.getChildText("IsPurgeCdnCache"));
                cname.setPurgeCdnCache(purgeCdnCache);
            }
            cnames.add(cname);
        }
        return cnames;
    } 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) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) CnameConfiguration(com.aliyun.oss.model.CnameConfiguration) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

CnameConfiguration (com.aliyun.oss.model.CnameConfiguration)2 OSSException (com.aliyun.oss.OSSException)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AddBucketCnameRequest (com.aliyun.oss.model.AddBucketCnameRequest)1 DeleteBucketCnameRequest (com.aliyun.oss.model.DeleteBucketCnameRequest)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1 Test (org.junit.Test)1