use of com.zimbra.soap.mail.type.FilterTest 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.FilterTest 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");
}
}
use of com.zimbra.soap.mail.type.FilterTest 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());
}
use of com.zimbra.soap.mail.type.FilterTest 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();
}
use of com.zimbra.soap.mail.type.FilterTest 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");
}
Aggregations