Search in sources :

Example 1 with OpenIMAPFolderResponse

use of com.zimbra.soap.mail.message.OpenIMAPFolderResponse in project zm-mailbox by Zimbra.

the class TestZClient method testOpenImapFolder.

@Test
public void testOpenImapFolder() throws Exception {
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    Folder folder = mbox.createFolder(null, "TestOpenImapFolder", new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
    ItemIdentifier folderId = folder.getFolderItemIdentifier();
    List<ImapMessage> expected = new LinkedList<ImapMessage>();
    for (int i = 1; i <= 10; i++) {
        Message msg = TestUtil.addMessage(mbox, folderId.id, String.format("imap message %s", i), System.currentTimeMillis());
        expected.add(new ImapMessage(msg));
    }
    // test pagination
    OpenIMAPFolderParams params = new OpenIMAPFolderParams(folderId);
    // test fetching all results
    params.setLimit(100);
    OpenIMAPFolderResponse result = zmbox.fetchImapFolderChunk(params);
    assertEquals(10, result.getImapMessageInfo().size());
    assertFalse(result.getHasMore());
    // test fetching first 5
    params.setLimit(5);
    result = zmbox.fetchImapFolderChunk(params);
    List<ImapMessageInfo> messages = result.getImapMessageInfo();
    assertEquals(5, messages.size());
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getId(), expected.get(i).getMsgId());
    }
    assertTrue(result.getHasMore());
    // test fetching 5 items starting at 3rd item, (more results left)
    Integer cursorId = expected.get(2).getMsgId();
    params.setCursorId(String.valueOf(cursorId));
    result = zmbox.fetchImapFolderChunk(params);
    messages = result.getImapMessageInfo();
    assertEquals(5, messages.size());
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getId(), expected.get(i + 3).getMsgId());
    }
    assertTrue(result.getHasMore());
    // test fetching 5 items starting at 7th item, exhausting all results
    cursorId = expected.get(6).getMsgId();
    params.setCursorId(String.valueOf(cursorId));
    result = zmbox.fetchImapFolderChunk(params);
    messages = result.getImapMessageInfo();
    assertEquals(3, messages.size());
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getId(), expected.get(i + 7).getMsgId());
    }
    assertFalse(result.getHasMore());
    // test getting all messages in batches of 3, so pagination is used
    List<ImapMessageInfo> actual = zmbox.openImapFolder(folderId, 3);
    Collections.sort(expected);
    Collections.sort(actual);
    assertEquals("expected and actual lists have different lengths", expected.size(), actual.size());
    for (int i = 0; i < expected.size(); i++) {
        assertEquals(expected.get(i).getImapUid(), actual.get(i).getImapUid());
    }
}
Also used : OpenIMAPFolderResponse(com.zimbra.soap.mail.message.OpenIMAPFolderResponse) ImapMessage(com.zimbra.cs.imap.ImapMessage) ZMessage(com.zimbra.client.ZMessage) ZOutgoingMessage(com.zimbra.client.ZMailbox.ZOutgoingMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Message(com.zimbra.cs.mailbox.Message) ImapMessage(com.zimbra.cs.imap.ImapMessage) Folder(com.zimbra.cs.mailbox.Folder) ZSearchFolder(com.zimbra.client.ZSearchFolder) ZFolder(com.zimbra.client.ZFolder) LinkedList(java.util.LinkedList) ZMountpoint(com.zimbra.client.ZMountpoint) OpenIMAPFolderParams(com.zimbra.client.ZMailbox.OpenIMAPFolderParams) ItemIdentifier(com.zimbra.common.mailbox.ItemIdentifier) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ImapMessageInfo(com.zimbra.soap.mail.type.ImapMessageInfo) Test(org.junit.Test)

Example 2 with OpenIMAPFolderResponse

use of com.zimbra.soap.mail.message.OpenIMAPFolderResponse in project zm-mailbox by Zimbra.

the class OpenImapFolder method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    OpenIMAPFolderRequest req = zsc.elementToJaxb(request);
    OpenIMAPFolderResponse resp = new OpenIMAPFolderResponse();
    ItemId folderId = new ItemId(req.getFolderId(), zsc);
    if (!folderId.belongsTo(mbox)) {
        ZimbraLog.imap.info("Proxying request: OpenImapFolder folder=%s", folderId);
        return proxyRequest(request, context, folderId.getAccountId());
    }
    int limit = req.getLimit();
    ImapCursorInfo cursor = req.getCursor();
    Integer cursorMsgId = null;
    if (cursor != null) {
        cursorMsgId = new ItemId(cursor.getId(), mbox.getAccountId()).getId();
    }
    Pair<List<ImapMessage>, Boolean> openFolderResults = mbox.openImapFolder(octxt, folderId.getId(), limit, cursorMsgId);
    List<ImapMessage> msgs = openFolderResults.getFirst();
    boolean hasMore = openFolderResults.getSecond();
    Integer msgId = null;
    for (ImapMessage msg : msgs) {
        msgId = msg.getMsgId();
        int imapUid = msg.getImapUid();
        String type = msg.getType().toString();
        int flags = msg.getFlags();
        String tags = msg.getTags() == null ? null : Joiner.on(",").join(msg.getTags());
        ImapMessageInfo info = new ImapMessageInfo(msgId, imapUid, type, flags, tags);
        resp.addImapMessageInfo(info);
    }
    if (hasMore && msgId != null) {
        resp.setCursor(new ImapCursorInfo(msgId.toString()));
    }
    resp.setHasMore(hasMore);
    return zsc.jaxbToElement(resp);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) OpenIMAPFolderResponse(com.zimbra.soap.mail.message.OpenIMAPFolderResponse) ImapMessage(com.zimbra.cs.imap.ImapMessage) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) OpenIMAPFolderRequest(com.zimbra.soap.mail.message.OpenIMAPFolderRequest) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) List(java.util.List) ImapMessageInfo(com.zimbra.soap.mail.type.ImapMessageInfo) ImapCursorInfo(com.zimbra.soap.mail.type.ImapCursorInfo)

Example 3 with OpenIMAPFolderResponse

use of com.zimbra.soap.mail.message.OpenIMAPFolderResponse in project zm-mailbox by Zimbra.

the class ZMailbox method openImapFolder.

/**
 * Iterate OpenImapFolderRequests until all results have been returned
 * @param params
 * @return
 * @throws ServiceException
 */
public List<ImapMessageInfo> openImapFolder(ItemIdentifier folderId, int chunkSize) throws ServiceException {
    List<ImapMessageInfo> msgs = new ArrayList<ImapMessageInfo>();
    String cursorId = null;
    OpenIMAPFolderResponse folderInfo = null;
    do {
        OpenIMAPFolderParams params = new OpenIMAPFolderParams(folderId);
        params.setLimit(chunkSize);
        if (cursorId != null) {
            params.setCursorId(cursorId);
        }
        folderInfo = fetchImapFolderChunk(params);
        List<ImapMessageInfo> results = folderInfo.getImapMessageInfo();
        if (results.isEmpty()) {
            break;
        } else {
            ImapCursorInfo cursor = folderInfo.getCursor();
            if (cursor == null) {
                // fall back to last message of this results page
                cursorId = String.valueOf(results.get(results.size() - 1).getId());
            } else {
                cursorId = cursor.getId();
            }
            msgs.addAll(results);
        }
    } while (folderInfo.getHasMore());
    return msgs;
}
Also used : OpenIMAPFolderResponse(com.zimbra.soap.mail.message.OpenIMAPFolderResponse) ArrayList(java.util.ArrayList) ImapMessageInfo(com.zimbra.soap.mail.type.ImapMessageInfo) ImapCursorInfo(com.zimbra.soap.mail.type.ImapCursorInfo)

Aggregations

OpenIMAPFolderResponse (com.zimbra.soap.mail.message.OpenIMAPFolderResponse)3 ImapMessageInfo (com.zimbra.soap.mail.type.ImapMessageInfo)3 ImapMessage (com.zimbra.cs.imap.ImapMessage)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ImapCursorInfo (com.zimbra.soap.mail.type.ImapCursorInfo)2 ZFolder (com.zimbra.client.ZFolder)1 ZMailbox (com.zimbra.client.ZMailbox)1 OpenIMAPFolderParams (com.zimbra.client.ZMailbox.OpenIMAPFolderParams)1 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)1 ZMessage (com.zimbra.client.ZMessage)1 ZMountpoint (com.zimbra.client.ZMountpoint)1 ZSearchFolder (com.zimbra.client.ZSearchFolder)1 ItemIdentifier (com.zimbra.common.mailbox.ItemIdentifier)1 Folder (com.zimbra.cs.mailbox.Folder)1 Message (com.zimbra.cs.mailbox.Message)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 ItemId (com.zimbra.cs.service.util.ItemId)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 OpenIMAPFolderRequest (com.zimbra.soap.mail.message.OpenIMAPFolderRequest)1