Search in sources :

Example 6 with ResponseParseException

use of com.aliyun.oss.common.parser.ResponseParseException in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseBucketWebsite.

/**
 * Unmarshall get bucket website response body to corresponding result.
 */
@SuppressWarnings("unchecked")
public static BucketWebsiteResult parseBucketWebsite(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        BucketWebsiteResult result = new BucketWebsiteResult();
        if (root.getChild("IndexDocument") != null) {
            result.setIndexDocument(root.getChild("IndexDocument").getChildText("Suffix"));
        }
        if (root.getChild("ErrorDocument") != null) {
            result.setErrorDocument(root.getChild("ErrorDocument").getChildText("Key"));
        }
        if (root.getChild("RoutingRules") != null) {
            List<Element> ruleElements = root.getChild("RoutingRules").getChildren("RoutingRule");
            for (Element ruleElem : ruleElements) {
                RoutingRule rule = new RoutingRule();
                rule.setNumber(Integer.parseInt(ruleElem.getChildText("RuleNumber")));
                Element condElem = ruleElem.getChild("Condition");
                if (condElem != null) {
                    rule.getCondition().setKeyPrefixEquals(condElem.getChildText("KeyPrefixEquals"));
                    if (condElem.getChild("HttpErrorCodeReturnedEquals") != null) {
                        rule.getCondition().setHttpErrorCodeReturnedEquals(Integer.parseInt(condElem.getChildText("HttpErrorCodeReturnedEquals")));
                    }
                }
                Element redirectElem = ruleElem.getChild("Redirect");
                if (redirectElem.getChild("RedirectType") != null) {
                    rule.getRedirect().setRedirectType(RoutingRule.RedirectType.parse(redirectElem.getChildText("RedirectType")));
                }
                rule.getRedirect().setHostName(redirectElem.getChildText("HostName"));
                if (redirectElem.getChild("Protocol") != null) {
                    rule.getRedirect().setProtocol(RoutingRule.Protocol.parse(redirectElem.getChildText("Protocol")));
                }
                rule.getRedirect().setReplaceKeyPrefixWith(redirectElem.getChildText("ReplaceKeyPrefixWith"));
                rule.getRedirect().setReplaceKeyWith(redirectElem.getChildText("ReplaceKeyWith"));
                if (redirectElem.getChild("HttpRedirectCode") != null) {
                    rule.getRedirect().setHttpRedirectCode(Integer.parseInt(redirectElem.getChildText("HttpRedirectCode")));
                }
                rule.getRedirect().setMirrorURL(redirectElem.getChildText("MirrorURL"));
                rule.getRedirect().setMirrorSecondaryURL(redirectElem.getChildText("MirrorURLSlave"));
                rule.getRedirect().setMirrorProbeURL(redirectElem.getChildText("MirrorURLProbe"));
                if (redirectElem.getChildText("MirrorPassQueryString") != null) {
                    rule.getRedirect().setPassQueryString(Boolean.valueOf(redirectElem.getChildText("MirrorPassQueryString")));
                }
                if (redirectElem.getChildText("MirrorPassOriginalSlashes") != null) {
                    rule.getRedirect().setPassOriginalSlashes(Boolean.valueOf(redirectElem.getChildText("MirrorPassOriginalSlashes")));
                }
                result.AddRoutingRule(rule);
            }
        }
        return result;
    } 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) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) RoutingRule(com.aliyun.oss.model.RoutingRule) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 7 with ResponseParseException

use of com.aliyun.oss.common.parser.ResponseParseException 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 8 with ResponseParseException

use of com.aliyun.oss.common.parser.ResponseParseException in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseGetUdfImageInfo.

/**
 * Unmarshall get udf info response body to udf image info list.
 */
@SuppressWarnings("unchecked")
public static List<UdfImageInfo> parseGetUdfImageInfo(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        List<UdfImageInfo> udfImages = new ArrayList<UdfImageInfo>();
        if (root.getChild("Item") != null) {
            List<Element> udfImageElems = root.getChildren("Item");
            for (Element elem : udfImageElems) {
                Integer version = Integer.valueOf(elem.getChildText("Version"));
                String status = elem.getChildText("Status");
                String region = elem.getChildText("CanonicalRegion");
                String desc = elem.getChildText("Description");
                Date date = DateUtil.parseIso8601Date(elem.getChildText("CreationDate"));
                udfImages.add(new UdfImageInfo(version, status, desc, region, date));
            }
        }
        return udfImages;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : BigInteger(java.math.BigInteger) JDOMParseException(org.jdom.input.JDOMParseException) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) UdfImageInfo(com.aliyun.oss.model.UdfImageInfo) ArrayList(java.util.ArrayList) Date(java.util.Date) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 9 with ResponseParseException

use of com.aliyun.oss.common.parser.ResponseParseException in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseGetBucketReplicationProgress.

/**
 * Unmarshall get bucket replication response body to replication progress.
 */
public static BucketReplicationProgress parseGetBucketReplicationProgress(InputStream responseBody) throws ResponseParseException {
    try {
        BucketReplicationProgress progress = new BucketReplicationProgress();
        Element root = getXmlRootElement(responseBody);
        Element ruleElem = root.getChild("Rule");
        progress.setReplicationRuleID(ruleElem.getChildText("ID"));
        Element destination = ruleElem.getChild("Destination");
        progress.setTargetBucketName(destination.getChildText("Bucket"));
        progress.setTargetBucketLocation(destination.getChildText("Location"));
        progress.setReplicationStatus(ReplicationStatus.parse(ruleElem.getChildText("Status")));
        if (ruleElem.getChildText("HistoricalObjectReplication").equals("enabled")) {
            progress.setEnableHistoricalObjectReplication(true);
        } else {
            progress.setEnableHistoricalObjectReplication(false);
        }
        Element progressElem = ruleElem.getChild("Progress");
        if (progressElem != null) {
            if (progressElem.getChild("HistoricalObject") != null) {
                progress.setHistoricalObjectProgress(Float.parseFloat(progressElem.getChildText("HistoricalObject")));
            }
            progress.setNewObjectProgress(DateUtil.parseIso8601Date(progressElem.getChildText("NewObject")));
        }
        return progress;
    } 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) BucketReplicationProgress(com.aliyun.oss.model.BucketReplicationProgress) 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 10 with ResponseParseException

use of com.aliyun.oss.common.parser.ResponseParseException in project aliyun-oss-java-sdk by aliyun.

the class ResponseParsers method parseObjectMetadata.

/**
 * Unmarshall object metadata from response headers.
 */
public static ObjectMetadata parseObjectMetadata(Map<String, String> headers) throws ResponseParseException {
    try {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        for (Iterator<String> it = headers.keySet().iterator(); it.hasNext(); ) {
            String key = it.next();
            if (key.indexOf(OSSHeaders.OSS_USER_METADATA_PREFIX) >= 0) {
                key = key.substring(OSSHeaders.OSS_USER_METADATA_PREFIX.length());
                objectMetadata.addUserMetadata(key, headers.get(OSSHeaders.OSS_USER_METADATA_PREFIX + key));
            } else if (key.equals(OSSHeaders.LAST_MODIFIED) || key.equals(OSSHeaders.DATE)) {
                try {
                    objectMetadata.setHeader(key, DateUtil.parseRfc822Date(headers.get(key)));
                } catch (ParseException pe) {
                    throw new ResponseParseException(pe.getMessage(), pe);
                }
            } else if (key.equals(OSSHeaders.CONTENT_LENGTH)) {
                Long value = Long.valueOf(headers.get(key));
                objectMetadata.setHeader(key, value);
            } else if (key.equals(OSSHeaders.ETAG)) {
                objectMetadata.setHeader(key, trimQuotes(headers.get(key)));
            } else {
                objectMetadata.setHeader(key, headers.get(key));
            }
        }
        return objectMetadata;
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Aggregations

ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)41 ParseException (java.text.ParseException)39 JDOMParseException (org.jdom.input.JDOMParseException)39 Element (org.jdom.Element)37 ArrayList (java.util.ArrayList)15 Date (java.util.Date)6 Owner (com.aliyun.oss.model.Owner)5 BigInteger (java.math.BigInteger)3 Bucket (com.aliyun.oss.model.Bucket)2 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)2 CannedUdfAcl (com.aliyun.oss.model.CannedUdfAcl)2 InstanceFlavor (com.aliyun.oss.model.InstanceFlavor)2 UdfApplicationInfo (com.aliyun.oss.model.UdfApplicationInfo)2 OSSException (com.aliyun.oss.OSSException)1 RequestSigner (com.aliyun.oss.common.auth.RequestSigner)1 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)1 RequestChecksumHanlder (com.aliyun.oss.common.comm.RequestChecksumHanlder)1 RequestHandler (com.aliyun.oss.common.comm.RequestHandler)1 RequestProgressHanlder (com.aliyun.oss.common.comm.RequestProgressHanlder)1 ResponseChecksumHandler (com.aliyun.oss.common.comm.ResponseChecksumHandler)1