Search in sources :

Example 6 with BucketReferer

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

the class BucketRefererTest method testNormalSetBucketReferer.

@Test
public void testNormalSetBucketReferer() {
    final String bucketName = "normal-set-bucket-referer-source";
    final String referer0 = "http://www.aliyun.com";
    final String referer1 = "https://www.aliyun.com";
    final String referer2 = "http://www.*.com";
    final String referer3 = "https://www.?.aliyuncs.com";
    try {
        ossClient.createBucket(bucketName);
        // Set non-empty referer list
        BucketReferer r = new BucketReferer();
        List<String> refererList = new ArrayList<String>();
        refererList.add(referer0);
        refererList.add(referer1);
        refererList.add(referer2);
        refererList.add(referer3);
        r.setRefererList(refererList);
        ossClient.setBucketReferer(bucketName, r);
        waitForCacheExpiration(5);
        r = ossClient.getBucketReferer(bucketName);
        List<String> returedRefererList = r.getRefererList();
        Assert.assertTrue(r.isAllowEmptyReferer());
        Assert.assertTrue(returedRefererList.contains(referer0));
        Assert.assertTrue(returedRefererList.contains(referer1));
        Assert.assertTrue(returedRefererList.contains(referer2));
        Assert.assertTrue(returedRefererList.contains(referer3));
        Assert.assertEquals(4, returedRefererList.size());
        Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
        // Set empty referer list
        r.clearRefererList();
        ossClient.setBucketReferer(bucketName, r);
        r = ossClient.getBucketReferer(bucketName);
        returedRefererList = r.getRefererList();
        Assert.assertTrue(r.isAllowEmptyReferer());
        Assert.assertEquals(0, returedRefererList.size());
        Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
        // Referer list not allowed to be empty
        refererList.clear();
        refererList.add(referer0);
        refererList.add(referer3);
        r.setRefererList(refererList);
        r.setAllowEmptyReferer(false);
        ossClient.setBucketReferer(bucketName, r);
        r = ossClient.getBucketReferer(bucketName);
        returedRefererList = r.getRefererList();
        Assert.assertFalse(r.isAllowEmptyReferer());
        Assert.assertTrue(returedRefererList.contains(referer0));
        Assert.assertTrue(returedRefererList.contains(referer3));
        Assert.assertEquals(2, returedRefererList.size());
        Assert.assertEquals(r.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : ArrayList(java.util.ArrayList) BucketReferer(com.aliyun.oss.model.BucketReferer) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 7 with BucketReferer

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

the class ResponseParsers method parseGetBucketReferer.

/**
 * Unmarshall get bucket referer response body to bucket referer list.
 */
@SuppressWarnings("unchecked")
public static BucketReferer parseGetBucketReferer(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        boolean allowEmptyReferer = Boolean.valueOf(root.getChildText("AllowEmptyReferer"));
        List<String> refererList = new ArrayList<String>();
        if (root.getChild("RefererList") != null) {
            Element refererListElem = root.getChild("RefererList");
            List<Element> refererElems = refererListElem.getChildren("Referer");
            if (refererElems != null && !refererElems.isEmpty()) {
                for (Element e : refererElems) {
                    refererList.add(e.getText());
                }
            }
        }
        return new BucketReferer(allowEmptyReferer, refererList);
    } 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) ArrayList(java.util.ArrayList) BucketReferer(com.aliyun.oss.model.BucketReferer) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

BucketReferer (com.aliyun.oss.model.BucketReferer)7 ArrayList (java.util.ArrayList)5 OSSException (com.aliyun.oss.OSSException)4 Test (org.junit.Test)4 OSSClient (com.aliyun.oss.OSSClient)1 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AccessControlList (com.aliyun.oss.model.AccessControlList)1 BucketLoggingResult (com.aliyun.oss.model.BucketLoggingResult)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 Grant (com.aliyun.oss.model.Grant)1 LifecycleRule (com.aliyun.oss.model.LifecycleRule)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 SetBucketCORSRequest (com.aliyun.oss.model.SetBucketCORSRequest)1 CORSRule (com.aliyun.oss.model.SetBucketCORSRequest.CORSRule)1 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)1 SetBucketLoggingRequest (com.aliyun.oss.model.SetBucketLoggingRequest)1 SetBucketWebsiteRequest (com.aliyun.oss.model.SetBucketWebsiteRequest)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1