com.sun.mail.iap.Response Java Examples

The following examples show how to use com.sun.mail.iap.Response. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns BODY object containing desired message fragment
 * 
 * @param folder Folder containing the message
 * @param uid Message UID
 * @param from starting byte
 * @param count bytes to read
 * @return BODY containing desired message fragment
 * @throws MessagingException
 */
private static BODY getMessageBodyPart(IMAPFolder folder, final Long uid, final Integer from, final Integer count) throws MessagingException
{
    return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[]<" + from + "." + count + ">)", null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message part <" + from + "." + count + ">");
            }

            FetchResponse fetchResponse = (FetchResponse) r[0];
            BODY body = (BODY) fetchResponse.getItem(com.sun.mail.imap.protocol.BODY.class);
            return body;
        }
    });

}
 
Example #2
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static RFC822DATA getRFC822Message(final IMAPFolder folder, final long uid) throws MessagingException
{
    return (RFC822DATA) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (RFC822)", null);
            logResponse(r);
            Response response = r[r.length - 1];
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message in RFC822 format");
            }

            FetchResponse fetchResponse = (FetchResponse) r[0];
            return fetchResponse.getItem(RFC822DATA.class);
        }
    });
   
}
 
Example #3
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns a full message body
 * 
 * @param folder Folder containing the message
 * @param uid Message UID
 * @return Returns size of the message
 * @throws MessagingException
 */
private static BODY getMessageBody(IMAPFolder folder, final Long uid) throws MessagingException
{
    return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[])", null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message size");
            }
            FetchResponse fetchResponse = (FetchResponse) r[0];
            BODY body = (BODY) fetchResponse.getItem(BODY.class);
            return body;
        }
    });
}
 
Example #4
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchNotFlags() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        folder.setFlags(new int[]{2, 3}, new Flags(Flags.Flag.ANSWERED), true);
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH NOT (ANSWERED) NOT (DELETED) NOT (SEEN) NOT (FLAGGED) ALL", null);
            }
        });
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("1 4 5 6 7 8 9 10" /* 2 and 3 set to answered */, response.getRest());
    } finally {
        store.close();
    }
}
 
Example #5
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testListAndStatusWithNonExistingFolder() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        assertFalse(folder.getFolder("non existent folder").exists());
        for (final String cmd : new String[]{
                "STATUS \"non existent folder\" (MESSAGES UIDNEXT UIDVALIDITY UNSEEN)",
                "SELECT \"non existent folder\""
        }) {
            Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
                @Override
                public Object doCommand(IMAPProtocol protocol) {
                    return protocol.command(cmd, null);
                }
            });

            IMAPResponse response = (IMAPResponse) ret[0];
            assertTrue(response.isNO());
        }
    } finally {
        store.close();
    }
}
 
Example #6
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testUidSearchAll() throws MessagingException, IOException {
    greenMail.setUser("foo2@localhost", "pwd");
    store.connect("foo2@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        final MimeMessage email = GreenMailUtil.createTextEmail("foo2@localhost", "foo@localhost",
                "some subject", "some content",
                greenMail.getSmtp().getServerSetup());


        final IMAPFolder.ProtocolCommand uid_search_all = new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH ALL", null);
            }
        };

        // Search empty
        Response[] ret = (Response[]) folder.doCommand(uid_search_all);
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("* SEARCH", response.toString());
    } finally {
        store.close();
    }
}
 
Example #7
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
private void searchAndValidateWithCharset(IMAPFolder folder, String expected, String charset, Argument arg) throws MessagingException {
    Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
        @Override
        public Object doCommand(IMAPProtocol protocol) {
            return protocol.command("UID SEARCH CHARSET " + charset + " TEXT", arg);
        }
    });
    IMAPResponse response = (IMAPResponse) ret[0];
    assertFalse(response.isBAD());
    String number = response.getRest();
    assertEquals("Failed for charset " + charset, expected, number);
}
 
Example #8
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameFolder() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("CREATE foo", null);
            }
        });

        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("RENAME foo bar", null);
            }
        });

        Response response2 = ret[0];
        assertTrue(response2.isOK());

        final Folder bar = store.getFolder("bar");
        bar.open(Folder.READ_ONLY);
        assertTrue(bar.exists());
    } finally {
        store.close();
    }
}
 
Example #9
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchUidsAndSize() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID FETCH 1:* RFC822.SIZE", null);
            }
        });

        FetchResponse fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID and SIZE

        RFC822SIZE size = fetchResponse.getItem(RFC822SIZE.class);
        assertNotNull(size);
        assertTrue(size.size > 0);

        UID uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);
    } finally {
        store.close();
    }
}
 
Example #10
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Simple util for logging response
 * 
 * @param r response
 */
private static void logResponse(Response[] r)
{
    for (int i = 0; i < r.length; i++)
    {
        logger.debug(r[i]);
    }
}
 
Example #11
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testFetchSpaceBeforeSize() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        // Fetch without partial as reference
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID FETCH 1 (BODY[HEADER])", null);
            }
        });
        FetchResponse fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(3, fetchResponse.getItemCount()); // UID, BODY, FLAGS

        BODY body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        final String content = new String(body.getByteArray().getNewBytes());

        UID uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size only
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<50>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID, BODY

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        assertEquals(50, body.getByteArray().getCount());

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size and zero offset
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<0.30>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID , BODY

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        assertEquals(30, body.getByteArray().getCount());

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size and non zero offset
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<10.30>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID and SIZE

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        final ByteArray byteArray = body.getByteArray();
        assertEquals(30, byteArray.getCount());
        assertEquals(content.substring(10, 10 + 30), new String(byteArray.getNewBytes()));

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);
    } finally {
        store.close();
    }
}
 
Example #12
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearchSequenceSet() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH 1", null);
            }
        });
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("1", response.getRest());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH 2:2", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertTrue(ret[1].isOK());
        assertEquals("2", response.getRest());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH 2:4", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("2 3 4", response.getRest());
        assertTrue(ret[1].isOK());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH 1,2:4,8", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("1 2 3 4 8", response.getRest());
        assertTrue(ret[1].isOK());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("SEARCH 1,2:4 3,8", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals("3", response.getRest());
        assertTrue(ret[1].isOK());
    } finally {
        store.close();
    }
}
 
Example #13
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testUidSearchSequenceSet() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        final Message[] messages = folder.getMessages();
        Map<Integer, Long> uids = new HashMap<>();
        for (Message msg : messages) {
            uids.put(msg.getMessageNumber(), folder.getUID(msg));
        }

        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH 1", null);
            }
        });
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals(uids.get(1).toString(), response.getRest());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH 2:2", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertTrue(ret[1].isOK());
        assertEquals(uids.get(2).toString(), response.getRest());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH 2:4", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals(msnListToUidString(uids, 2, 3, 4), response.getRest());
        assertTrue(ret[1].isOK());

        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH 1,2:4,8", null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals(msnListToUidString(uids, 1, 2, 3, 4, 8), response.getRest());
        assertTrue(ret[1].isOK());
    } finally {
        store.close();
    }
}
 
Example #14
Source File: IMAPMockFolder.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@Override
public void handleResponse(final Response r) {
    throw new RuntimeException("not implemented/should not happen");
}
 
Example #15
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testUidSearchText() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        final Message[] messages = folder.getMessages();
        Map<Integer, String> uids = new HashMap<>();
        for (Message msg : messages) {
            uids.put(msg.getMessageNumber(), Long.toString(folder.getUID(msg)));
        }

        // messages[2] contains content with search text, match must be case insensitive
        final String searchText1 = "conTEnt2";
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH TEXT " + searchText1, null);
            }
        });
        IMAPResponse response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        assertEquals(uids.get(messages[2].getMessageNumber()), response.getRest());

        // messages[2] contains search text in CC, with different upper case
        final String searchText2 = "foo@localHOST";
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID SEARCH TEXT " + searchText2, null);
            }
        });
        response = (IMAPResponse) ret[0];
        assertFalse(response.isBAD());
        // Match all
        assertArrayEquals(uids.values().toArray(), response.getRest().split(" "));
    } finally {
        store.close();
    }
}
 
Example #16
Source File: LoginLogoutTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
private void mockCreateOrderItems() {
  mockValidPostRestCall(Response.OK, "/tools.descartes.teastore.persistence/rest/orderitems");
}
 
Example #17
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the UID of the first message in folder
 * 
 * @param folder Folder containing the message
 * @param msn message sequence number
 * @return UID of the first message
 * @throws MessagingException
 */
private static Long getMessageUid(IMAPFolder folder, final int msn) throws MessagingException
{
    return (Long) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            String command = "FETCH " + msn + " (UID)";
            Response[] r = p.command(command, null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message UID");
            }
            
            for(int i = 0 ; i < r.length; i++)
            {
                if(r[i] instanceof FetchResponse)
                {
                    FetchResponse fetchResponse = (FetchResponse) r[0];
                    UID uid = (UID) fetchResponse.getItem(UID.class);
                    logger.debug("SECNUM=" + uid.seqnum + ", UID="+uid.uid);
                    return uid.uid;
                }
            }
            
            /**
              * Uh-oh - this is where we would intermittently fall over with a class cast exception.
              * The following code probes why we don't have a FetchResponse
              */
            StringBuffer sb = new StringBuffer();
            sb.append("command="+command);
            sb.append('\n');
            sb.append("resp length=" + r.length);
            sb.append('\n');
            for(int i = 0 ; i < r.length; i++)
            {
                logger.error(r[i]);
                sb.append("class=" + r[i].getClass().getName());
                IMAPResponse unexpected = (IMAPResponse)r[i];
                sb.append("key=" + unexpected.getKey());
                sb.append("number=" + unexpected.getNumber());
                sb.append("rest=" + unexpected.getRest());
             
                sb.append("r[" + i + "]=" + r[i] + '\n');
            }
            throw new ProtocolException("getMessageUid: "+ sb.toString());
        }
    });
}