Search in sources :

Example 6 with ImapResponse

use of com.zimbra.cs.mailclient.imap.ImapResponse 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);
    final CountDownLatch doneSignal = new CountDownLatch(1);
    // Start IDLE...
    connection.idle(new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) {
            System.out.println("XXX res = " + res);
            if (res.getCCode() == CAtom.EXISTS) {
                exists.set(res.getNumber());
                doneSignal.countDown();
            }
        }
    });
    assertTrue(connection.isIdling());
    // Send test message
    TestUtil.addMessage(TestUtil.getZMailbox(USER), "TestImapClient testIdle");
    // Wait for message delivery...
    try {
        doneSignal.await(5, TimeUnit.SECONDS);
    } catch (Exception e) {
        fail("Wait interrupted. ");
    }
    // Stop IDLE...
    connection.stopIdle();
    // Check mailbox status
    MailboxInfo mb = connection.getMailboxInfo();
    assertEquals(String.format("got wrong number of messages. mb.getExists(): %d. exists.get(): %d", mb.getExists(), exists.get()), 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) CountDownLatch(java.util.concurrent.CountDownLatch) 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 7 with ImapResponse

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

the class TestImap method testOverflowNotAppend.

@Test
public void testOverflowNotAppend() throws Exception {
    int oldReadTimeout = connection.getConfig().getReadTimeout();
    try {
        connection.setReadTimeout(10);
        Flags flags = Flags.fromSpec("afs");
        Date date = new Date(System.currentTimeMillis());
        ImapRequest req = connection.newRequest(CAtom.FETCH, "1:*");
        req.addParam("{" + ((long) (Integer.MAX_VALUE) + 1) + "+}");
        ImapResponse resp = req.send();
        assertTrue(resp.isNO() || resp.isBAD());
    } finally {
        connection.setReadTimeout(oldReadTimeout);
    }
}
Also used : ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) Test(org.junit.Test)

Example 8 with ImapResponse

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

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

the class TestImap method testOverflowAppend.

@Test
public void testOverflowAppend() throws Exception {
    assertTrue(connection.hasCapability("UIDPLUS"));
    int oldReadTimeout = connection.getConfig().getReadTimeout();
    try {
        connection.setReadTimeout(10);
        Flags flags = Flags.fromSpec("afs");
        Date date = new Date(System.currentTimeMillis());
        ImapRequest req = connection.newRequest(CAtom.APPEND, new MailboxName("INBOX"));
        req.addParam("{" + ((long) (Integer.MAX_VALUE) + 1) + "+}");
        ImapResponse resp = req.send();
        assertTrue(resp.isNO() || resp.isBAD());
        req = connection.newRequest(CAtom.APPEND, new MailboxName("INBOX"));
        req.addParam("{" + ((long) (Integer.MAX_VALUE) + 1) + "}");
        resp = req.send();
        assertTrue(resp.isNO() || resp.isBAD());
    } finally {
        connection.setReadTimeout(oldReadTimeout);
    }
}
Also used : MailboxName(com.zimbra.cs.mailclient.imap.MailboxName) ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) Test(org.junit.Test)

Example 10 with ImapResponse

use of com.zimbra.cs.mailclient.imap.ImapResponse 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)12 Test (org.junit.Test)12 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)8 ImapRequest (com.zimbra.cs.mailclient.imap.ImapRequest)8 ResponseHandler (com.zimbra.cs.mailclient.imap.ResponseHandler)8 IOException (java.io.IOException)8 Flags (com.zimbra.cs.mailclient.imap.Flags)7 ServiceException (com.zimbra.common.service.ServiceException)6 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)6 MessagingException (javax.mail.MessagingException)6 MessageData (com.zimbra.cs.mailclient.imap.MessageData)4 ZMailbox (com.zimbra.client.ZMailbox)3 Date (java.sql.Date)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ZTag (com.zimbra.client.ZTag)2 MailException (com.zimbra.cs.mailclient.MailException)2 Literal (com.zimbra.cs.mailclient.imap.Literal)2 MailboxName (com.zimbra.cs.mailclient.imap.MailboxName)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2