Search in sources :

Example 1 with ReplicationAction

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

the class RequestMarshallers method joinRepliationAction.

private static String joinRepliationAction(List<ReplicationAction> actions) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (ReplicationAction action : actions) {
        if (!first) {
            sb.append(",");
        }
        sb.append(action);
        first = false;
    }
    return sb.toString();
}
Also used : ReplicationAction(com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction)

Example 2 with ReplicationAction

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

the class BucketReplicationTest method testNormalAddBucketReplicationWithAction.

@Test
public void testNormalAddBucketReplicationWithAction() throws ParseException {
    final String bucketName = "test-bucket-replication-action-10";
    try {
        ossClient.createBucket(bucketName);
        AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);
        request.setTargetBucketName(targetBucketName);
        request.setTargetBucketLocation(targetBucketLoc);
        List<String> prefixes = new ArrayList<String>();
        prefixes.add("image/");
        prefixes.add("video");
        request.setObjectPrefixList(prefixes);
        List<ReplicationAction> actions = new ArrayList<ReplicationAction>();
        actions.add(ReplicationAction.PUT);
        actions.add(ReplicationAction.DELETE);
        request.setReplicationActionList(actions);
        ossClient.addBucketReplication(request);
        List<ReplicationRule> rules = ossClient.getBucketReplication(bucketName);
        Assert.assertEquals(rules.size(), 1);
        ReplicationRule r0 = rules.get(0);
        Assert.assertEquals(r0.getReplicationRuleID().length(), "d6a8bfe3-56f6-42dd-9e7f-b4301d99b0ed".length());
        Assert.assertEquals(r0.getTargetBucketName(), targetBucketName);
        Assert.assertEquals(r0.getTargetBucketLocation(), targetBucketLoc);
        Assert.assertEquals(r0.isEnableHistoricalObjectReplication(), true);
        Assert.assertEquals(r0.getReplicationStatus(), ReplicationStatus.Starting);
        Assert.assertEquals(r0.getObjectPrefixList().size(), 2);
        Assert.assertEquals(r0.getReplicationActionList().size(), 2);
        ossClient.deleteBucketReplication(new DeleteBucketReplicationRequest(bucketName, r0.getReplicationRuleID()));
    } catch (OSSException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AddBucketReplicationRequest(com.aliyun.oss.model.AddBucketReplicationRequest) ArrayList(java.util.ArrayList) ReplicationAction(com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction) OSSException(com.aliyun.oss.OSSException) DeleteBucketReplicationRequest(com.aliyun.oss.model.DeleteBucketReplicationRequest) ReplicationRule(com.aliyun.oss.model.ReplicationRule) Test(org.junit.Test)

Example 3 with ReplicationAction

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

the class ResponseParsers method parseGetBucketReplication.

/**
 * Unmarshall get bucket replication response body to replication result.
 */
@SuppressWarnings("unchecked")
public static List<ReplicationRule> parseGetBucketReplication(InputStream responseBody) throws ResponseParseException {
    try {
        List<ReplicationRule> repRules = new ArrayList<ReplicationRule>();
        Element root = getXmlRootElement(responseBody);
        List<Element> ruleElems = root.getChildren("Rule");
        for (Element ruleElem : ruleElems) {
            ReplicationRule repRule = new ReplicationRule();
            repRule.setReplicationRuleID(ruleElem.getChildText("ID"));
            Element destination = ruleElem.getChild("Destination");
            repRule.setTargetBucketName(destination.getChildText("Bucket"));
            repRule.setTargetBucketLocation(destination.getChildText("Location"));
            repRule.setReplicationStatus(ReplicationStatus.parse(ruleElem.getChildText("Status")));
            if (ruleElem.getChildText("HistoricalObjectReplication").equals("enabled")) {
                repRule.setEnableHistoricalObjectReplication(true);
            } else {
                repRule.setEnableHistoricalObjectReplication(false);
            }
            if (ruleElem.getChild("PrefixSet") != null) {
                List<String> objectPrefixes = new ArrayList<String>();
                List<Element> prefixElems = ruleElem.getChild("PrefixSet").getChildren("Prefix");
                for (Element prefixElem : prefixElems) {
                    objectPrefixes.add(prefixElem.getText());
                }
                repRule.setObjectPrefixList(objectPrefixes);
            }
            if (ruleElem.getChild("Action") != null) {
                String[] actionStrs = ruleElem.getChildText("Action").split(",");
                List<ReplicationAction> repActions = new ArrayList<ReplicationAction>();
                for (String actionStr : actionStrs) {
                    repActions.add(ReplicationAction.parse(actionStr));
                }
                repRule.setReplicationActionList(repActions);
            }
            repRules.add(repRule);
        }
        return repRules;
    } 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) ArrayList(java.util.ArrayList) ReplicationRule(com.aliyun.oss.model.ReplicationRule) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ReplicationAction(com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction)

Aggregations

ReplicationAction (com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction)3 ReplicationRule (com.aliyun.oss.model.ReplicationRule)2 ArrayList (java.util.ArrayList)2 OSSException (com.aliyun.oss.OSSException)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AddBucketReplicationRequest (com.aliyun.oss.model.AddBucketReplicationRequest)1 DeleteBucketReplicationRequest (com.aliyun.oss.model.DeleteBucketReplicationRequest)1 ParseException (java.text.ParseException)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1 Test (org.junit.Test)1