Search in sources :

Example 1 with Owner

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

the class ResponseParsers method parseGetBucketInfo.

/**
 * Unmarshall get bucket info response body to bucket info.
 */
public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        Element bucketElem = root.getChild("Bucket");
        BucketInfo bucketInfo = new BucketInfo();
        // owner
        Bucket bucket = new Bucket();
        String id = bucketElem.getChild("Owner").getChildText("ID");
        String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        bucket.setOwner(owner);
        // bucket
        bucket.setName(bucketElem.getChildText("Name"));
        bucket.setLocation(bucketElem.getChildText("Location"));
        bucket.setExtranetEndpoint(bucketElem.getChildText("ExtranetEndpoint"));
        bucket.setIntranetEndpoint(bucketElem.getChildText("IntranetEndpoint"));
        bucket.setCreationDate(DateUtil.parseIso8601Date(bucketElem.getChildText("CreationDate")));
        if (bucketElem.getChild("StorageClass") != null) {
            bucket.setStorageClass(StorageClass.parse(bucketElem.getChildText("StorageClass")));
        }
        bucketInfo.setBucket(bucket);
        // acl
        String aclString = bucketElem.getChild("AccessControlList").getChildText("Grant");
        CannedAccessControlList acl = CannedAccessControlList.parse(aclString);
        bucketInfo.setCannedACL(acl);
        switch(acl) {
            case PublicRead:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.Read);
                break;
            case PublicReadWrite:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
                break;
            default:
                break;
        }
        return bucketInfo;
    } 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) BucketInfo(com.aliyun.oss.model.BucketInfo) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 2 with Owner

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

the class ResponseParsers method parseListObjects.

/**
 * Unmarshall list objects response body to object listing.
 */
@SuppressWarnings("unchecked")
public static ObjectListing parseListObjects(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        ObjectListing objectListing = new ObjectListing();
        objectListing.setBucketName(root.getChildText("Name"));
        objectListing.setMaxKeys(Integer.valueOf(root.getChildText("MaxKeys")));
        objectListing.setTruncated(Boolean.valueOf(root.getChildText("IsTruncated")));
        if (root.getChild("Prefix") != null) {
            String prefix = root.getChildText("Prefix");
            objectListing.setPrefix(isNullOrEmpty(prefix) ? null : prefix);
        }
        if (root.getChild("Marker") != null) {
            String marker = root.getChildText("Marker");
            objectListing.setMarker(isNullOrEmpty(marker) ? null : marker);
        }
        if (root.getChild("Delimiter") != null) {
            String delimiter = root.getChildText("Delimiter");
            objectListing.setDelimiter(isNullOrEmpty(delimiter) ? null : delimiter);
        }
        if (root.getChild("NextMarker") != null) {
            String nextMarker = root.getChildText("NextMarker");
            objectListing.setNextMarker(isNullOrEmpty(nextMarker) ? null : nextMarker);
        }
        if (root.getChild("EncodingType") != null) {
            String encodingType = root.getChildText("EncodingType");
            objectListing.setEncodingType(isNullOrEmpty(encodingType) ? null : encodingType);
        }
        List<Element> objectSummaryElems = root.getChildren("Contents");
        for (Element elem : objectSummaryElems) {
            OSSObjectSummary ossObjectSummary = new OSSObjectSummary();
            ossObjectSummary.setKey(elem.getChildText("Key"));
            ossObjectSummary.setETag(trimQuotes(elem.getChildText("ETag")));
            ossObjectSummary.setLastModified(DateUtil.parseIso8601Date(elem.getChildText("LastModified")));
            ossObjectSummary.setSize(Long.valueOf(elem.getChildText("Size")));
            ossObjectSummary.setStorageClass(elem.getChildText("StorageClass"));
            ossObjectSummary.setBucketName(objectListing.getBucketName());
            String id = elem.getChild("Owner").getChildText("ID");
            String displayName = elem.getChild("Owner").getChildText("DisplayName");
            ossObjectSummary.setOwner(new Owner(id, displayName));
            objectListing.addObjectSummary(ossObjectSummary);
        }
        List<Element> commonPrefixesElems = root.getChildren("CommonPrefixes");
        for (Element elem : commonPrefixesElems) {
            String prefix = elem.getChildText("Prefix");
            if (!isNullOrEmpty(prefix)) {
                objectListing.addCommonPrefix(prefix);
            }
        }
        return objectListing;
    } 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) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) Owner(com.aliyun.oss.model.Owner) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ObjectListing(com.aliyun.oss.model.ObjectListing) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 3 with Owner

use of com.aliyun.oss.model.Owner in project druid by druid-io.

the class OssTaskLogsTest method testPushInternal.

private List<Grant> testPushInternal(boolean disableAcl, String ownerId, String ownerDisplayName) throws Exception {
    EasyMock.expect(ossClient.putObject(EasyMock.anyObject())).andReturn(new PutObjectResult()).once();
    AccessControlList aclExpected = new AccessControlList();
    aclExpected.setOwner(new Owner(ownerId, ownerDisplayName));
    EasyMock.expect(ossClient.getBucketAcl(TEST_BUCKET)).andReturn(aclExpected).once();
    EasyMock.expect(ossClient.putObject(EasyMock.anyObject(PutObjectRequest.class))).andReturn(new PutObjectResult()).once();
    EasyMock.replay(ossClient);
    OssTaskLogsConfig config = new OssTaskLogsConfig();
    config.setDisableAcl(disableAcl);
    config.setBucket(TEST_BUCKET);
    CurrentTimeMillisSupplier timeSupplier = new CurrentTimeMillisSupplier();
    OssInputDataConfig inputDataConfig = new OssInputDataConfig();
    OssTaskLogs taskLogs = new OssTaskLogs(ossClient, config, inputDataConfig, timeSupplier);
    String taskId = "index_test-datasource_2019-06-18T13:30:28.887Z";
    File logFile = tempFolder.newFile("test_log_file");
    taskLogs.pushTaskLog(taskId, logFile);
    return new ArrayList<>(aclExpected.getGrants());
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) Owner(com.aliyun.oss.model.Owner) PutObjectResult(com.aliyun.oss.model.PutObjectResult) CurrentTimeMillisSupplier(org.apache.druid.common.utils.CurrentTimeMillisSupplier) ArrayList(java.util.ArrayList) File(java.io.File)

Example 4 with Owner

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

the class ResponseParsers method parseGetObjectAcl.

/**
 * Unmarshall object acl response body to object ACL.
 */
public static ObjectAcl parseGetObjectAcl(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        ObjectAcl acl = new ObjectAcl();
        String id = root.getChild("Owner").getChildText("ID");
        String displayName = root.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        acl.setOwner(owner);
        String grantString = root.getChild("AccessControlList").getChildText("Grant");
        acl.setPermission(ObjectPermission.parsePermission(grantString));
        return acl;
    } 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) ObjectAcl(com.aliyun.oss.model.ObjectAcl) 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)

Example 5 with Owner

use of com.aliyun.oss.model.Owner 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

Owner (com.aliyun.oss.model.Owner)6 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)5 ParseException (java.text.ParseException)5 Element (org.jdom.Element)5 JDOMParseException (org.jdom.input.JDOMParseException)5 AccessControlList (com.aliyun.oss.model.AccessControlList)2 Bucket (com.aliyun.oss.model.Bucket)2 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)2 ArrayList (java.util.ArrayList)2 BucketInfo (com.aliyun.oss.model.BucketInfo)1 BucketList (com.aliyun.oss.model.BucketList)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 File (java.io.File)1 CurrentTimeMillisSupplier (org.apache.druid.common.utils.CurrentTimeMillisSupplier)1