use of com.zimbra.soap.mail.type.FilterRule 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;
}
use of com.zimbra.soap.mail.type.FilterRule in project zm-mailbox by Zimbra.
the class GetFilterRulesTest method testBug1281_SingleRuleElsif.
@Test
public void testBug1281_SingleRuleElsif() throws Exception {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
RuleManager.clearCachedRules(acct);
// set One rule with elsif
String ss = "require [\"fileinto\", \"log\"];\n";
ss += "if anyof address :is \"from\" \"p2@zqa-380.eng.zimbra.com\" {\n";
ss += "fileinto \"FromP2\"; log \"Move message to FromP2 folder\"; }\n";
ss += "elsif anyof (header :contains \"subject\" [\"automation\", \"nunit\"]) \n";
ss += "{ redirect \"qa-automation@zqa-380.eng.zimbra.com\"; log \"Forward message to qa-automation DL\"; }\n";
acct.setMailSieveScript(ss);
try {
List<FilterRule> ids = RuleManager.getIncomingRulesAsXML(acct);
} catch (Exception e) {
e.printStackTrace();
fail("No exception should be thrown");
}
}
use of com.zimbra.soap.mail.type.FilterRule 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);
}
}
use of com.zimbra.soap.mail.type.FilterRule in project zm-mailbox by Zimbra.
the class SoapToSieve method getSieveScript.
public String getSieveScript() throws ServiceException {
if (buffer == null) {
buffer = new StringBuilder();
buffer.append("require [\"fileinto\", \"reject\", \"tag\", \"flag\", \"variables\", \"log\", \"enotify\"]" + END_OF_LINE);
for (FilterRule rule : rules) {
buffer.append('\n');
handleRule(rule);
}
}
return buffer.toString();
}
use of com.zimbra.soap.mail.type.FilterRule in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method bug65572_BooleanAndXmlElements.
/**
*
{
"filterRules": [{
"filterRule": [{
"name": "filter.bug65572",
"active": false,
"filterTests": [{
"condition": "anyof",
"headerTest": [{
"index": 0,
"header": "X-Spam-Score",
"caseSensitive": false,
"stringComparison": "contains",
"value": "0"
}]
}],
"filterActions": [{
"actionFlag": [{
"flagName": "flagged",
"index": 0
}],
"actionStop": [{
"index": 1
}]
}]
}]
}],
"_jsns": "urn:zimbraMail"
}
*/
/**
* This also tests {@link XmlElements} - It is used in {@link FilterTests}
* @throws Exception
*/
@Test
public void bug65572_BooleanAndXmlElements() throws Exception {
Element legacyXmlElem = mkFilterRulesResponse(XMLElement.mFactory);
Element legacyJsonElem = mkFilterRulesResponse(JSONElement.mFactory);
GetFilterRulesResponse jaxb = new GetFilterRulesResponse();
FilterTests tests = FilterTests.createForCondition("anyof");
FilterTest.HeaderTest hdrTest = FilterTest.HeaderTest.createForIndexNegative(0, null);
hdrTest.setHeaders("X-Spam-Score");
hdrTest.setCaseSensitive(false);
hdrTest.setStringComparison("contains");
hdrTest.setValue("0");
tests.addTest(hdrTest);
FilterAction.FlagAction flagAction = new FilterAction.FlagAction("flagged");
flagAction.setIndex(0);
FilterAction.StopAction stopAction = new FilterAction.StopAction();
stopAction.setIndex(1);
FilterRule rule1 = FilterRule.createForNameFilterTestsAndActiveSetting("filter.bug65572", tests, false);
rule1.addFilterAction(flagAction);
rule1.addFilterAction(stopAction);
jaxb.addFilterRule(rule1);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
logDebug("legacyXMLElement ---> prettyPrint\n%1$s", legacyXmlElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
// Attribute Ordering not reliable: Assert.assertEquals("XML", legacyXmlElem.prettyPrint(), xmlElem.prettyPrint());
Element xmlFr = xmlElem.getElement(MailConstants.E_FILTER_RULES).getElement(MailConstants.E_FILTER_RULE);
Assert.assertEquals("XMLElement from JAXB filter rule name", "filter.bug65572", xmlFr.getAttribute(MailConstants.A_NAME));
Assert.assertEquals("XMLElement from JAXB filter rule active", false, xmlFr.getAttributeBool(MailConstants.A_ACTIVE));
Element xmlFT = xmlFr.getElement(MailConstants.E_FILTER_TESTS);
Assert.assertEquals("XMLElement from JAXB filter tests condition", "anyof", xmlFT.getAttribute(MailConstants.A_CONDITION));
Element xmlHdrT = xmlFT.getElement(MailConstants.E_HEADER_TEST);
Assert.assertEquals("XMLElement from JAXB filter hdr test index", 0, xmlHdrT.getAttributeInt(MailConstants.A_INDEX));
Assert.assertEquals("XMLElement from JAXB filter hdr test hdr", "X-Spam-Score", xmlHdrT.getAttribute(MailConstants.A_HEADER));
Assert.assertEquals("XMLElement from JAXB filter hdr test caseSense", false, xmlHdrT.getAttributeBool(MailConstants.A_CASE_SENSITIVE));
Assert.assertEquals("XMLElement from JAXB filter hdr test comparison", "contains", xmlHdrT.getAttribute(MailConstants.A_STRING_COMPARISON));
Assert.assertEquals("XMLElement from JAXB filter hdr test value", 0, xmlHdrT.getAttributeInt(MailConstants.A_VALUE));
Element xmlFA = xmlFr.getElement(MailConstants.E_FILTER_ACTIONS);
Element xmlFlag = xmlFA.getElement(MailConstants.E_ACTION_FLAG);
Assert.assertEquals("XMLElement from JAXB action flag name", "flagged", xmlFlag.getAttribute(MailConstants.A_FLAG_NAME));
Assert.assertEquals("XMLElement from JAXB action flag index", 0, xmlFlag.getAttributeInt(MailConstants.A_INDEX));
Element xmlStop = xmlFA.getElement(MailConstants.E_ACTION_STOP);
Assert.assertEquals("XMLElement from JAXB action stop index", 1, xmlStop.getAttributeInt(MailConstants.A_INDEX));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.GET_FILTER_RULES_RESPONSE);
logDebug("GetFilterRulesResponse legacyJSONElement ---> prettyPrint\n%1$s", legacyJsonElem.prettyPrint());
logDebug("GetFilterRulesResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSON", legacyJsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
GetFilterRulesResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetFilterRulesResponse.class);
List<FilterRule> rules = roundtripped.getFilterRules();
Assert.assertEquals("num roundtripped rules", 1, rules.size());
FilterRule rtRule = rules.get(0);
Assert.assertEquals("roundtripped rule name", "filter.bug65572", rtRule.getName());
Assert.assertEquals("roundtripped rule active setting", false, rtRule.isActive());
Assert.assertEquals("roundtripped rule action count", 2, rtRule.getActionCount());
FilterTests rtTests = rtRule.getFilterTests();
Assert.assertEquals("roundtripped filterTests condition", "anyof", rtTests.getCondition());
List<FilterTest> rtFilterTests = rtTests.getTests();
Assert.assertEquals("num roundtripped filter tests", 1, rtFilterTests.size());
FilterTest.HeaderTest rtHdrTest = (FilterTest.HeaderTest) rtFilterTests.get(0);
Assert.assertEquals("roundtripped header test index", 0, rtHdrTest.getIndex());
Assert.assertEquals("roundtripped header test header", "X-Spam-Score", rtHdrTest.getHeaders());
Assert.assertEquals("roundtripped header test caseSens", false, rtHdrTest.isCaseSensitive());
Assert.assertEquals("roundtripped header test stringComparison", "contains", rtHdrTest.getStringComparison());
Assert.assertEquals("roundtripped header test value", "0", rtHdrTest.getValue());
List<FilterAction> rtActions = rtRule.getFilterActions();
Assert.assertEquals("num roundtripped actions", 2, rtActions.size());
FilterAction.FlagAction rtFlagAction = (FilterAction.FlagAction) rtActions.get(0);
Assert.assertEquals("roundtripped FlagAction name", "flagged", rtFlagAction.getFlag());
Assert.assertEquals("roundtripped FlagAction index", 0, rtFlagAction.getIndex());
FilterAction.StopAction rtStopAction = (FilterAction.StopAction) rtActions.get(1);
Assert.assertEquals("roundtripped StopAction index", 1, rtStopAction.getIndex());
}
Aggregations