Search in sources :

Example 61 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderNonAscii.

/*
     * Replace the subject with non-ASCII strings
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderNonAscii() {
    try {
        String filterScript = "require [\"editheader\", \"variables\"];\n" + "replaceheader :newvalue \"[追加]${1}\" :matches \"Subject\" \"*\";";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(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 newSubject = "";
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header temp = enumeration.nextElement();
            if ("Subject".equals(temp.getName())) {
                newSubject = temp.getValue();
                break;
            }
        }
        Assert.assertEquals("=?UTF-8?Q?[=E8=BF=BD=E5=8A=A0]example?=", newSubject);
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Example 62 with Message

use of com.zimbra.cs.mailbox.Message 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\", \"relational\"];\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.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(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) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Example 63 with Message

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

the class ReplaceHeaderTest method testReplaceNoValuePattern.

/*
     * Replace header when no value-patterns are specified
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceNoValuePattern() {
    try {
        String filterScript = "require [\"editheader\"];\n" + " replaceheader :newname \"X-New-Header\" :newvalue \"new value\" :comparator \"i;ascii-casemap\" :matches \"Subject\" \r\n" + "  ;\n";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(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-New-Header".equals(header.getName())) {
                Assert.assertEquals("new value", header.getValue());
            } else if ("Subject".equals(header.getName())) {
                fail("Subject header should have been replaced");
            }
        }
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Example 64 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderAtIndex.

/*
     * Replace header value at index
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderAtIndex() {
    try {
        String filterScript = "require [\"editheader\"];\n" + " replaceheader :index 2 :newvalue \"new test\" :contains \"X-Test-Header\" \"test\" \r\n" + "  ;\n";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(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);
        int indexMatch = 0;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Test-Header".equals(header.getName())) {
                indexMatch++;
                if (indexMatch == 2) {
                    Assert.assertEquals("new test", header.getValue());
                    break;
                }
            }
        }
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Example 65 with Message

use of com.zimbra.cs.mailbox.Message 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\", \"relational\", \"comparator-i;ascii-numeric\"];\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.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(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());
    }
}
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) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Aggregations

Message (com.zimbra.cs.mailbox.Message)442 Mailbox (com.zimbra.cs.mailbox.Mailbox)375 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)358 Test (org.junit.Test)335 Account (com.zimbra.cs.account.Account)315 OperationContext (com.zimbra.cs.mailbox.OperationContext)302 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)281 ItemId (com.zimbra.cs.service.util.ItemId)211 MimeMessage (javax.mail.internet.MimeMessage)123 SyntaxException (org.apache.jsieve.exception.SyntaxException)87 Header (javax.mail.Header)83 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)63 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)53 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)46 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)43 ServiceException (com.zimbra.common.service.ServiceException)41 Element (com.zimbra.common.soap.Element)41 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)36 Folder (com.zimbra.cs.mailbox.Folder)32 ZMailbox (com.zimbra.client.ZMailbox)26