Search in sources :

Example 1 with ResponseHandler

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

the class TestImapClient method testIdle.

@Test
public void testIdle() throws Exception {
    login();
    assertFalse(connection.isIdling());
    final AtomicLong exists = new AtomicLong(-1);
    // Start IDLE...
    connection.idle(new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) {
            System.out.println("XXX res = " + res);
            if (res.getCCode() == CAtom.EXISTS) {
                synchronized (exists) {
                    exists.set(res.getNumber());
                }
            }
        }
    });
    assertTrue(connection.isIdling());
    // Send test message
    sendTestMessage();
    // Wait for message delivery...
    synchronized (exists) {
        while (exists.get() <= 0) {
            exists.wait();
        }
    }
    // Stop IDLE...
    connection.stopIdle();
    // Check mailbox status
    MailboxInfo mb = connection.getMailboxInfo();
    assertEquals(mb.getExists(), exists.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 2 with ResponseHandler

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

the class TestImap method testStoreTagsDirty.

@Test
public void testStoreTagsDirty() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue(tags == null || tags.size() == 0);
    String tagName = "T1";
    final String tagName2 = "T2";
    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);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tagName, info.getFlags().isSet(tagName));
    assertFalse("INBOX contain unexpected flag " + tagName2, info.getFlags().isSet(tagName2));
    Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
    assertEquals(1, 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));
    ImapRequest req = connection.newRequest("STORE", seq + "", "+FLAGS", tagName2);
    req.setResponseHandler(new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) throws Exception {
            if (res.isUntagged() && res.getCCode() == CAtom.FLAGS) {
                Flags flags = (Flags) res.getData();
                assertTrue(flags.isSet(tagName2));
            }
        }
    });
    req.sendCheckStatus();
}
Also used : ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) MessageData(com.zimbra.cs.mailclient.imap.MessageData) ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) ZTag(com.zimbra.client.ZTag) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) ServiceException(com.zimbra.common.service.ServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 3 with ResponseHandler

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

the class TestImap method testAppendTagsDirty.

@Test
public void testAppendTagsDirty() throws Exception {
    Flags flags = Flags.fromSpec("afs");
    //new tag; does not exist in mbox
    final String tag1 = "NEWDIRTYTAG";
    MailboxInfo info = connection.select("INBOX");
    assertFalse("INBOX contains unexpected flag " + tag1, info.getFlags().isSet(tag1));
    flags.set(tag1);
    Date date = new Date(System.currentTimeMillis());
    Literal msg = message(10);
    try {
        ImapRequest req = connection.newRequest("APPEND", "INBOX", flags, date, msg);
        req.setResponseHandler(new ResponseHandler() {

            @Override
            public void handleResponse(ImapResponse res) throws Exception {
                if (res.isUntagged() && res.getCCode() == CAtom.FLAGS) {
                    Flags flags = (Flags) res.getData();
                    assertTrue(flags.isSet(tag1));
                }
            }
        });
        req.sendCheckStatus();
    } finally {
        msg.dispose();
    }
}
Also used : ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) Literal(com.zimbra.cs.mailclient.imap.Literal) ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) ServiceException(com.zimbra.common.service.ServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 4 with ResponseHandler

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

Aggregations

ImapResponse (com.zimbra.cs.mailclient.imap.ImapResponse)4 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)4 ResponseHandler (com.zimbra.cs.mailclient.imap.ResponseHandler)4 Test (org.junit.Test)4 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)3 IOException (java.io.IOException)3 ServiceException (com.zimbra.common.service.ServiceException)2 Flags (com.zimbra.cs.mailclient.imap.Flags)2 ImapRequest (com.zimbra.cs.mailclient.imap.ImapRequest)2 MessageData (com.zimbra.cs.mailclient.imap.MessageData)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ZFolder (com.zimbra.client.ZFolder)1 ZMailbox (com.zimbra.client.ZMailbox)1 ZTag (com.zimbra.client.ZTag)1 MailException (com.zimbra.cs.mailclient.MailException)1 BodyStructure (com.zimbra.cs.mailclient.imap.BodyStructure)1 Envelope (com.zimbra.cs.mailclient.imap.Envelope)1 Literal (com.zimbra.cs.mailclient.imap.Literal)1 Date (java.sql.Date)1