Search in sources :

Example 1 with AbstractActionCommand

use of org.apache.jsieve.commands.AbstractActionCommand in project zm-mailbox by Zimbra.

the class RuleManagerWithCustomActionFilterTest method init.

@BeforeClass
public static void init() throws Exception {
    // keep original sieve factory
    original_sf = RuleManager.getSieveFactory();
    ExtensionTestUtil.init();
    MailboxTestUtil.initServer();
    Provisioning prov = Provisioning.getInstance();
    prov.createAccount("test@zimbra.com", "secret", new HashMap<String, Object>());
    // make sure the behavior before registering custom actions
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    AbstractActionCommand ext = (AbstractActionCommand) ExtensionUtil.getExtension("discard");
    Assert.assertNull(ext);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { discard; }");
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: test".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(0, ids.size());
    // register custom action extensions
    ExtensionTestUtil.registerExtension("com.zimbra.extensions.DummyCustomDiscard");
    ExtensionTestUtil.registerExtension("com.zimbra.extensions.DummyCustomTag");
    ExtensionUtil.initAll();
}
Also used : Account(com.zimbra.cs.account.Account) AbstractActionCommand(org.apache.jsieve.commands.AbstractActionCommand) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) MockProvisioning(com.zimbra.cs.account.MockProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) BeforeClass(org.junit.BeforeClass)

Example 2 with AbstractActionCommand

use of org.apache.jsieve.commands.AbstractActionCommand in project zm-mailbox by Zimbra.

the class RuleManagerWithCustomActionFilterTest method customDicardAndCustomTag.

@Test
public void customDicardAndCustomTag() throws Exception {
    // register custom action extensions
    //ExtensionTestUtil.registerExtension("com.zimbra.extensions.DummyCustomDiscard");
    //ExtensionTestUtil.registerExtension("com.zimbra.extensions.DummyCustomTag");
    //ExtensionUtil.initAll();
    JsieveConfigMapHandler.registerCommand("discard", "com.zimbra.extensions.DummyCustomDiscard");
    JsieveConfigMapHandler.registerCommand("tag", "com.zimbra.extensions.DummyCustomTag");
    // recreate sieve factory
    Method method = RuleManager.class.getDeclaredMethod("createSieveFactory");
    method.setAccessible(true);
    Field field = RuleManager.class.getDeclaredField("SIEVE_FACTORY");
    field.setAccessible(true);
    field.set(RuleManager.class, method.invoke(RuleManager.class));
    // make sure the registrations
    AbstractActionCommand discard_ext = (AbstractActionCommand) ExtensionUtil.getExtension("discard");
    Assert.assertNotNull(discard_ext);
    AbstractActionCommand tag_ext = (AbstractActionCommand) ExtensionUtil.getExtension("tag");
    Assert.assertNotNull(tag_ext);
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if header :contains [\"Subject\"] [\"Zimbra\"] { tag \"socialcast\"; discard; }");
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: Zimbra".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    Message msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertArrayEquals(new String[] { "zimbra", "priority" }, msg.getTags());
// inactivate custom tag action extension for just in case this test would be executed
// before tagAndCustomDiscard test above
//ZimbraExtension tag_ext2 = ExtensionUtil.getExtension("tag");
//tag_ext2.destroy();
}
Also used : Field(java.lang.reflect.Field) Account(com.zimbra.cs.account.Account) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) AbstractActionCommand(org.apache.jsieve.commands.AbstractActionCommand) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Method(java.lang.reflect.Method) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test)

Example 3 with AbstractActionCommand

use of org.apache.jsieve.commands.AbstractActionCommand in project zm-mailbox by Zimbra.

the class RuleManagerWithCustomActionFilterTest method tagAndCustomDiscard.

@Test
public void tagAndCustomDiscard() throws Exception {
    // register custom action extension
    //ExtensionTestUtil.registerExtension("com.zimbra.extensions.DummyCustomDiscard");
    //ExtensionUtil.initAll();
    JsieveConfigMapHandler.registerCommand("discard", "com.zimbra.extensions.DummyCustomDiscard");
    JsieveConfigMapHandler.registerCommand("tag", "com.zimbra.cs.filter.jsieve.Tag");
    // recreate sieve factory
    Method method = RuleManager.class.getDeclaredMethod("createSieveFactory");
    method.setAccessible(true);
    Field field = RuleManager.class.getDeclaredField("SIEVE_FACTORY");
    field.setAccessible(true);
    field.set(RuleManager.class, method.invoke(RuleManager.class));
    // make sure the registrations
    AbstractActionCommand ext = (AbstractActionCommand) ExtensionUtil.getExtension("discard");
    Assert.assertNotNull(ext);
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"socialcast\"; discard; }");
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: test".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    Message msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals("Inbox", mbox.getFolderById(null, msg.getFolderId()).getName());
    Assert.assertArrayEquals(new String[] { "socialcast", "priority" }, msg.getTags());
}
Also used : Field(java.lang.reflect.Field) Account(com.zimbra.cs.account.Account) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) AbstractActionCommand(org.apache.jsieve.commands.AbstractActionCommand) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Method(java.lang.reflect.Method) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test)

Aggregations

Account (com.zimbra.cs.account.Account)3 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)3 ItemId (com.zimbra.cs.service.util.ItemId)3 AbstractActionCommand (org.apache.jsieve.commands.AbstractActionCommand)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 MockProvisioning (com.zimbra.cs.account.MockProvisioning)1 Provisioning (com.zimbra.cs.account.Provisioning)1 BeforeClass (org.junit.BeforeClass)1