Search in sources :

Example 6 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class TestImap method testAppendTags.

@Test
public void testAppendTags() throws Exception {
    Flags flags = Flags.fromSpec("afs");
    //new tag; does not exist in mbox
    String tag1 = "APPENDTAG1";
    flags.set(tag1);
    Date date = new Date(System.currentTimeMillis());
    Literal msg = message(10);
    try {
        AppendResult res = connection.append("INBOX", flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag1));
    } finally {
        msg.dispose();
    }
    //should not have created a visible tag
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue("APPEND created new visible tag", tags == null || tags.size() == 0);
    //now create a visible tag, add it to a message in inbox then try append to message in different folder
    String tag2 = "APPENDTAG2";
    ZTag tag = mbox.getTag(tag2);
    if (tag == null) {
        tag = mbox.createTag(tag2, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals(tag2, tags.get(0).getName());
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tag2, info.getFlags().isSet(tag2));
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    info = connection.select(folderName);
    assertFalse("new tag unexpectedly set in new folder", info.getFlags().isSet(tag2));
    msg = message(10);
    flags = Flags.fromSpec("afs");
    flags.set(tag2);
    try {
        AppendResult res = connection.append(folderName, flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag2));
    } finally {
        msg.dispose();
    }
    info = connection.select(folderName);
    assertTrue("new tag not set in new folder", info.getFlags().isSet(tag2));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) MessageData(com.zimbra.cs.mailclient.imap.MessageData) Literal(com.zimbra.cs.mailclient.imap.Literal) AppendResult(com.zimbra.cs.mailclient.imap.AppendResult) ZTag(com.zimbra.client.ZTag) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) ZFolder(com.zimbra.client.ZFolder) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) Test(org.junit.Test)

Example 7 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class TestImap method fetchBody.

private byte[] fetchBody(long uid) throws IOException {
    MessageData md = connection.uidFetch(uid, "(BODY.PEEK[])");
    assertNotNull("message not found", md);
    assertEquals(uid, md.getUid());
    Body[] bs = md.getBodySections();
    assertNotNull(bs);
    assertEquals(1, bs.length);
    return bs[0].getImapData().getBytes();
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) Body(com.zimbra.cs.mailclient.imap.Body)

Example 8 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class TestImap method testStoreInvalidSystemFlag.

@Test
public void testStoreInvalidSystemFlag() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", "", System.currentTimeMillis(), simpleMessage("foo"), true);
    MailboxInfo info = connection.select("INBOX");
    Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
    assertEquals(1, data.size());
    Iterator<Long> it = data.keySet().iterator();
    Long seq = it.next();
    storeInvalidFlag("\\Bulk", seq);
    storeInvalidFlag("\\Unread", seq);
    storeInvalidFlag("\\Forwarded", seq);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) MessageData(com.zimbra.cs.mailclient.imap.MessageData) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 9 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class TestImapClient method testFetch.

@Test
public void testFetch() throws Exception {
    connect();
    login();
    MailboxInfo mb = connection.select("INBOX");
    final AtomicLong count = new AtomicLong(mb.getExists());
    connection.uidFetch("1:*", "(FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY BODY.PEEK[])", new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) throws Exception {
            if (res.getCCode() != CAtom.FETCH)
                return;
            MessageData md = (MessageData) res.getData();
            assertNotNull(md);
            Envelope env = md.getEnvelope();
            assertNotNull(env);
            assertNotNull(env.getSubject());
            assertNotNull(md.getUid());
            assertTrue(md.getRfc822Size() != -1);
            assertNotNull(md.getInternalDate());
            BodyStructure bs = md.getBodyStructure();
            assertNotNull(bs);
            if (bs.isMultipart()) {
                BodyStructure[] parts = bs.getParts();
                for (BodyStructure part : parts) {
                    assertNotNull(part.getType());
                    assertNotNull(part.getSubtype());
                }
            } else {
                assertNotNull(bs.getType());
                assertNotNull(bs.getSubtype());
            }
            Body[] body = md.getBodySections();
            assertNotNull(body);
            assertEquals(1, body.length);
            // assertNotNull(body[0].getBytes());
            count.decrementAndGet();
            System.out.printf("Fetched uid = %s\n", md.getUid());
        }
    });
    assertEquals(0, count.longValue());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) MessageData(com.zimbra.cs.mailclient.imap.MessageData) BodyStructure(com.zimbra.cs.mailclient.imap.BodyStructure) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Envelope(com.zimbra.cs.mailclient.imap.Envelope) MailException(com.zimbra.cs.mailclient.MailException) MessagingException(javax.mail.MessagingException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class TestImap method testStoreTags.

@Test
public void testStoreTags() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue(tags == null || tags.size() == 0);
    String tagName = "T1";
    ZTag tag = mbox.getTag(tagName);
    if (tag == null) {
        tag = mbox.createTag(tagName, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals("T1", tags.get(0).getName());
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", "", System.currentTimeMillis(), simpleMessage("foo2"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tagName, info.getFlags().isSet(tagName));
    Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
    assertEquals(2, data.size());
    Iterator<Long> it = data.keySet().iterator();
    Long seq = it.next();
    assertTrue("flag not set on first message", data.get(seq).getFlags().isSet(tagName));
    seq = it.next();
    assertFalse("flag unexpectedly set on second message", data.get(seq).getFlags().isSet(tagName));
    connection.store(seq + "", "+FLAGS", tagName);
    data = connection.fetch(seq + "", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertTrue("flag not set after STORE in INBOX", data.get(seq).getFlags().isSet(tagName));
    mbox.addMessage(folder.getId(), "u", "", System.currentTimeMillis(), simpleMessage("bar"), true);
    info = connection.select(folderName);
    assertFalse(folderName + " contains unexpected flag " + tagName, info.getFlags().isSet(tagName));
    data = connection.fetch("*", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertFalse("flag unexpectedly set on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
    connection.store(seq + "", "+FLAGS", tagName);
    data = connection.fetch(seq + "", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
    info = connection.select(folderName);
    assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
    String tagName2 = "T2";
    connection.store(seq + "", "+FLAGS", tagName2);
    data = connection.fetch(seq + "", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
    assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName2));
    info = connection.select(folderName);
    assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
    assertTrue("new tag not set in new folder", info.getFlags().isSet(tagName2));
    //should not have created T2 as a visible tag
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals("T1", tags.get(0).getName());
    String tagName3 = "T3";
    connection.store(seq + "", "FLAGS", tagName3);
    data = connection.fetch(seq + "", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertFalse("flag unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
    assertFalse("flag unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName2));
    assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName3));
    info = connection.select(folderName);
    assertTrue("new tag not set in new folder", info.getFlags().isSet(tagName3));
    assertFalse("old tag unexpectedly set in new folder", info.getFlags().isSet(tagName));
    assertFalse("old tag unexpectedly set in new folder", info.getFlags().isSet(tagName2));
    //should not have created T2 or T3 as a visible tag
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals("T1", tags.get(0).getName());
    connection.store(seq + "", "-FLAGS", tagName3);
    data = connection.fetch(seq + "", "FLAGS");
    assertEquals(1, data.size());
    seq = data.keySet().iterator().next();
    assertTrue("flags unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isEmpty());
    info = connection.select("INBOX");
    assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
    assertFalse("new tag unexpectedly set in new folder", info.getFlags().isSet(tagName2));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) MessageData(com.zimbra.cs.mailclient.imap.MessageData) ZTag(com.zimbra.client.ZTag) ZFolder(com.zimbra.client.ZFolder) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Aggregations

MessageData (com.zimbra.cs.mailclient.imap.MessageData)14 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)6 Test (org.junit.Test)6 ServiceException (com.zimbra.common.service.ServiceException)5 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)5 MailException (com.zimbra.cs.mailclient.MailException)5 Flags (com.zimbra.cs.mailclient.imap.Flags)5 IOException (java.io.IOException)5 ZMailbox (com.zimbra.client.ZMailbox)4 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)4 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 ZFolder (com.zimbra.client.ZFolder)3 ZTag (com.zimbra.client.ZTag)3 FetchResponseHandler (com.zimbra.cs.mailclient.imap.FetchResponseHandler)3 SQLException (java.sql.SQLException)3 PurgedMessage (com.zimbra.cs.db.DbDataSource.PurgedMessage)2 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)2 Body (com.zimbra.cs.mailclient.imap.Body)2 ImapResponse (com.zimbra.cs.mailclient.imap.ImapResponse)2 Literal (com.zimbra.cs.mailclient.imap.Literal)2