Search in sources :

Example 1 with FilterTests

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

the class ModifyFilterRulesTest method SetIncomingXMLRulesForEnvelope.

@Test
public void SetIncomingXMLRulesForEnvelope() {
    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.setStringComparison("is");
        test.setPart("all");
        test.setValue("u1@zimbra.com");
        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 :all :is :comparator \"i;ascii-casemap\" [\"to\"] \"u1@zimbra.com\"");
        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 2 with FilterTests

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

the class ZFilterRule method toJAXB.

FilterRule toJAXB() {
    FilterRule rule = new FilterRule(name, active);
    FilterTests tests = new FilterTests(allConditions ? "allof" : "anyof");
    int index = 0;
    for (ZFilterCondition condition : conditions) {
        FilterTest test = condition.toJAXB();
        test.setIndex(index++);
        tests.addTest(test);
    }
    rule.setFilterTests(tests);
    index = 0;
    for (ZFilterAction zaction : actions) {
        FilterAction action = zaction.toJAXB();
        action.setIndex(index++);
        rule.addFilterAction(action);
    }
    return rule;
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 3 with FilterTests

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

the class JaxbToJsonTest method inviteTestMethods.

/**
 * Tests a list of strings.  Was using a JSON Serializer called ContentListSerializer but the ObjectMapper we
 * use for Zimbra JSON now handles lists of strings this way by default.  GetFilterRules uses JAXB rather than
 * Element already.
 * Similar situation using Element based code for GetDistributionListMembers response used multiple calls to:
 *     parent.addNonUniqueElement(AccountConstants.E_DLM).setText(member);
 * Desired JSON :
 *      {
 *        "condition": "anyof",
 *        "inviteTest": [{
 *            "index": 0,
 *            "method": [
 *              {
 *                "_content": "REQUEST"
 *              },
 *              {
 *                "_content": "REPLY"
 *              },
 *              {
 *                "_content": "CANCEL"
 *              }]
 *          }],
 *        "_jsns": "urn:zimbraMail"
 *      }
 */
@Test
public void inviteTestMethods() throws Exception {
    FilterTests tests = FilterTests.createForCondition("anyof");
    FilterTest.InviteTest inviteTest = new FilterTest.InviteTest();
    inviteTest.addMethod("REQUEST");
    inviteTest.addMethod("REPLY");
    inviteTest.addMethod("CANCEL");
    tests.addTest(inviteTest);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tests, QName.get(MailConstants.E_FILTER_TESTS, MailConstants.NAMESPACE));
    logDebug("filterTests JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    FilterTests roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, FilterTests.class);
    FilterTest.InviteTest rtInviteTest = (FilterTest.InviteTest) roundtripped.getTests().get(0);
    Assert.assertEquals("roundtripped num methods", 3, rtInviteTest.getMethods().size());
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 4 with FilterTests

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

the class SieveToSoap method initRule.

private void initRule(RuleProperties props) {
    if (!isNestedRule()) {
        initCurrentRule(props);
    } else {
        // set new Nested Rule instance as child of current one.
        FilterTests tests = props.getCondition() != null ? new FilterTests(props.getCondition().toString()) : new FilterTests(null);
        NestedRule nestedRule = new NestedRule(tests);
        if (currentNestedRule != null) {
            // some nested rule has been already processed
            // set it as child of previous one
            currentNestedRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentNestedRule.addFilterAction(new FilterVariables(actionVariables));
            }
        } else {
            // set it as child of root rule
            if (null == currentRule) {
                initCurrentRule(props);
            }
            currentRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentRule.addFilterAction(new FilterVariables(actionVariables));
            }
        }
        currentNestedRule = nestedRule;
        actionVariables = null;
    }
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) NestedRule(com.zimbra.soap.mail.type.NestedRule) FilterVariables(com.zimbra.soap.mail.type.FilterVariables)

Example 5 with FilterTests

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

the class SoapToSieve method handleRule.

private void handleRule(FilterRule rule, boolean isAdminScript) 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();
    if (hasTest(tests)) {
        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, isAdminScript);
                if (result != null) {
                    FilterUtil.addToMap(index2action, action.getIndex(), result);
                }
            }
        }
        for (String action : index2action.values()) {
            if (hasTest(tests)) {
                buffer.append("    ");
            }
            buffer.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, isAdminScript);
        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);
    }
    if (hasTest(tests)) {
        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

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