use of com.zimbra.cs.mailbox.DeliveryContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderWithXSpamScore.
/*
* Replace header with numeric comparator :count
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderWithXSpamScore() {
try {
String filterScript = "require [\"editheader\", \"variables\"];\n" + "if anyof(header :value \"ge\" :comparator \"i;ascii-numeric\" [\"X-Spam-Score\"] [\"80\"]) {" + " if exists \"Subject\" {" + " replaceheader :newvalue \"[SPAM]${1}\" :matches \"Subject\" \"*\";" + " } else {" + " addheader :last \"Subject\" \"[SPAM]\";" + " }" + " }";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
String subjectValue = "";
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("Subject".equals(header.getName())) {
subjectValue = header.getValue();
}
}
Assert.assertEquals("[SPAM]example", subjectValue);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.DeliveryContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderForMultilineValuedHeader.
/*
* Replace header with multiline valued header
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderForMultilineValuedHeader() {
try {
String filterScript = "require [\"editheader\", \"variables\"];\n" + " replaceheader :newvalue \"${1}[test]${2}\" :matches \"X-Test-Header\" \"*line2*\";";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg2.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
String headerValue = "";
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Test-Header".equals(header.getName())) {
headerValue = header.getValue();
break;
}
}
Assert.assertEquals("line1 [test] line3", headerValue);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.DeliveryContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderValueNegative.
/*
* Replace header with numeric comparator :value
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderValueNegative() {
try {
String filterScript = "require [\"editheader\"];\n" + "replaceheader :newname \"X-Numeric2-Header\" :newvalue \"0\" :value \"lt\" :comparator \"i;ascii-numeric\" \"X-Numeric-Header\" \"-3\";\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
boolean matchFound = false;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Numeric2-Header".equals(header.getName())) {
matchFound = true;
}
}
Assert.assertFalse(matchFound);
matchFound = false;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Numeric-Header".equals(header.getName())) {
matchFound = true;
}
}
Assert.assertTrue(matchFound);
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.DeliveryContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderWithNumericComparisionUsingValue.
/*
* Replace header with numeric comparator :value
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderWithNumericComparisionUsingValue() {
try {
String filterScript = "require [\"editheader\"];\n" + " replaceheader :newname \"X-Numeric2-Header\" :newvalue \"0\" :value \"lt\" :comparator \"i;ascii-numeric\" \"X-Numeric-Header\" \"3\" \r\n" + " ;\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Numeric2-Header".equals(header.getName())) {
Assert.assertEquals("0", header.getValue());
break;
}
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mailbox.DeliveryContext in project zm-mailbox by Zimbra.
the class ReplaceHeaderTest method testReplaceHeaderForEncodedHeaderValue.
/*
* Replace encoded header value
*/
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderForEncodedHeaderValue() {
try {
String filterScript = "require [\"editheader\", \"variables\"];\n" + "replaceheader :newvalue \"[test]${1}\" :matches \"X-Test-Header\" \"*\";" + "tag \"tag1-${1}\";" + "tag \"tag2-${2}\";" + "tag \"tag3-${3}\";" + "tag \"tag4-${4}\";" + "tag \"tag5-${5}\";" + "tag \"tag6-${6}\";" + "tag \"tag7-${7}\";" + "tag \"tag8-${8}\";" + "tag \"tag9-${9}\";";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg3.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
String headerValue = "";
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header header = enumeration.nextElement();
if ("X-Test-Header".equals(header.getName())) {
headerValue = header.getValue();
break;
}
}
String matchValue = "=?UTF-8?Q?[test][SP?=\r\n" + " =?UTF-8?Q?AM]=E6=97=A5=E6=9C=AC=E8=AA=9E=E3=81=AE=E4=BB=B6=E5=90=8D?=";
Assert.assertEquals(matchValue, headerValue);
String[] expectedTags = { "tag1-[SPAM]日本語の件名", "tag2-ABC", "tag3-123", "tag4-abc", "tag5-\"\"", "tag6-XYZ", "tag7-", "tag8-xyz", "tag9-\'\'" };
String[] resultTags = message.getTags();
for (String resultTag : resultTags) {
String expectedTag = null;
for (String testTag : expectedTags) {
if (testTag.equalsIgnoreCase(resultTag)) {
expectedTag = testTag;
break;
}
}
Assert.assertEquals(expectedTag, resultTag);
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
Aggregations