Search in sources :

Example 1 with PendingFolderModifications

use of com.zimbra.soap.mail.type.PendingFolderModifications in project zm-mailbox by Zimbra.

the class SoapSession method putQueuedNotifications.

/**
 * Write a single instance of the PendingLocalModifications structure into the
 *  passed-in <ctxt> block.
 */
protected void putQueuedNotifications(Mailbox mbox, QueuedNotifications ntfn, Element parent, ZimbraSoapContext zsc) {
    // create the base "notify" block:  <notify seq="6"/>
    Element eNotify = parent.addNonUniqueElement(ZimbraNamespace.E_NOTIFY);
    if (ntfn.getSequence() > 0) {
        eNotify.addAttribute(HeaderConstants.A_SEQNO, ntfn.getSequence());
    }
    OperationContext octxt = null;
    try {
        octxt = DocumentHandler.getOperationContext(zsc, this);
    } catch (ServiceException e) {
        ZimbraLog.session.warn("error fetching operation context for: " + zsc.getAuthtokenAccountId(), e);
        return;
    }
    boolean debug = ZimbraLog.session.isDebugEnabled();
    PendingLocalModifications pms = ntfn.mMailboxChanges;
    RemoteNotifications rns = ntfn.mRemoteChanges;
    Element eDeleted = eNotify.addUniqueElement(ZimbraNamespace.E_DELETED);
    StringBuilder deletedIds = new StringBuilder();
    if (pms != null && pms.deleted != null && pms.deleted.size() > 0) {
        for (ModificationKey mkey : pms.deleted.keySet()) {
            addDeletedNotification(mkey, deletedIds);
        }
    }
    if (rns != null && rns.deleted != null) {
        deletedIds.append(deletedIds.length() == 0 ? "" : ",").append(rns.deleted);
    }
    boolean hasLocalCreates = pms != null && pms.created != null && !pms.created.isEmpty();
    boolean hasRemoteCreates = rns != null && rns.created != null && !rns.created.isEmpty();
    boolean hasLocalModifies = pms != null && pms.modified != null && !pms.modified.isEmpty();
    boolean hasRemoteModifies = rns != null && rns.modified != null && !rns.modified.isEmpty();
    if (SoapTransport.NotificationFormat.valueOf(zsc.getNotificationFormat()) == SoapTransport.NotificationFormat.IMAP) {
        try {
            AccountWithModifications info = new AccountWithModifications(zsc.getAuthtokenAccountId(), mbox.getLastChangeID());
            Map<Integer, PendingFolderModifications> folderMods = PendingModifications.encodeIMAPFolderModifications(pms);
            info.setPendingFolderModifications(folderMods.values());
            eNotify.addUniqueElement(JaxbUtil.jaxbToElement(info, eNotify.getFactory()));
        } catch (ContainerException | ServiceException e) {
            ZimbraLog.session.error("Failed to encode IMAP notifications for a SOAP session ", e);
        }
    }
    if (hasLocalCreates || hasRemoteCreates) {
        Element eCreated = eNotify.addUniqueElement(ZimbraNamespace.E_CREATED);
        if (hasLocalCreates) {
            for (BaseItemInfo item : pms.created.values()) {
                if (item instanceof MailItem) {
                    MailItem mi = (MailItem) item;
                    ItemIdFormatter ifmt = new ItemIdFormatter(mAuthenticatedAccountId, mi.getMailbox(), false);
                    try {
                        Element elem = ToXML.encodeItem(eCreated, ifmt, octxt, mi, ToXML.NOTIFY_FIELDS);
                        // special-case notifications for new mountpoints in the authenticated user's mailbox
                        if (item instanceof Mountpoint && mbox == mi.getMailbox()) {
                            Map<ItemId, Pair<Boolean, Element>> mountpoints = new HashMap<ItemId, Pair<Boolean, Element>>(2);
                            expandLocalMountpoint(octxt, (Mountpoint) mi, eCreated.getFactory(), mountpoints);
                            expandRemoteMountpoints(octxt, zsc, mountpoints);
                            transferMountpointContents(elem, octxt, mountpoints);
                        }
                    } catch (ServiceException e) {
                        ZimbraLog.session.warn("error encoding item " + mi.getId(), e);
                        return;
                    }
                }
            }
            // sanity-check the returned element
            if (!eCreated.hasChildren() && debug) {
                ZimbraLog.session.debug("no serialied creates for item set: %s", pms.created.keySet());
            }
        }
        if (hasRemoteCreates) {
            if (debug) {
                ZimbraLog.session.debug("adding %d proxied creates", rns.created.size());
            }
            for (Element elt : rns.created) {
                if (encodingMatches(parent, elt)) {
                    eCreated.addElement(elt.clone().detach());
                } else {
                    ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
                }
            }
        }
    }
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    if (hasLocalModifies || hasRemoteModifies) {
        Element eModified = eNotify.addUniqueElement(ZimbraNamespace.E_MODIFIED);
        if (hasLocalModifies) {
            for (Change chg : pms.modified.values()) {
                if (chg.why != 0 && chg.what instanceof MailItem) {
                    MailItem item = (MailItem) chg.what;
                    try {
                        Element elt = ToXML.encodeItem(eModified, ifmt, octxt, item, chg.why);
                        if (elt == null) {
                            ModificationKey mkey = new PendingLocalModifications.ModificationKey(item);
                            addDeletedNotification(mkey, deletedIds);
                            if (debug) {
                                ZimbraLog.session.debug("marking nonserialized item as a delete: %s", mkey);
                            }
                        }
                    } catch (ServiceException e) {
                        ZimbraLog.session.warn("error encoding item " + item.getId(), e);
                        return;
                    }
                } else if (chg.why != 0 && chg.what instanceof Mailbox) {
                    ToXML.encodeMailbox(eModified, octxt, (Mailbox) chg.what, chg.why);
                }
            }
            // sanity-check the returned element
            if (!eModified.hasChildren() && debug) {
                ZimbraLog.session.debug("no serialied modifies for item set: %s", pms.modified.keySet());
            }
        }
        if (hasRemoteModifies) {
            if (debug) {
                ZimbraLog.session.debug("adding %d proxied modifies", rns.modified.size());
            }
            for (Element elt : rns.modified) {
                if (encodingMatches(parent, elt)) {
                    eModified.addElement(elt.clone().detach());
                } else {
                    ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
                }
            }
        }
    }
    if (rns != null && rns.activities != null && !rns.activities.isEmpty()) {
        for (Element elt : rns.activities) {
            if (encodingMatches(parent, elt)) {
                eNotify.addElement(elt.clone().detach());
            } else {
                ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
            }
        }
    }
    putExtraNotifications(ntfn, eNotify, ifmt);
    if (deletedIds == null || deletedIds.length() == 0) {
        eDeleted.detach();
    } else {
        eDeleted.addAttribute(A_ID, deletedIds.toString());
    }
}
Also used : AccountWithModifications(com.zimbra.soap.type.AccountWithModifications) BaseItemInfo(com.zimbra.common.mailbox.BaseItemInfo) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ModificationKey(com.zimbra.cs.session.PendingModifications.ModificationKey) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ContainerException(com.zimbra.common.soap.Element.ContainerException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) Pair(com.zimbra.common.util.Pair) OperationContext(com.zimbra.cs.mailbox.OperationContext) Change(com.zimbra.cs.session.PendingModifications.Change) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) PendingFolderModifications(com.zimbra.soap.mail.type.PendingFolderModifications)

Example 2 with PendingFolderModifications

use of com.zimbra.soap.mail.type.PendingFolderModifications in project zm-mailbox by Zimbra.

the class PendingModifications method encodeIMAPFolderModifications.

@SuppressWarnings("rawtypes")
public static Map<Integer, PendingFolderModifications> encodeIMAPFolderModifications(PendingModifications accountMods, Set<Integer> folderInterests) throws ServiceException {
    HashMap<Integer, PendingFolderModifications> folderMap = Maps.newHashMap();
    if (accountMods != null && accountMods.created != null) {
        for (Object mod : accountMods.created.values()) {
            if (mod instanceof BaseItemInfo) {
                Integer folderId = ((BaseItemInfo) mod).getFolderIdInMailbox();
                if (folderInterests != null && !folderInterests.contains(folderId)) {
                    continue;
                }
                JaxbUtil.getFolderMods(folderId, folderMap).addCreatedItem(JaxbUtil.getCreatedItemSOAP((BaseItemInfo) mod));
            }
        }
    }
    if (accountMods != null && accountMods.modified != null) {
        // aggregate tag changes so they are sent to each folder we are interested in
        List<ModifyTagNotification> tagMods = new ArrayList<ModifyTagNotification>();
        List<DeleteItemNotification> tagDeletes = new ArrayList<DeleteItemNotification>();
        for (Object maybeTagChange : accountMods.modified.values()) {
            if (maybeTagChange instanceof Change) {
                Object maybeTag = ((Change) maybeTagChange).what;
                if (maybeTag != null && maybeTag instanceof Tag) {
                    Tag tag = (Tag) maybeTag;
                    tagMods.add(new ModifyTagNotification(tag.getIdInMailbox(), tag.getName(), ((Change) maybeTagChange).why));
                }
            }
        }
        if (accountMods != null && accountMods.deleted != null) {
            @SuppressWarnings("unchecked") Map<ModificationKey, Change> deletedMap = accountMods.deleted;
            for (Map.Entry<ModificationKey, Change> entry : deletedMap.entrySet()) {
                ModificationKey key = entry.getKey();
                Change mod = entry.getValue();
                if (mod instanceof Change) {
                    Object what = mod.what;
                    if (what != null && what instanceof MailItem.Type) {
                        if (what == MailItem.Type.TAG) {
                            // aggregate tag deletions so they are sent to each folder we are interested in
                            tagDeletes.add(JaxbUtil.getDeletedItemSOAP(key.getItemId(), what.toString()));
                        } else {
                            Integer folderId;
                            if (what == MailItem.Type.FOLDER) {
                                folderId = key.getItemId();
                            } else {
                                folderId = mod.getFolderId();
                            }
                            if (folderInterests != null && !folderInterests.contains(folderId)) {
                                continue;
                            }
                            JaxbUtil.getFolderMods(folderId, folderMap).addDeletedItem(JaxbUtil.getDeletedItemSOAP(key.getItemId(), what.toString()));
                        }
                    }
                }
            }
        }
        for (Object mod : accountMods.modified.values()) {
            if (mod instanceof Change) {
                Object what = ((Change) mod).what;
                if (what != null && what instanceof BaseItemInfo) {
                    BaseItemInfo itemInfo = (BaseItemInfo) what;
                    Integer folderId = itemInfo.getFolderIdInMailbox();
                    if (itemInfo instanceof Folder) {
                        Integer itemId = itemInfo.getIdInMailbox();
                        if (folderInterests != null && !folderInterests.contains(folderId) && !folderInterests.contains(itemId)) {
                            continue;
                        }
                        if (!tagMods.isEmpty()) {
                            PendingFolderModifications folderMods = JaxbUtil.getFolderMods(itemId, folderMap);
                            for (ModifyTagNotification modTag : tagMods) {
                                folderMods.addModifiedTag(modTag);
                            }
                        } else if (!tagDeletes.isEmpty()) {
                            PendingFolderModifications folderMods = JaxbUtil.getFolderMods(itemId, folderMap);
                            for (DeleteItemNotification tagDelete : tagDeletes) {
                                folderMods.addDeletedItem(tagDelete);
                            }
                        }
                    } else if (!(itemInfo instanceof Tag)) {
                        if (folderInterests != null && !folderInterests.contains(folderId)) {
                            continue;
                        }
                        JaxbUtil.getFolderMods(folderId, folderMap).addModifiedMsg(JaxbUtil.getModifiedItemSOAP(itemInfo, ((Change) mod).why));
                    }
                }
            }
        }
    }
    return folderMap;
}
Also used : BaseItemInfo(com.zimbra.common.mailbox.BaseItemInfo) ArrayList(java.util.ArrayList) Folder(com.zimbra.cs.mailbox.Folder) Type(com.zimbra.cs.mailbox.MailItem.Type) MailItem(com.zimbra.cs.mailbox.MailItem) ZimbraMailItem(com.zimbra.common.mailbox.ZimbraMailItem) DeleteItemNotification(com.zimbra.soap.mail.type.DeleteItemNotification) Tag(com.zimbra.cs.mailbox.Tag) ModifyTagNotification(com.zimbra.soap.mail.type.ModifyNotification.ModifyTagNotification) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PendingFolderModifications(com.zimbra.soap.mail.type.PendingFolderModifications)

Example 3 with PendingFolderModifications

use of com.zimbra.soap.mail.type.PendingFolderModifications in project zm-mailbox by Zimbra.

the class TestWaitSetRequest method testFolderInterestSyncWaitSetRequest.

@Test
public void testFolderInterestSyncWaitSetRequest() throws Exception {
    String user1Name = "testFISyncWaitSetRequest_user1";
    acc1 = TestUtil.createAccount(user1Name);
    ZMailbox mbox = TestUtil.getZMailbox(user1Name);
    String acctId = mbox.getAccountId();
    String authToken = mbox.getAuthToken().getValue();
    ZimbraLog.test.info("GREN 1");
    String adminAuthToken = TestUtil.getAdminSoapTransport().getAuthToken().getValue();
    ZimbraLog.test.info("GREN 2");
    ZFolder myFolder = TestUtil.createFolder(mbox, "funFolder");
    Set<Integer> folderInterest = Sets.newHashSet();
    folderInterest.add(myFolder.getFolderIdInOwnerMailbox());
    /* initially only interested in funFolder */
    CreateWaitSetResponse resp = createWaitSet(acctId, authToken, folderInterest);
    assertNotNull(resp);
    waitSetId = resp.getWaitSetId();
    int seq = resp.getSequence();
    WaitSetRequest waitSet = new WaitSetRequest(waitSetId, Integer.toString(seq));
    WaitSetResponse wsResp = (WaitSetResponse) sendReq(waitSet, authToken, TestUtil.getSoapUrl());
    assertEquals("0", wsResp.getSeqNo());
    String subject = NAME_PREFIX + " test wait set request 1";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    TestUtil.waitForMessages(mbox, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    wsResp = (WaitSetResponse) sendReq(envelope(authToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getSoapUrl() + "WaitSetRequest");
    assertTrue(wsResp.getSeqNo().equals("0"));
    assertEquals("Number of signalled accounts", 0, wsResp.getSignalledAccounts().size());
    QueryWaitSetResponse qwsResp;
    QueryWaitSetRequest qwsReq = new QueryWaitSetRequest(waitSetId);
    qwsResp = (QueryWaitSetResponse) sendReq(qwsReq, adminAuthToken, TestUtil.getAdminSoapUrl());
    validateQueryWaitSetResponse(qwsResp, acctId, folderInterest, null);
    /* interested in funFolder AND inbox */
    folderInterest.add(Integer.valueOf(Mailbox.ID_FOLDER_INBOX));
    waitSet.addUpdateAccount(createWaitSetAddSpec(acctId, folderInterest));
    wsResp = (WaitSetResponse) sendReq(envelope(authToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getSoapUrl() + "WaitSetRequest");
    assertTrue(wsResp.getSeqNo().equals("0"));
    assertEquals("Number of signalled accounts", 0, wsResp.getSignalledAccounts().size());
    qwsResp = (QueryWaitSetResponse) sendReq(qwsReq, adminAuthToken, TestUtil.getAdminSoapUrl());
    validateQueryWaitSetResponse(qwsResp, acctId, folderInterest, null);
    subject = NAME_PREFIX + " test wait set request 2";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    TestUtil.waitForMessages(mbox, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    qwsResp = (QueryWaitSetResponse) sendReq(qwsReq, adminAuthToken, TestUtil.getAdminSoapUrl());
    validateQueryWaitSetResponse(qwsResp, acctId, folderInterest, null);
    waitSet = new WaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (WaitSetResponse) sendReq(envelope(authToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getSoapUrl() + "WaitSetRequest");
    assertFalse(wsResp.getSeqNo().equals("0"));
    assertEquals("Number of signalled accounts", 1, wsResp.getSignalledAccounts().size());
    AccountWithModifications acctInfo = wsResp.getSignalledAccounts().get(0);
    assertEquals("Signaled account id", mbox.getAccountId(), acctInfo.getId());
    Collection<PendingFolderModifications> mods = acctInfo.getPendingFolderModifications();
    assertNotNull("'mod' field should not be null", mods);
    assertEquals("Should have 1 folder object with modifications", 1, mods.size());
    Integer foldInt = mods.iterator().next().getFolderId();
    assertEquals(String.format("Folder ID should be %d (Inbox). Getting %d instead", Mailbox.ID_FOLDER_INBOX, foldInt), foldInt.intValue(), Mailbox.ID_FOLDER_INBOX);
}
Also used : CreateWaitSetRequest(com.zimbra.soap.mail.message.CreateWaitSetRequest) QueryWaitSetRequest(com.zimbra.soap.admin.message.QueryWaitSetRequest) WaitSetRequest(com.zimbra.soap.mail.message.WaitSetRequest) AdminDestroyWaitSetRequest(com.zimbra.soap.admin.message.AdminDestroyWaitSetRequest) AdminWaitSetRequest(com.zimbra.soap.admin.message.AdminWaitSetRequest) AdminCreateWaitSetRequest(com.zimbra.soap.admin.message.AdminCreateWaitSetRequest) AdminCreateWaitSetResponse(com.zimbra.soap.admin.message.AdminCreateWaitSetResponse) WaitSetResponse(com.zimbra.soap.mail.message.WaitSetResponse) AdminDestroyWaitSetResponse(com.zimbra.soap.admin.message.AdminDestroyWaitSetResponse) QueryWaitSetResponse(com.zimbra.soap.admin.message.QueryWaitSetResponse) AdminWaitSetResponse(com.zimbra.soap.admin.message.AdminWaitSetResponse) CreateWaitSetResponse(com.zimbra.soap.mail.message.CreateWaitSetResponse) QueryWaitSetResponse(com.zimbra.soap.admin.message.QueryWaitSetResponse) AccountWithModifications(com.zimbra.soap.type.AccountWithModifications) QueryWaitSetRequest(com.zimbra.soap.admin.message.QueryWaitSetRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) AdminCreateWaitSetResponse(com.zimbra.soap.admin.message.AdminCreateWaitSetResponse) CreateWaitSetResponse(com.zimbra.soap.mail.message.CreateWaitSetResponse) PendingFolderModifications(com.zimbra.soap.mail.type.PendingFolderModifications) Test(org.junit.Test)

Example 4 with PendingFolderModifications

use of com.zimbra.soap.mail.type.PendingFolderModifications in project zm-mailbox by Zimbra.

the class TestWaitSetRequest method testFolderInterestSyncAdminWaitSetRequest.

@Test
public void testFolderInterestSyncAdminWaitSetRequest() throws Exception {
    String user1Name = "testFISyncAdminWaitSetRequest_user1";
    String user2Name = "testFISyncAdminWaitSetRequest_user2";
    acc1 = TestUtil.createAccount(user1Name);
    acc2 = TestUtil.createAccount(user2Name);
    ZMailbox zMbox1 = TestUtil.getZMailbox(user1Name);
    ZMailbox zMbox2 = TestUtil.getZMailbox(user2Name);
    Mailbox mbox1 = TestUtil.getMailbox(user1Name);
    Mailbox mbox2 = TestUtil.getMailbox(user2Name);
    Set<String> accountIds = new HashSet<String>();
    String acct1Id = zMbox1.getAccountId();
    String acct2Id = zMbox2.getAccountId();
    accountIds.add(acct1Id);
    String adminAuthToken = TestUtil.getAdminSoapTransport().getAuthToken().getValue();
    ZFolder user1FunFolder = TestUtil.createFolder(zMbox1, "funFolder");
    ZFolder user2FunFolder = TestUtil.createFolder(zMbox2, "funFolder");
    ZFolder user2FunFolder2 = TestUtil.createFolder(zMbox2, "funFolder2");
    Set<Integer> folderInterest = Sets.newHashSet();
    folderInterest.add(user1FunFolder.getFolderIdInOwnerMailbox());
    // initially only interested in user1::funFolder
    AdminCreateWaitSetResponse resp = createAdminWaitSet(accountIds, adminAuthToken, false);
    assertNotNull(resp);
    waitSetId = resp.getWaitSetId();
    int seq = resp.getSequence();
    AdminWaitSetRequest waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.addUpdateAccount(createWaitSetAddSpec(acct1Id, folderInterest));
    AdminWaitSetResponse wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals(0, seq);
    String subject = NAME_PREFIX + " test wait set request 1";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox1, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    assertEquals("Number of signalled accounts", 0, wsResp.getSignalledAccounts().size());
    seq = Integer.parseInt(wsResp.getSeqNo());
    // now interested in user1::funFolder AND user1::inbox
    folderInterest.add(Integer.valueOf(Mailbox.ID_FOLDER_INBOX));
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.addUpdateAccount(createWaitSetAddSpec(acct1Id, folderInterest));
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    // nothing happened, so should not trigger any accounts
    assertEquals("Number of signalled accounts (test 1)", 0, wsResp.getSignalledAccounts().size());
    seq = Integer.parseInt(wsResp.getSeqNo());
    subject = NAME_PREFIX + " test wait set request 2";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox1, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 2)", 1, wsResp.getSignalledAccounts().size());
    AccountWithModifications acctInfo = wsResp.getSignalledAccounts().get(0);
    assertEquals("Signaled account id (should signal user1)", acct1Id, acctInfo.getId());
    Collection<PendingFolderModifications> mods = acctInfo.getPendingFolderModifications();
    assertNotNull("'mod' field should not be null", mods);
    assertEquals("Should have 1 folder object with modifications", 1, mods.size());
    Integer foldInt = mods.iterator().next().getFolderId();
    assertEquals(String.format("Folder ID should be %d (Inbox). Getting %d instead", Mailbox.ID_FOLDER_INBOX, foldInt), foldInt.intValue(), Mailbox.ID_FOLDER_INBOX);
    // Add message to user2 (should not trigger this waitset, because this waitset is not subscribed to user2 yet)
    subject = NAME_PREFIX + " test wait set request 3";
    TestUtil.addMessageLmtp(subject, user2Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox2, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 3)", 0, wsResp.getSignalledAccounts().size());
    // subscribe to user2::funFolder2
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    folderInterest = Sets.newHashSet();
    folderInterest.add(user2FunFolder2.getFolderIdInOwnerMailbox());
    waitSet.addAddAccount(createWaitSetAddSpec(acct2Id, folderInterest));
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 4)", 0, wsResp.getSignalledAccounts().size());
    // Add message to user2 (should NOT trigger this waitset yet, because WaitSet is subscribed to user2:funFolder2, user1:funFolder and user1:INBOX
    subject = NAME_PREFIX + " test wait set request 4";
    TestUtil.addMessageLmtp(subject, user2Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox2, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 5)", 0, wsResp.getSignalledAccounts().size());
    // add interest in user2:INBOX
    folderInterest = Sets.newHashSet();
    folderInterest.add(user2FunFolder2.getFolderIdInOwnerMailbox());
    folderInterest.add(Integer.valueOf(Mailbox.ID_FOLDER_INBOX));
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.addUpdateAccount(createWaitSetAddSpec(acct2Id, folderInterest));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    // nothing happened, so should not trigger any accounts
    assertEquals("Number of signalled accounts (test 6)", 0, wsResp.getSignalledAccounts().size());
    // Add message to user2:INBOX (should trigger this WatSet now)
    subject = NAME_PREFIX + " test wait set request 5";
    TestUtil.addMessageLmtp(subject, user2Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox2, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    // now user2 should be triggered
    assertEquals("Number of signalled accounts (test 7)", 1, wsResp.getSignalledAccounts().size());
    acctInfo = wsResp.getSignalledAccounts().get(0);
    assertEquals("Signaled account id (should signal user2)", acct2Id, acctInfo.getId());
    mods = acctInfo.getPendingFolderModifications();
    assertNotNull("'mod' field should not be null", mods);
    assertEquals("Should have 1 folder object with modifications", 1, mods.size());
    foldInt = mods.iterator().next().getFolderId();
    assertEquals(String.format("Folder ID should be %d (Inbox). Getting %d instead", Mailbox.ID_FOLDER_INBOX, foldInt), foldInt.intValue(), Mailbox.ID_FOLDER_INBOX);
    // Add message to user1:funFolder (should trigger this WatSet)
    subject = NAME_PREFIX + " test wait set request 6";
    TestUtil.addMessage(mbox1, user1FunFolder.getFolderIdInOwnerMailbox(), subject, System.currentTimeMillis());
    TestUtil.waitForMessages(zMbox1, String.format("in:%s is:unread \"%s\"", user1FunFolder.getName(), subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 8)", 1, wsResp.getSignalledAccounts().size());
    acctInfo = wsResp.getSignalledAccounts().get(0);
    assertEquals("Signaled account id (should signal user1)", acct1Id, acctInfo.getId());
    mods = acctInfo.getPendingFolderModifications();
    assertNotNull("'mod' field should not be null", mods);
    assertEquals("Should have 1 folder object with modifications", 1, mods.size());
    foldInt = mods.iterator().next().getFolderId();
    assertEquals(String.format("Folder ID should be %d (%s). Getting %d instead", user1FunFolder.getFolderIdInOwnerMailbox(), user1FunFolder.getName(), foldInt), foldInt.intValue(), user1FunFolder.getFolderIdInOwnerMailbox());
    // Add message to user2:funFolder (should NOT trigger this WatSet, because it is subscribed to INBOX and funFolder2 on user2)
    subject = NAME_PREFIX + " test wait set request 7";
    TestUtil.addMessage(mbox2, user2FunFolder.getFolderIdInOwnerMailbox(), subject, System.currentTimeMillis());
    TestUtil.waitForMessages(zMbox2, String.format("in:%s is:unread \"%s\"", user2FunFolder.getName(), subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 9)", 0, wsResp.getSignalledAccounts().size());
    // Add message to user2:funFolder2 (should trigger this WatSet)
    subject = NAME_PREFIX + " test wait set request 8";
    TestUtil.addMessage(mbox2, user2FunFolder2.getFolderIdInOwnerMailbox(), subject, System.currentTimeMillis());
    TestUtil.waitForMessages(zMbox2, String.format("in:%s is:unread \"%s\"", user2FunFolder2.getName(), subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 10)", 1, wsResp.getSignalledAccounts().size());
    acctInfo = wsResp.getSignalledAccounts().get(0);
    assertEquals("Signaled account id (should signal user2)", acct2Id, acctInfo.getId());
    mods = acctInfo.getPendingFolderModifications();
    assertNotNull("'mod' field should not be null", mods);
    assertEquals("Should have 1 folder object with modifications", 1, mods.size());
    foldInt = mods.iterator().next().getFolderId();
    assertEquals(String.format("Folder ID should be %d (%s). Getting %d instead", user2FunFolder2.getFolderIdInOwnerMailbox(), user2FunFolder2.getName(), foldInt), user2FunFolder2.getFolderIdInOwnerMailbox(), foldInt.intValue());
    // Add message to user2:funFolder2 and user1:INBOX (should trigger this WatSet)
    subject = NAME_PREFIX + " test wait set request 9";
    TestUtil.addMessage(mbox2, user2FunFolder2.getFolderIdInOwnerMailbox(), subject, System.currentTimeMillis());
    TestUtil.waitForMessages(zMbox2, String.format("in:%s is:unread \"%s\"", user2FunFolder2.getName(), subject), 1, 1000);
    subject = NAME_PREFIX + " test wait set request 10";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    TestUtil.waitForMessages(zMbox1, String.format("in:inbox is:unread \"%s\"", subject), 1, 1000);
    waitSet = new AdminWaitSetRequest(waitSetId, Integer.toString(seq));
    waitSet.setExpand(true);
    wsResp = (AdminWaitSetResponse) sendReq(envelope(adminAuthToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getAdminSoapUrl() + "AdminWaitSetRequest");
    seq = Integer.parseInt(wsResp.getSeqNo());
    assertEquals("Number of signalled accounts (test 11)", 2, wsResp.getSignalledAccounts().size());
    boolean user1Triggered = false;
    boolean user2Triggered = false;
    List<AccountWithModifications> accnts = wsResp.getSignalledAccounts();
    for (AccountWithModifications info : accnts) {
        if (info.getId().equalsIgnoreCase(acct1Id)) {
            user1Triggered = true;
            mods = info.getPendingFolderModifications();
            PendingFolderModifications fm = ((ArrayList<PendingFolderModifications>) mods).get(0);
            foldInt = fm.getFolderId();
            assertNotNull("'mods' field should not be null", mods);
            assertEquals("Should have 1 folder object with modifications for user1", 1, mods.size());
            assertEquals(String.format("Folder ID should be %d (INBOX). Getting %d instead. Account %s", Mailbox.ID_FOLDER_INBOX, foldInt, acct1Id), Mailbox.ID_FOLDER_INBOX, foldInt.intValue());
        }
        if (info.getId().equalsIgnoreCase(acct2Id)) {
            user2Triggered = true;
            mods = info.getPendingFolderModifications();
            assertNotNull("'mods' field should not be null", mods);
            assertEquals("Should have 1 folder object with modifications for user2", 1, mods.size());
            PendingFolderModifications fm = ((ArrayList<PendingFolderModifications>) mods).get(0);
            foldInt = fm.getFolderId();
            assertEquals(String.format("Folder ID should be %d (%s). Getting %d instead. Account %s", user2FunFolder2.getFolderIdInOwnerMailbox(), user2FunFolder2.getName(), foldInt, acct2Id), user2FunFolder2.getFolderIdInOwnerMailbox(), foldInt.intValue());
        }
    }
    assertTrue("Should have signalled user2", user2Triggered);
    assertTrue("Should have signalled user1", user1Triggered);
}
Also used : AdminWaitSetRequest(com.zimbra.soap.admin.message.AdminWaitSetRequest) AccountWithModifications(com.zimbra.soap.type.AccountWithModifications) ArrayList(java.util.ArrayList) AdminCreateWaitSetResponse(com.zimbra.soap.admin.message.AdminCreateWaitSetResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) AdminWaitSetResponse(com.zimbra.soap.admin.message.AdminWaitSetResponse) ZFolder(com.zimbra.client.ZFolder) PendingFolderModifications(com.zimbra.soap.mail.type.PendingFolderModifications) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with PendingFolderModifications

use of com.zimbra.soap.mail.type.PendingFolderModifications in project zm-mailbox by Zimbra.

the class JaxbUtil method getFolderMods.

public static PendingFolderModifications getFolderMods(Integer folderId, HashMap<Integer, PendingFolderModifications> folderMap) {
    PendingFolderModifications folderMods = folderMap.get(folderId);
    if (folderMods == null) {
        folderMods = new PendingFolderModifications(folderId);
        folderMap.put(folderId, folderMods);
    }
    return folderMods;
}
Also used : PendingFolderModifications(com.zimbra.soap.mail.type.PendingFolderModifications)

Aggregations

PendingFolderModifications (com.zimbra.soap.mail.type.PendingFolderModifications)6 AccountWithModifications (com.zimbra.soap.type.AccountWithModifications)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ZFolder (com.zimbra.client.ZFolder)2 ZMailbox (com.zimbra.client.ZMailbox)2 BaseItemInfo (com.zimbra.common.mailbox.BaseItemInfo)2 MailItem (com.zimbra.cs.mailbox.MailItem)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 AdminCreateWaitSetResponse (com.zimbra.soap.admin.message.AdminCreateWaitSetResponse)2 AdminWaitSetRequest (com.zimbra.soap.admin.message.AdminWaitSetRequest)2 AdminWaitSetResponse (com.zimbra.soap.admin.message.AdminWaitSetResponse)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 ZimbraMailItem (com.zimbra.common.mailbox.ZimbraMailItem)1 ServiceException (com.zimbra.common.service.ServiceException)1 Element (com.zimbra.common.soap.Element)1 ContainerException (com.zimbra.common.soap.Element.ContainerException)1 Pair (com.zimbra.common.util.Pair)1