Search in sources :

Example 1 with AccountWithModifications

use of com.zimbra.soap.type.AccountWithModifications 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 AccountWithModifications

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

the class WaitSetRequest method processCallback.

private static void processCallback(WaitSetResp resp, WaitSetCallback cb, String waitSetId, String lastKnownSeqNo, boolean expand) throws ServiceException {
    cb.ws.doneWaiting(cb);
    resp.setWaitSetId(waitSetId);
    if (cb.canceled) {
        resp.setCanceled(true);
    } else if (cb.completed) {
        resp.setSeqNo(cb.seqNo);
        for (String signalledAccount : cb.signalledAccounts) {
            WaitSetSession signalledSession = cb.signalledSessions.get(signalledAccount);
            int lastChangeId = 0;
            Set<Integer> folderInterests = null;
            if (signalledSession != null) {
                folderInterests = signalledSession.getFolderInterest();
                if (signalledSession.getMailbox() != null) {
                    lastChangeId = signalledSession.getMailbox().getLastChangeID();
                }
            } else {
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(signalledAccount, false);
                if (mbox != null) {
                    lastChangeId = mbox.getLastChangeID();
                }
            }
            AccountWithModifications info = new AccountWithModifications(signalledAccount, lastChangeId);
            @SuppressWarnings("rawtypes") PendingModifications accountMods = cb.pendingModifications.get(signalledAccount);
            Map<Integer, PendingFolderModifications> folderMap = PendingModifications.encodeIMAPFolderModifications(accountMods, folderInterests);
            if (folderInterests != null && !folderInterests.isEmpty() && !folderMap.isEmpty()) {
                // interested only in specific folders
                if (expand) {
                    info.setPendingFolderModifications(folderMap.values());
                }
                resp.addSignalledAccount(info);
            } else if (folderInterests == null || folderInterests.isEmpty()) {
                // interested in any folder
                resp.addSignalledAccount(info);
            }
        }
    } else {
        // timed out....they should try again
        resp.setSeqNo(lastKnownSeqNo);
    }
    resp.setErrors(encodeErrors(cb.errors));
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) Mailbox(com.zimbra.cs.mailbox.Mailbox) AccountWithModifications(com.zimbra.soap.type.AccountWithModifications) PendingModifications(com.zimbra.cs.session.PendingModifications) WaitSetSession(com.zimbra.cs.session.WaitSetSession) Map(java.util.Map)

Example 3 with AccountWithModifications

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

the class TestWaitSetRequest method testSyncWaitSetRequest.

@Test
public void testSyncWaitSetRequest() throws Exception {
    String user1Name = "testSyncWaitSetRequest_user1";
    acc1 = TestUtil.createAccount(user1Name);
    ZMailbox mbox = TestUtil.getZMailbox(user1Name);
    String authToken = mbox.getAuthToken().getValue();
    CreateWaitSetResponse resp = createWaitSet(mbox.getAccountInfo(false).getId(), authToken);
    assertNotNull(resp);
    waitSetId = resp.getWaitSetId();
    int seq = resp.getSequence();
    WaitSetRequest waitSet = new WaitSetRequest(waitSetId, Integer.toString(seq));
    WaitSetResponse wsResp = (WaitSetResponse) sendReq(envelope(authToken, jaxbToString(waitSet), "urn:zimbra"), TestUtil.getSoapUrl() + "WaitSetRequest");
    assertEquals("0", wsResp.getSeqNo());
    String subject = NAME_PREFIX + " test wait set request 1";
    TestUtil.addMessageLmtp(subject, user1Name, "user999@example.com");
    // try { Thread.sleep(500); } catch (Exception e) {}
    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");
    assertFalse(wsResp.getSeqNo().equals("0"));
    List<AccountWithModifications> accounts = wsResp.getSignalledAccounts();
    assertEquals("should have signaled 1 account", 1, accounts.size());
    assertEquals(String.format("Shold have signaled account %s", acc1.getId()), acc1.getId(), accounts.get(0).getId());
    assertNull("Should not return folder notifications unless 'expand' is set to 'true'", accounts.get(0).getPendingFolderModifications());
}
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) ZMailbox(com.zimbra.client.ZMailbox) 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) AccountWithModifications(com.zimbra.soap.type.AccountWithModifications) AdminCreateWaitSetResponse(com.zimbra.soap.admin.message.AdminCreateWaitSetResponse) CreateWaitSetResponse(com.zimbra.soap.mail.message.CreateWaitSetResponse) Test(org.junit.Test)

Example 4 with AccountWithModifications

use of com.zimbra.soap.type.AccountWithModifications 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 5 with AccountWithModifications

use of com.zimbra.soap.type.AccountWithModifications 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)

Aggregations

AccountWithModifications (com.zimbra.soap.type.AccountWithModifications)6 ZMailbox (com.zimbra.client.ZMailbox)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 AdminCreateWaitSetResponse (com.zimbra.soap.admin.message.AdminCreateWaitSetResponse)3 AdminWaitSetRequest (com.zimbra.soap.admin.message.AdminWaitSetRequest)3 AdminWaitSetResponse (com.zimbra.soap.admin.message.AdminWaitSetResponse)3 PendingFolderModifications (com.zimbra.soap.mail.type.PendingFolderModifications)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3 ZFolder (com.zimbra.client.ZFolder)2 AdminCreateWaitSetRequest (com.zimbra.soap.admin.message.AdminCreateWaitSetRequest)2 AdminDestroyWaitSetRequest (com.zimbra.soap.admin.message.AdminDestroyWaitSetRequest)2 AdminDestroyWaitSetResponse (com.zimbra.soap.admin.message.AdminDestroyWaitSetResponse)2 QueryWaitSetRequest (com.zimbra.soap.admin.message.QueryWaitSetRequest)2 QueryWaitSetResponse (com.zimbra.soap.admin.message.QueryWaitSetResponse)2 CreateWaitSetRequest (com.zimbra.soap.mail.message.CreateWaitSetRequest)2 CreateWaitSetResponse (com.zimbra.soap.mail.message.CreateWaitSetResponse)2 WaitSetRequest (com.zimbra.soap.mail.message.WaitSetRequest)2 WaitSetResponse (com.zimbra.soap.mail.message.WaitSetResponse)2 HashSet (java.util.HashSet)2