Search in sources :

Example 6 with SoapSession

use of com.zimbra.cs.session.SoapSession in project zm-mailbox by Zimbra.

the class Mailbox method snapshotCounts.

void snapshotCounts() throws ServiceException {
    // for write ops, update the "new messages" count in the DB appropriately
    OperationContext octxt = currentChange().octxt;
    RedoableOp player = currentChange().getRedoPlayer();
    RedoableOp recorder = currentChange().recorder;
    if (recorder != null && (player == null || (octxt != null && !octxt.isRedo()))) {
        assert (currentChange().writeChange);
        boolean isNewMessage = recorder.getOperation() == MailboxOperation.CreateMessage;
        if (isNewMessage) {
            CreateMessage cm = (CreateMessage) recorder;
            if (cm.getFolderId() == ID_FOLDER_SPAM || cm.getFolderId() == ID_FOLDER_TRASH) {
                isNewMessage = false;
            } else if ((cm.getFlags() & NON_DELIVERY_FLAGS) != 0) {
                isNewMessage = false;
            } else if (octxt != null && octxt.getSession() != null && !octxt.isDelegatedRequest(this)) {
                isNewMessage = false;
            }
            if (isNewMessage) {
                String folderList = getAccount().getPrefMailFoldersCheckedForNewMsgIndicator();
                if (folderList != null) {
                    String[] folderIds = folderList.split(",");
                    isNewMessage = false;
                    for (int i = 0; i < folderIds.length; i++) {
                        if (cm.getFolderId() == Integer.parseInt(folderIds[i])) {
                            isNewMessage = true;
                            break;
                        }
                    }
                }
            }
        }
        if (isNewMessage) {
            currentChange().recent = mData.recentMessages + 1;
        } else if (octxt != null && mData.recentMessages != 0) {
            Session s = octxt.getSession();
            if (s instanceof SoapSession || (s instanceof SoapSession.DelegateSession && ((SoapSession.DelegateSession) s).getParentSession().isOfflineSoapSession())) {
                currentChange().recent = 0;
            }
        }
    }
    if (currentChange().isMailboxRowDirty(mData)) {
        assert (currentChange().writeChange);
        if (currentChange().recent != MailboxChange.NO_CHANGE) {
            ZimbraLog.mailbox.debug("setting recent count to %d", currentChange().recent);
        }
        DbMailbox.updateMailboxStats(this);
    }
    boolean foldersTagsDirty = false;
    if (currentChange().dirty != null && currentChange().dirty.hasNotifications()) {
        assert (currentChange().writeChange);
        if (currentChange().dirty.created != null) {
            for (MailItem item : currentChange().dirty.created.values()) {
                if (item instanceof Folder) {
                    foldersTagsDirty = true;
                    if (item.getSize() != 0) {
                        ((Folder) item).saveFolderCounts(false);
                    }
                } else if (item instanceof Tag) {
                    foldersTagsDirty = true;
                    if (item.isUnread()) {
                        ((Tag) item).saveTagCounts();
                    }
                }
            }
        }
        if (currentChange().dirty.modified != null) {
            for (Change change : currentChange().dirty.modified.values()) {
                if (change.what instanceof Folder) {
                    foldersTagsDirty = true;
                    if ((change.why & (Change.UNREAD | Change.SIZE)) != 0) {
                        ((Folder) change.what).saveFolderCounts(false);
                    }
                } else if (change.what instanceof Tag) {
                    foldersTagsDirty = true;
                    if ((change.why & Change.UNREAD | Change.SIZE) != 0) {
                        ((Tag) change.what).saveTagCounts();
                    }
                } else if ((change.what instanceof MailItem)) {
                    if (change.what instanceof Conversation) {
                        uncache((MailItem) change.what);
                    } else {
                        cache((MailItem) change.what);
                    }
                }
            }
        }
        if (currentChange().dirty.deleted != null) {
            for (Change change : currentChange().dirty.deleted.values()) {
                if (change.what instanceof Folder || change.what instanceof Tag) {
                    foldersTagsDirty = true;
                    break;
                }
            }
        }
        if (foldersTagsDirty) {
            cacheFoldersTagsToMemcached();
        }
    }
    if (DebugConfig.checkMailboxCacheConsistency && currentChange().dirty != null && currentChange().dirty.hasNotifications()) {
        if (currentChange().dirty.created != null) {
            for (MailItem item : currentChange().dirty.created.values()) {
                DbMailItem.consistencyCheck(item, item.mData, item.encodeMetadata().toString());
            }
        }
        if (currentChange().dirty.modified != null) {
            for (Change change : currentChange().dirty.modified.values()) {
                if (change.what instanceof MailItem) {
                    MailItem item = (MailItem) change.what;
                    DbMailItem.consistencyCheck(item, item.mData, item.encodeMetadata().toString());
                }
            }
        }
    }
}
Also used : Change(com.zimbra.cs.session.PendingModifications.Change) CreateFolder(com.zimbra.cs.redolog.op.CreateFolder) ZFolder(com.zimbra.client.ZFolder) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) DbMailItem(com.zimbra.cs.db.DbMailItem) SoapSession(com.zimbra.cs.session.SoapSession) RedoableOp(com.zimbra.cs.redolog.op.RedoableOp) CreateMessage(com.zimbra.cs.redolog.op.CreateMessage) AlterItemTag(com.zimbra.cs.redolog.op.AlterItemTag) CreateTag(com.zimbra.cs.redolog.op.CreateTag) DbTag(com.zimbra.cs.db.DbTag) DbSession(com.zimbra.cs.db.DbSession) Session(com.zimbra.cs.session.Session) SoapSession(com.zimbra.cs.session.SoapSession)

Example 7 with SoapSession

use of com.zimbra.cs.session.SoapSession in project zm-mailbox by Zimbra.

the class DocumentHandler method proxyWithNotification.

public static Element proxyWithNotification(Element request, ProxyTarget proxy, ZimbraSoapContext zscProxy, Session localSession) throws ServiceException {
    Server server = proxy.getServer();
    boolean isLocal = getLocalHostId().equalsIgnoreCase(server.getId());
    if (isLocal) {
        zscProxy.resetProxyAuthToken();
    }
    if (zscProxy.isNotificationEnabled()) {
        // if we've got a SOAP session, make sure to use the appropriate remote session ID
        if (localSession instanceof SoapSession.DelegateSession) {
            localSession = ((SoapSession.DelegateSession) localSession).getParentSession();
        }
        // note that requests proxied to *this same host* shouldn't request notification, as the existing local session collects the notifications
        if (!(localSession instanceof SoapSession) || localSession.getMailbox() == null) {
            zscProxy.disableNotifications();
        } else if (!isLocal) {
            zscProxy.setProxySession(((SoapSession) localSession).getRemoteSessionId(server));
        } else {
            zscProxy.setProxySession(localSession.getSessionId());
        }
    }
    Pair<Element, Element> envelope = proxy.execute(request, zscProxy);
    // if we've got a SOAP session, handle the returned notifications and session ID
    if (localSession instanceof SoapSession && zscProxy.isNotificationEnabled())
        ((SoapSession) localSession).handleRemoteNotifications(server, envelope.getFirst());
    return envelope.getSecond().detach();
}
Also used : SoapSession(com.zimbra.cs.session.SoapSession) Server(com.zimbra.cs.account.Server) Element(com.zimbra.common.soap.Element)

Example 8 with SoapSession

use of com.zimbra.cs.session.SoapSession in project zm-mailbox by Zimbra.

the class ItemAction method forceRemoteSession.

private Account forceRemoteSession(ZimbraSoapContext zsc, Map<String, Object> context, OperationContext octxt, String op, Element action) throws ServiceException {
    // only proxying notification from the user's home-server master session
    if (!zsc.isNotificationEnabled()) {
        return null;
    }
    Session session = (Session) context.get(SoapEngine.ZIMBRA_SESSION);
    if (session instanceof SoapSession.DelegateSession) {
        session = ((SoapSession.DelegateSession) session).getParentSession();
    }
    if (!(session instanceof SoapSession) || session.getMailbox() == null) {
        return null;
    }
    SoapSession ss = (SoapSession) session;
    // only have to worry about operations where things can get created in other mailboxes (regular notification works for all other cases)
    if (!op.equals(OP_MOVE) && !op.equals(OP_COPY) && !op.equals(OP_SPAM) && !op.equals(OP_RENAME) && !op.equals(OP_UPDATE)) {
        return null;
    }
    String folderStr = action.getAttribute(MailConstants.A_FOLDER, null);
    if (folderStr == null) {
        return null;
    }
    // recursively dereference mountpoints to find ultimate target folder
    ItemId iidFolder = new ItemId(folderStr, zsc), iidRequested = iidFolder;
    Account owner = null;
    int hopCount = 0;
    ZAuthToken zat = null;
    while (hopCount < ZimbraSoapContext.MAX_HOP_COUNT) {
        owner = Provisioning.getInstance().getAccountById(iidFolder.getAccountId());
        if (Provisioning.onLocalServer(owner)) {
            try {
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(owner);
                Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
                if (!(folder instanceof Mountpoint))
                    break;
                iidFolder = ((Mountpoint) folder).getTarget();
            } catch (ServiceException e) {
                // could be a PERM_DENIED, could be something else -- this is not the right place to fail, however
                break;
            }
        } else {
            if (zat == null) {
                AuthToken at = AuthToken.getCsrfUnsecuredAuthToken(zsc.getAuthToken());
                String pxyAuthToken = at.getProxyAuthToken();
                zat = pxyAuthToken == null ? at.toZAuthToken() : new ZAuthToken(pxyAuthToken);
            }
            ZMailbox.Options zoptions = new ZMailbox.Options(zat, AccountUtil.getSoapUri(owner));
            zoptions.setNoSession(true);
            zoptions.setTargetAccount(owner.getId());
            zoptions.setTargetAccountBy(Key.AccountBy.id);
            ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
            ZFolder zfolder = zmbx.getFolderById(iidFolder.toString(zsc.getAuthtokenAccountId()));
            if (!(zfolder instanceof ZMountpoint))
                break;
            iidFolder = new ItemId(((ZMountpoint) zfolder).getCanonicalRemoteId(), zsc.getAuthtokenAccountId());
        }
        hopCount++;
    }
    if (hopCount >= ZimbraSoapContext.MAX_HOP_COUNT) {
        throw MailServiceException.TOO_MANY_HOPS(iidRequested);
    }
    // avoid dereferencing the mountpoint again later on
    action.addAttribute(MailConstants.A_FOLDER, iidFolder.toString());
    // fault in a session to listen in on the target folder's mailbox
    if (iidFolder.belongsTo(session.getAuthenticatedAccountId())) {
        return null;
    } else if (iidFolder.isLocal()) {
        ss.getDelegateSession(iidFolder.getAccountId());
        return null;
    } else {
        try {
            proxyRequest(zsc.createElement(MailConstants.NO_OP_REQUEST), context, owner.getId());
            return owner;
        } catch (ServiceException e) {
            return null;
        }
    }
}
Also used : ZMountpoint(com.zimbra.client.ZMountpoint) Account(com.zimbra.cs.account.Account) ImapFolder(com.zimbra.cs.datasource.imap.ImapFolder) ZFolder(com.zimbra.client.ZFolder) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) ZAuthToken(com.zimbra.common.auth.ZAuthToken) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) ZMountpoint(com.zimbra.client.ZMountpoint) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) SoapSession(com.zimbra.cs.session.SoapSession) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZFolder(com.zimbra.client.ZFolder) ZMountpoint(com.zimbra.client.ZMountpoint) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) SoapSession(com.zimbra.cs.session.SoapSession) Session(com.zimbra.cs.session.Session)

Aggregations

SoapSession (com.zimbra.cs.session.SoapSession)8 Session (com.zimbra.cs.session.Session)7 ServiceException (com.zimbra.common.service.ServiceException)4 Element (com.zimbra.common.soap.Element)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 ZFolder (com.zimbra.client.ZFolder)2 Account (com.zimbra.cs.account.Account)2 AccountServiceException (com.zimbra.cs.account.AccountServiceException)2 AuthToken (com.zimbra.cs.account.AuthToken)2 Server (com.zimbra.cs.account.Server)2 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)2 SessionInfo (com.zimbra.soap.ZimbraSoapContext.SessionInfo)2 ZMailbox (com.zimbra.client.ZMailbox)1 ZMountpoint (com.zimbra.client.ZMountpoint)1 ZAuthToken (com.zimbra.common.auth.ZAuthToken)1 SoapProtocol (com.zimbra.common.soap.SoapProtocol)1 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)1 AuthTokenException (com.zimbra.cs.account.AuthTokenException)1 Config (com.zimbra.cs.account.Config)1 Provisioning (com.zimbra.cs.account.Provisioning)1