Search in sources :

Example 16 with BucketList

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

the class ResponseParsers method parseListBucket.

/**
 * Unmarshall list bucket response body to bucket list.
 */
@SuppressWarnings("unchecked")
public static BucketList parseListBucket(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        BucketList bucketList = new BucketList();
        if (root.getChild("Prefix") != null) {
            bucketList.setPrefix(root.getChildText("Prefix"));
        }
        if (root.getChild("Marker") != null) {
            bucketList.setMarker(root.getChildText("Marker"));
        }
        if (root.getChild("MaxKeys") != null) {
            String value = root.getChildText("MaxKeys");
            bucketList.setMaxKeys(isNullOrEmpty(value) ? null : Integer.valueOf(value));
        }
        if (root.getChild("IsTruncated") != null) {
            String value = root.getChildText("IsTruncated");
            bucketList.setTruncated(isNullOrEmpty(value) ? false : Boolean.valueOf(value));
        }
        if (root.getChild("NextMarker") != null) {
            bucketList.setNextMarker(root.getChildText("NextMarker"));
        }
        Element ownerElem = root.getChild("Owner");
        String id = ownerElem.getChildText("ID");
        String displayName = ownerElem.getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        List<Bucket> buckets = new ArrayList<Bucket>();
        if (root.getChild("Buckets") != null) {
            List<Element> bucketElems = root.getChild("Buckets").getChildren("Bucket");
            for (Element e : bucketElems) {
                Bucket bucket = new Bucket();
                bucket.setOwner(owner);
                bucket.setName(e.getChildText("Name"));
                bucket.setLocation(e.getChildText("Location"));
                bucket.setCreationDate(DateUtil.parseIso8601Date(e.getChildText("CreationDate")));
                if (e.getChild("StorageClass") != null) {
                    bucket.setStorageClass(StorageClass.parse(e.getChildText("StorageClass")));
                }
                bucket.setExtranetEndpoint(e.getChildText("ExtranetEndpoint"));
                bucket.setIntranetEndpoint(e.getChildText("IntranetEndpoint"));
                buckets.add(bucket);
            }
        }
        bucketList.setBucketList(buckets);
        return bucketList;
    } 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) Owner(com.aliyun.oss.model.Owner) Bucket(com.aliyun.oss.model.Bucket) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

BucketList (com.aliyun.oss.model.BucketList)16 Test (org.junit.Test)9 OSSException (com.aliyun.oss.OSSException)8 Bucket (com.aliyun.oss.model.Bucket)6 CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)6 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)5 ArrayList (java.util.ArrayList)4 AccessControlList (com.aliyun.oss.model.AccessControlList)3 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)3 Grant (com.aliyun.oss.model.Grant)3 Ignore (org.junit.Ignore)3 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 Owner (com.aliyun.oss.model.Owner)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1 BeforeClass (org.junit.BeforeClass)1