Search in sources :

Example 11 with MailboxInfo

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

the class TestImapClient method testSelect.

@Test
public void testSelect() throws Exception {
    login();
    MailboxInfo mb = connection.getMailboxInfo();
    assertNotNull(mb);
    assertTrue(mb.isReadWrite());
    assertTrue(mb.getUidValidity() > 0);
    assertTrue(mb.getUidNext() > 0);
}
Also used : MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 12 with MailboxInfo

use of com.zimbra.cs.mailclient.imap.MailboxInfo 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 13 with MailboxInfo

use of com.zimbra.cs.mailclient.imap.MailboxInfo 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)

Example 14 with MailboxInfo

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

the class ImapAppender method appendSlow.

// Slow APPEND for servers lacking UIDPLUS capability
private long appendSlow(MessageInfo mi, Literal lit) throws IOException, MessagingException {
    MailboxInfo mb = connection.getMailboxInfo();
    if (mdf == null) {
        mdf = new MailDateFormat();
    }
    if (mb == null || !mailbox.equals(mb.getName())) {
        connection.select(mailbox);
    }
    long startUid = getUidNext();
    connection.append(mailbox, mi.flags, mi.date, lit);
    if (mi.mm.getSentDate() == null || mi.mm.getMessageID() == null) {
        // "Date" and "Message-ID" headers are required to find message
        return -1;
    }
    // Check new messages for the one we just appended
    try {
        //exchange doesn't give accurate UIDNEXT unless mbox is selected again.
        connection.select(mailbox);
        long endUid = getUidNext() - 1;
        if (startUid <= endUid) {
            List<Long> found = findUids(startUid + ":" + endUid, mi);
            if (found.size() == 1) {
                return found.get(0);
            }
        }
        // If not found then server must have de-duped the message. This
        // is certainly possible with GMail. Search through the entire mailbox
        // for matching message and hope this is not too slow.
        List<Long> uids;
        try {
            // bug 45385: Temporarily increase timeout to 10 minutes in case of slow search.
            // Not pretty, but hopefully we never get here since most servers now support
            // UIDPLUS or don't de-dup messages.
            // bug 64062 : let's try 2 minutes. hopefully search shouldn't take too long now that we use msg id rather than subject
            connection.setReadTimeout(2 * 60);
            uids = connection.uidSearch(getSearchParams(mi));
        } finally {
            connection.setReadTimeout(connection.getConfig().getReadTimeout());
        }
        Iterator<Long> it = uids.iterator();
        while (it.hasNext()) {
            List<Long> found = findUids(nextSeq(it, 5), mi);
            if (found.size() > 0) {
                if (found.size() > 1) {
                    ZimbraLog.imap_client.warn("found more than one (%d)" + "matching UID during appendSlow. Probably a leftover dupe from earlier bugs?", found.size());
                    if (ZimbraLog.imap_client.isDebugEnabled()) {
                        ZimbraLog.imap_client.debug("potential duplicate ids = %s", Joiner.on(',').join(found));
                    }
                }
                return found.get(0);
            }
        }
    } catch (Exception e) {
        //if this is a real exception (e.g. network went down) next command will fail regardless.
        //otherwise, don't allow appendSlow to create loop
        ZimbraLog.imap_client.warn("Dedupe search in appendSlow failed.", e);
    }
    //this usually is OK, and actually the way Exchange has been working due to size check in matches()
    //we delete the local tracker and allow next sync to get the current version of message
    ZimbraLog.imap_client.warn("append slow failed to find appended message id");
    // If still not found, then give up :(
    return -1;
}
Also used : MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) MailDateFormat(javax.mail.internet.MailDateFormat) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 15 with MailboxInfo

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

the class RemoteFolder method status.

public MailboxInfo status() throws IOException {
    MailboxInfo mi = connection.status(path, "UIDVALIDITY", "UIDNEXT", "MESSAGES");
    // Bug 35554: If server does not provide UIDVALIDITY, then assume a value of 1
    if (mi.getUidValidity() <= 0) {
        mi.setUidValidity(1);
    }
    if (!path.equals(mi.getName())) {
        path = mi.getName();
    }
    if (mi.getExists() == -1) {
        debug("Server did not provide MESSAGES");
        mi.setExists(1);
    }
    return mi;
}
Also used : MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo)

Aggregations

MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)15 Test (org.junit.Test)10 MessageData (com.zimbra.cs.mailclient.imap.MessageData)6 ZFolder (com.zimbra.client.ZFolder)4 ZMailbox (com.zimbra.client.ZMailbox)4 Flags (com.zimbra.cs.mailclient.imap.Flags)4 ImapResponse (com.zimbra.cs.mailclient.imap.ImapResponse)4 Literal (com.zimbra.cs.mailclient.imap.Literal)4 ResponseHandler (com.zimbra.cs.mailclient.imap.ResponseHandler)4 IOException (java.io.IOException)4 ZTag (com.zimbra.client.ZTag)3 ServiceException (com.zimbra.common.service.ServiceException)3 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)3 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)3 ImapRequest (com.zimbra.cs.mailclient.imap.ImapRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.sql.Date)2 Date (java.util.Date)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 MessagingException (javax.mail.MessagingException)2