Search in sources :

Example 91 with ParsedMessage

use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.

the class ReplaceHeaderTest method testReplaceHeaderNewNameFromCondition.

/*
     * Replace header with new name from the matching condition
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderNewNameFromCondition() {
    try {
        String filterScript = "require [\"editheader\", \"variables\"];\n" + "replaceheader :newname \"${1}\" :newvalue \"${2}\" :matches \"X-Test-Header\" \"t*t*\";\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 matchNotFound = false;
        boolean matchFound = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Test-Header".equals(header.getName()) && "test1".equals(header.getValue())) {
                matchNotFound = true;
            }
            if ("es".equals(header.getName()) && "1".equals(header.getValue())) {
                matchFound = true;
            }
        }
        Assert.assertFalse(matchNotFound);
        Assert.assertTrue(matchFound);
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 92 with ParsedMessage

use of com.zimbra.cs.mime.ParsedMessage in project zm-mailbox by Zimbra.

the class ReplaceHeaderTest method testReplaceHeaderWithEmptyHeaderNewName.

/*
     * Replace header with empty new name should not replace the original header
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderWithEmptyHeaderNewName() {
    try {
        String filterScript = "require [\"editheader\"];\n" + " replaceheader :newname \"\" :newvalue \"0\" :value \"lt\" :comparator \"i;ascii-casemap\" \"X-Test-Header\" \"test2\" \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);
        boolean found = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            Assert.assertFalse(header.getName().equals(""));
            if (header.getName().equals("X-Test-Header") && header.getValue().equals("test2")) {
                found = true;
            }
        }
        Assert.assertTrue(found);
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 93 with ParsedMessage

use of com.zimbra.cs.mime.ParsedMessage 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());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 94 with ParsedMessage

use of com.zimbra.cs.mime.ParsedMessage 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());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 95 with ParsedMessage

use of com.zimbra.cs.mime.ParsedMessage 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());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Aggregations

ParsedMessage (com.zimbra.cs.mime.ParsedMessage)388 Mailbox (com.zimbra.cs.mailbox.Mailbox)304 Test (org.junit.Test)300 Message (com.zimbra.cs.mailbox.Message)278 Account (com.zimbra.cs.account.Account)271 OperationContext (com.zimbra.cs.mailbox.OperationContext)264 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)252 ItemId (com.zimbra.cs.service.util.ItemId)186 MimeMessage (javax.mail.internet.MimeMessage)113 Header (javax.mail.Header)66 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)55 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)50 SyntaxException (org.apache.jsieve.exception.SyntaxException)49 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)45 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)33 ServiceException (com.zimbra.common.service.ServiceException)30 InputStream (java.io.InputStream)24 IOException (java.io.IOException)21 Element (com.zimbra.common.soap.Element)18