use of com.zimbra.soap.mail.type.FilterTest in project zm-mailbox by Zimbra.
the class SoapToSieve method handleTest.
private String handleTest(FilterTest test) throws ServiceException {
String snippet = null;
if (test instanceof FilterTest.HeaderTest) {
snippet = toSieve((FilterTest.HeaderTest) test);
} else if (test instanceof FilterTest.MimeHeaderTest) {
snippet = toSieve((FilterTest.MimeHeaderTest) test);
} else if (test instanceof FilterTest.AddressTest) {
snippet = toSieve((FilterTest.AddressTest) test);
} else if (test instanceof FilterTest.HeaderExistsTest) {
String header = ((FilterTest.HeaderExistsTest) test).getHeader();
snippet = String.format("exists \"%s\"", FilterUtil.escape(header));
} else if (test instanceof FilterTest.SizeTest) {
FilterTest.SizeTest sizeTest = (FilterTest.SizeTest) test;
Sieve.NumberComparison comp = Sieve.NumberComparison.fromString(sizeTest.getNumberComparison());
String size = sizeTest.getSize();
try {
FilterUtil.parseSize(size);
} catch (NumberFormatException e) {
throw ServiceException.INVALID_REQUEST("Invalid size: " + size, e);
}
snippet = String.format("size :%s %s", comp, size);
} else if (test instanceof FilterTest.DateTest) {
FilterTest.DateTest dateTest = (FilterTest.DateTest) test;
Sieve.DateComparison comp = Sieve.DateComparison.fromString(dateTest.getDateComparison());
Date date = new Date(dateTest.getDate() * 1000L);
snippet = String.format("date :%s \"%s\"", comp, Sieve.DATE_PARSER.format(date));
} else if (test instanceof FilterTest.BodyTest) {
FilterTest.BodyTest bodyTest = (FilterTest.BodyTest) test;
String format = bodyTest.isCaseSensitive() ? "body :contains :comparator \"i;octet\" \"%s\"" : "body :contains \"%s\"";
snippet = String.format(format, FilterUtil.escape(bodyTest.getValue()));
} else if (test instanceof FilterTest.AddressBookTest) {
FilterTest.AddressBookTest abTest = (FilterTest.AddressBookTest) test;
String header = abTest.getHeader();
if (header.contains(",")) {
String[] headerList = header.split(",");
StringBuilder format = new StringBuilder();
for (String item : headerList) {
if (format.length() > 0) {
format.append(",");
}
format.append("\"").append(FilterUtil.escape(item)).append("\"");
}
snippet = "addressbook :in [" + format.toString() + ']';
} else {
snippet = "addressbook :in \"" + FilterUtil.escape(header) + '"';
}
} else if (test instanceof FilterTest.ContactRankingTest) {
FilterTest.ContactRankingTest rankingTest = (FilterTest.ContactRankingTest) test;
snippet = "contact_ranking :in \"" + FilterUtil.escape(rankingTest.getHeader()) + '"';
} else if (test instanceof FilterTest.MeTest) {
FilterTest.MeTest meTest = (FilterTest.MeTest) test;
snippet = "me :in \"" + FilterUtil.escape(meTest.getHeader()) + '"';
} else if (test instanceof FilterTest.AttachmentTest) {
snippet = "attachment";
} else if (test instanceof FilterTest.InviteTest) {
snippet = toSieve((FilterTest.InviteTest) test);
} else if (test instanceof FilterTest.CurrentTimeTest) {
FilterTest.CurrentTimeTest timeTest = (FilterTest.CurrentTimeTest) test;
Sieve.DateComparison comparison = Sieve.DateComparison.fromString(timeTest.getDateComparison());
String time = timeTest.getTime();
// validate time value
String timeFormat = "HHmm";
SimpleDateFormat parser = new SimpleDateFormat(timeFormat);
parser.setLenient(false);
try {
parser.parse(time);
} catch (ParseException e) {
throw ServiceException.INVALID_REQUEST("Invalid time: " + time, e);
}
if (time.length() != timeFormat.length()) {
throw ServiceException.INVALID_REQUEST("Time string must be of length " + timeFormat.length(), null);
}
snippet = String.format("current_time :%s \"%s\"", comparison, time);
} else if (test instanceof FilterTest.CurrentDayOfWeekTest) {
FilterTest.CurrentDayOfWeekTest dayTest = (FilterTest.CurrentDayOfWeekTest) test;
String value = dayTest.getValues();
String[] daysOfWeek = value.split(",");
for (int i = 0; i < daysOfWeek.length; i++) {
// first validate value
try {
int day = Integer.valueOf(daysOfWeek[i]);
if (day < 0 || day > 6) {
throw ServiceException.INVALID_REQUEST("Day of week index must be from 0 (Sunday) to 6 (Saturday)", null);
}
} catch (NumberFormatException e) {
throw ServiceException.INVALID_REQUEST("Invalid day of week index: " + daysOfWeek[i], e);
}
daysOfWeek[i] = StringUtil.enclose(daysOfWeek[i], '"');
}
snippet = "current_day_of_week :is [" + Joiner.on(',').join(daysOfWeek) + "]";
} else if (test instanceof FilterTest.ConversationTest) {
FilterTest.ConversationTest convTest = (FilterTest.ConversationTest) test;
String where = MoreObjects.firstNonNull(convTest.getWhere(), "started");
snippet = String.format("conversation :where \"%s\"", FilterUtil.escape(where));
} else if (test instanceof FilterTest.FacebookTest) {
snippet = "facebook";
} else if (test instanceof FilterTest.LinkedInTest) {
snippet = "linkedin";
} else if (test instanceof FilterTest.SocialcastTest) {
snippet = "socialcast";
} else if (test instanceof FilterTest.TwitterTest) {
snippet = "twitter";
} else if (test instanceof FilterTest.CommunityRequestsTest) {
snippet = "community_requests";
} else if (test instanceof FilterTest.CommunityContentTest) {
snippet = "community_content";
} else if (test instanceof FilterTest.CommunityConnectionsTest) {
snippet = "community_connections";
} else if (test instanceof FilterTest.ListTest) {
snippet = "list";
} else if (test instanceof FilterTest.BulkTest) {
snippet = "bulk";
} else if (test instanceof FilterTest.ImportanceTest) {
FilterTest.ImportanceTest impTest = (FilterTest.ImportanceTest) test;
snippet = String.format("importance \"%s\"", impTest.getImportance());
} else if (test instanceof FilterTest.FlaggedTest) {
FilterTest.FlaggedTest flaggedTest = (FilterTest.FlaggedTest) test;
snippet = "flagged \"" + flaggedTest.getFlag() + "\"";
} else if (test instanceof FilterTest.TrueTest) {
snippet = "true";
} else {
ZimbraLog.soap.debug("Ignoring unexpected test: %s", test);
}
if (snippet != null && test.isNegative()) {
snippet = "not " + snippet;
}
return snippet;
}
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, boolean isAdminScript) 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, isAdminScript);
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(), isAdminScript));
}
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 JaxbToJsonTest method bug65572BooleanAndXmlElements.
/**
* {
* "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 bug65572BooleanAndXmlElements() 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