Search in sources :

Example 6 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class ModifyFilterRulesTest method SetIncomingXMLRulesForEnvelopeCountComparison.

@Test
public void SetIncomingXMLRulesForEnvelopeCountComparison() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        // Construct a filter rule with 'address' test whose value is empty (null)
        FilterRule rule = new FilterRule("testSetIncomingXMLRulesForEnvelope", true);
        FilterTest.EnvelopeTest test = new FilterTest.EnvelopeTest();
        test.setHeader("to");
        test.setCountComparison("eq");
        test.setPart("all");
        test.setValue("1");
        test.setIndex(0);
        FilterTests tests = new FilterTests("anyof");
        tests.addTest(test);
        FilterAction action = new FilterAction.KeepAction();
        action.setIndex(0);
        rule.setFilterTests(tests);
        rule.addFilterAction(action);
        List<FilterRule> filterRuleList = new ArrayList<FilterRule>();
        filterRuleList.add(rule);
        // When the ModifyFilterRulesRequest is submitted from the Web client,
        // eventually this RuleManager.setIncomingXMLRules is called to convert
        // the request in JSON to the SIEVE rule text.
        RuleManager.setIncomingXMLRules(account, filterRuleList);
        // Verify that the saved zimbraMailSieveScript
        String sieve = account.getMailSieveScript();
        int result = sieve.indexOf("envelope :count \"eq\" :all :comparator \"i;ascii-numeric\" [\"to\"] \"1\"");
        Assert.assertNotSame(-1, result);
    } catch (Exception e) {
        fail("No exception should be thrown" + e);
    }
}
Also used : Account(com.zimbra.cs.account.Account) ArrayList(java.util.ArrayList) FilterRule(com.zimbra.soap.mail.type.FilterRule) ServiceException(com.zimbra.common.service.ServiceException) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterAction(com.zimbra.soap.mail.type.FilterAction) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 7 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class SoapToSieve method handleNest.

// Constructing nested rule block with base indents which is for entire block.
private String handleNest(String baseIndents, NestedRule currentNestedRule) throws ServiceException {
    StringBuilder nestedIfBlock = new StringBuilder();
    FilterVariables filterVariables = currentNestedRule.getFilterVariables();
    nestedIfBlock.append(handleVariables(filterVariables, baseIndents));
    Sieve.Condition childCondition = Sieve.Condition.fromString(currentNestedRule.getFilterTests().getCondition());
    if (childCondition == null) {
        childCondition = Sieve.Condition.allof;
    }
    // assuming no disabled_if for child tests so far
    nestedIfBlock.append(baseIndents).append("if ");
    nestedIfBlock.append(childCondition).append(" (");
    // Handle tests
    // sort by index
    Map<Integer, String> index2childTest = new TreeMap<Integer, String>();
    for (FilterTest childTest : currentNestedRule.getFilterTests().getTests()) {
        String childResult = handleTest(childTest);
        if (childResult != null) {
            FilterUtil.addToMap(index2childTest, childTest.getIndex(), childResult);
        }
    }
    Joiner.on(",\n  " + baseIndents).appendTo(nestedIfBlock, index2childTest.values());
    nestedIfBlock.append(") {\n");
    // Handle actions
    // sort by index
    Map<Integer, String> index2childAction = new TreeMap<Integer, String>();
    List<FilterAction> childActions = currentNestedRule.getFilterActions();
    String variables = "";
    if (childActions != null) {
        for (FilterAction childAction : childActions) {
            if (childAction instanceof FilterVariables) {
                FilterVariables var = (FilterVariables) childAction;
                variables = handleVariables(var, baseIndents + "    ");
            } else {
                String childResult = handleAction(childAction);
                if (childResult != null) {
                    FilterUtil.addToMap(index2childAction, childAction.getIndex(), childResult);
                }
            }
        }
        for (String childAction : index2childAction.values()) {
            nestedIfBlock.append(baseIndents);
            nestedIfBlock.append("    ").append(childAction).append(END_OF_LINE);
        }
        if (!variables.isEmpty()) {
            nestedIfBlock.append(variables);
        }
    }
    // Handle nest
    if (currentNestedRule.getChild() != null) {
        nestedIfBlock.append(handleNest(baseIndents + "    ", currentNestedRule.getChild()));
    }
    if (childActions == null && currentNestedRule.getChild() == null) {
        // If there is no more nested rule, there should be at least one action.
        throw ServiceException.INVALID_REQUEST("Missing action", null);
    }
    nestedIfBlock.append(baseIndents);
    nestedIfBlock.append("}\n");
    return nestedIfBlock.toString();
}
Also used : FilterTest(com.zimbra.soap.mail.type.FilterTest) Sieve(com.zimbra.common.filter.Sieve) TreeMap(java.util.TreeMap) FilterVariables(com.zimbra.soap.mail.type.FilterVariables) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 8 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class SoapToSieve method handleRule.

private void handleRule(FilterRule rule) throws ServiceException {
    String name = rule.getName();
    boolean active = rule.isActive();
    // Rule name
    buffer.append("# ").append(name).append('\n');
    FilterVariables filterVariables = rule.getFilterVariables();
    buffer.append(handleVariables(filterVariables, null));
    FilterTests tests = rule.getFilterTests();
    Sieve.Condition condition = Sieve.Condition.fromString(tests.getCondition());
    if (condition == null) {
        condition = Sieve.Condition.allof;
    }
    if (active) {
        buffer.append("if ");
    } else {
        buffer.append("disabled_if ");
    }
    buffer.append(condition).append(" (");
    // Handle tests
    // sort by index
    Map<Integer, String> index2test = new TreeMap<Integer, String>();
    for (FilterTest test : tests.getTests()) {
        String result = handleTest(test);
        if (result != null) {
            FilterUtil.addToMap(index2test, test.getIndex(), result);
        }
    }
    Joiner.on(",\n  ").appendTo(buffer, index2test.values());
    buffer.append(") {\n");
    // Handle actions
    // sort by index
    Map<Integer, String> index2action = new TreeMap<Integer, String>();
    List<FilterAction> filterActions = rule.getFilterActions();
    String variables = "";
    if (filterActions != null) {
        for (FilterAction action : filterActions) {
            if (action instanceof FilterVariables) {
                FilterVariables var = (FilterVariables) action;
                variables = handleVariables(var, "    ");
            } else {
                String result = handleAction(action);
                if (result != null) {
                    FilterUtil.addToMap(index2action, action.getIndex(), result);
                }
            }
        }
        for (String action : index2action.values()) {
            buffer.append("    ").append(action).append(END_OF_LINE);
        }
        if (!variables.isEmpty()) {
            buffer.append(variables);
        }
    }
    NestedRule child = rule.getChild();
    // Handle nested rule
    if (child != null) {
        // first nested block's indent is "    "
        String nestedRuleBlock = handleNest("    ", child);
        buffer.append(nestedRuleBlock);
    }
    if (filterActions == null && child == null) {
        // If there is no more nested rule, there should be at least one action.
        throw ServiceException.INVALID_REQUEST("Missing action", null);
    }
    buffer.append("}\n");
}
Also used : NestedRule(com.zimbra.soap.mail.type.NestedRule) TreeMap(java.util.TreeMap) FilterVariables(com.zimbra.soap.mail.type.FilterVariables) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) Sieve(com.zimbra.common.filter.Sieve) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Aggregations

FilterAction (com.zimbra.soap.mail.type.FilterAction)8 FilterTest (com.zimbra.soap.mail.type.FilterTest)7 FilterTests (com.zimbra.soap.mail.type.FilterTests)6 FilterRule (com.zimbra.soap.mail.type.FilterRule)5 Test (org.junit.Test)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 FilterVariables (com.zimbra.soap.mail.type.FilterVariables)3 ArrayList (java.util.ArrayList)3 Sieve (com.zimbra.common.filter.Sieve)2 TreeMap (java.util.TreeMap)2 Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 GetFilterRulesResponse (com.zimbra.soap.mail.message.GetFilterRulesResponse)1 NestedRule (com.zimbra.soap.mail.type.NestedRule)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1