Search in sources :

Example 1 with TagSet

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

the class BucketTaggingTest method testSetBucketTagging.

@Test
public void testSetBucketTagging() {
    try {
        SetBucketTaggingRequest request = new SetBucketTaggingRequest(bucketName);
        request.setTag("tk1", "tv1");
        request.setTag("tk2", "tv2");
        ossClient.setBucketTagging(request);
        TagSet tagSet = ossClient.getBucketTagging(new GenericRequest(bucketName));
        Assert.assertEquals(tagSet.getRequestId().length(), REQUEST_ID_LEN);
        Map<String, String> tags = tagSet.getAllTags();
        Assert.assertEquals(2, tags.size());
        Assert.assertTrue(tags.containsKey("tk1"));
        Assert.assertTrue(tags.containsKey("tk2"));
        ossClient.deleteBucketTagging(new GenericRequest(bucketName));
        waitForCacheExpiration(5);
        tagSet = ossClient.getBucketTagging(new GenericRequest(bucketName));
        Assert.assertEquals(tagSet.getRequestId().length(), REQUEST_ID_LEN);
        tags = tagSet.getAllTags();
        Assert.assertTrue(tags.isEmpty());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SetBucketTaggingRequest(com.aliyun.oss.model.SetBucketTaggingRequest) TagSet(com.aliyun.oss.model.TagSet) GenericRequest(com.aliyun.oss.model.GenericRequest) Test(org.junit.Test)

Example 2 with TagSet

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

the class ResponseParsers method parseGetBucketTagging.

/**
 * Unmarshall get bucket tagging response body to cors rules.
 */
@SuppressWarnings("unchecked")
public static TagSet parseGetBucketTagging(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        TagSet tagSet = new TagSet();
        List<Element> tagElems = root.getChild("TagSet").getChildren("Tag");
        for (Element tagElem : tagElems) {
            String key = null;
            String value = null;
            if (tagElem.getChild("Key") != null) {
                key = tagElem.getChildText("Key");
            }
            if (tagElem.getChild("Value") != null) {
                value = tagElem.getChildText("Value");
            }
            tagSet.setTag(key, value);
        }
        return tagSet;
    } 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) TagSet(com.aliyun.oss.model.TagSet) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

TagSet (com.aliyun.oss.model.TagSet)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 GenericRequest (com.aliyun.oss.model.GenericRequest)1 SetBucketTaggingRequest (com.aliyun.oss.model.SetBucketTaggingRequest)1 ParseException (java.text.ParseException)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1 Test (org.junit.Test)1