Search in sources :

Example 1 with Session

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

the class DumpSessions method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Server localServer = Provisioning.getInstance().getLocalServer();
    checkRight(zsc, context, localServer, Admin.R_getSessions);
    Element response = zsc.createElement(AdminConstants.DUMP_SESSIONS_RESPONSE);
    boolean includeAccounts = request.getAttributeBool(AdminConstants.A_LIST_SESSIONS, false);
    boolean groupByAccount = request.getAttributeBool(AdminConstants.A_GROUP_BY_ACCOUNT, false);
    int totalActiveSessions = 0;
    Provisioning prov = Provisioning.getInstance();
    for (Session.Type type : Session.Type.values()) {
        if (type == Session.Type.NULL)
            continue;
        if (!includeAccounts) {
            int[] stats = SessionCache.countActive(type);
            if (stats[1] == 0)
                // no active sessions, skip this type!
                continue;
            Element e = response.addElement(type.name().toLowerCase());
            totalActiveSessions += stats[1];
            e.addAttribute(AdminConstants.A_ACTIVE_ACCOUNTS, stats[0]);
            e.addAttribute(AdminConstants.A_ACTIVE_SESSIONS, stats[1]);
        } else {
            List<Session> sessions = SessionCache.getActiveSessions(type);
            if (sessions.size() == 0)
                // no active sessions, skip this type!
                continue;
            Element e = response.addElement(type.name().toLowerCase());
            totalActiveSessions += sessions.size();
            e.addAttribute(AdminConstants.A_ACTIVE_SESSIONS, sessions.size());
            if (sessions.size() == 0)
                continue;
            if (groupByAccount) {
                // stick the sessions into a big map organized by the account ID
                HashMap<String, List<Session>> /*accountid*/
                map = new HashMap<String, List<Session>>();
                for (Session s : sessions) {
                    List<Session> list = map.get(s.getAuthenticatedAccountId());
                    if (list == null) {
                        list = new ArrayList<Session>();
                        map.put(s.getAuthenticatedAccountId(), list);
                    }
                    list.add(s);
                }
                e.addAttribute(AdminConstants.A_ACTIVE_ACCOUNTS, map.size());
                for (Map.Entry<String, List<Session>> entry : map.entrySet()) {
                    Element acctElt = e.addElement(AdminConstants.A_ZIMBRA_ID);
                    acctElt.addAttribute(AdminConstants.A_NAME, getName(prov, entry.getKey()));
                    acctElt.addAttribute(AdminConstants.A_ID, entry.getKey());
                    for (Session s : entry.getValue()) {
                        encodeSession(acctElt, s, false, prov);
                    }
                }
            } else {
                int[] stats = SessionCache.countActive(type);
                e.addAttribute(AdminConstants.A_ACTIVE_ACCOUNTS, stats[0]);
                for (Session s : sessions) {
                    encodeSession(e, s, true, prov);
                }
            }
        }
    }
    response.addAttribute(AdminConstants.A_ACTIVE_SESSIONS, totalActiveSessions);
    return response;
}
Also used : Server(com.zimbra.cs.account.Server) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Provisioning(com.zimbra.cs.account.Provisioning) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Session(com.zimbra.cs.session.Session)

Example 2 with Session

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

the class ZimbraSoapContext method beginWaitForNotifications.

public boolean beginWaitForNotifications(Continuation continuation, boolean includeDelegates) throws ServiceException {
    mWaitForNotifications = true;
    continuationResume = new ResumeContinuationListener(continuation);
    Session session = SessionCache.lookup(mSessionInfo.sessionId, mAuthTokenAccountId);
    if (!(session instanceof SoapSession))
        return false;
    SoapSession ss = (SoapSession) session;
    SoapSession.RegisterNotificationResult result = ss.registerNotificationConnection(mSessionInfo.getPushChannel(!includeDelegates));
    switch(result) {
        case NO_NOTIFY:
            return false;
        case DATA_READY:
            return false;
        case BLOCKING:
            return true;
        default:
            return false;
    }
}
Also used : SoapSession(com.zimbra.cs.session.SoapSession) ResumeContinuationListener(com.zimbra.cs.servlet.continuation.ResumeContinuationListener) SoapSession(com.zimbra.cs.session.SoapSession) Session(com.zimbra.cs.session.Session)

Example 3 with Session

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

the class Mailbox method purgeListeners.

/** Cleans up and disconnects all {@link Session}s listening for
     *  notifications on this Mailbox.
     *
     * @see SessionCache#clearSession(Session) */
private void purgeListeners() {
    ZimbraLog.mailbox.debug("purging listeners");
    for (Session session : mListeners) {
        SessionCache.clearSession(session);
    }
    // this may be redundant, as Session.doCleanup should dequeue
    //   the listener, but empty the list here just to be sure
    mListeners.clear();
}
Also used : DbSession(com.zimbra.cs.db.DbSession) Session(com.zimbra.cs.session.Session) SoapSession(com.zimbra.cs.session.SoapSession)

Example 4 with Session

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

the class DocumentHandler method getSession.

/** Fetches a {@link Session} object to persist and manage state between
     *  SOAP requests.  If no appropriate session already exists, a new one
     *  is created if possible.
     *
     * @param zsc The encapsulation of the SOAP request's <tt>&lt;context</tt>
     *            element.
     * @param stype  The type of session needed.
     * @return An in-memory {@link Session} object of the specified type,
     *         referenced by the request's {@link ZimbraSoapContext} object,
     *         or <tt>null</tt>.
     * @see SessionCache#SESSION_SOAP
     * @see SessionCache#SESSION_ADMIN */
protected Session getSession(ZimbraSoapContext zsc, Session.Type stype) {
    if (zsc == null || stype == null || !zsc.isNotificationEnabled()) {
        return null;
    }
    String authAccountId = zsc.getAuthtokenAccountId();
    if (authAccountId == null) {
        return null;
    }
    // if they asked for a SOAP session on a remote host and it's a non-proxied request, we don't notify
    boolean isLocal = zsc.isAuthUserOnLocalhost();
    if (stype == Session.Type.SOAP && !isLocal && !zsc.isSessionProxied()) {
        return null;
    }
    Session s = null;
    // if the caller referenced a session of this type, fetch it from the session cache
    SessionInfo sinfo = zsc.getSessionInfo();
    if (sinfo != null) {
        s = SessionCache.lookup(sinfo.sessionId, authAccountId);
        if (s == null) {
            // purge dangling references from the context's list of referenced sessions
            ZimbraLog.session.info("requested session no longer exists: " + sinfo.sessionId);
            zsc.clearSessionInfo();
        } else if (s.getSessionType() != stype) {
            // only want a session of the appropriate type
            s = null;
        }
    }
    // if there's no valid referenced session, create a new session of the requested type
    if (s == null) {
        try {
            if (stype == Session.Type.SOAP) {
                s = SoapSessionFactory.getInstance().getSoapSession(zsc).register();
            } else if (stype == Session.Type.ADMIN) {
                s = new AdminSession(authAccountId).register();
            }
        } catch (ServiceException e) {
            ZimbraLog.session.info("exception while creating session", e);
        }
        if (s != null) {
            zsc.recordNewSession(s.getSessionId());
        }
    }
    //   (note that if the requested account is remote, getDelegateSession returns null)
    if (s instanceof SoapSession && zsc.isDelegatedRequest()) {
        Session delegate = ((SoapSession) s).getDelegateSession(zsc.getRequestedAccountId());
        if (delegate != null) {
            s = delegate;
        }
    }
    return s;
}
Also used : SoapSession(com.zimbra.cs.session.SoapSession) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) AdminSession(com.zimbra.cs.session.AdminSession) SessionInfo(com.zimbra.soap.ZimbraSoapContext.SessionInfo) AdminSession(com.zimbra.cs.session.AdminSession) SoapSession(com.zimbra.cs.session.SoapSession) Session(com.zimbra.cs.session.Session)

Example 5 with Session

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

the class DocumentHandler method proxyRequest.

protected Element proxyRequest(Element request, Map<String, Object> context, Server server, ZimbraSoapContext zsc) throws ServiceException {
    // figure out whether we can just re-dispatch or if we need to proxy via HTTP
    SoapEngine engine = (SoapEngine) context.get(SoapEngine.ZIMBRA_ENGINE);
    boolean isLocal = getLocalHostId().equalsIgnoreCase(server.getId());
    //reset proxy token if proxying locally; it could previously be set to wrong account
    if (isLocal) {
        zsc.resetProxyAuthToken();
    }
    //make sure proxy token is set correctly for current requested acct
    if (zsc.getRequestedAccountId() != null) {
        try {
            AuthToken at = zsc.getAuthToken();
            String proxyToken = getProxyAuthToken(zsc.getRequestedAccountId(), context);
            if (at != null && (at.getProxyAuthToken() == null || !at.getProxyAuthToken().equals(proxyToken))) {
                at.setProxyAuthToken(proxyToken);
            }
        } catch (ServiceException se) {
            ZimbraLog.soap.warn("failed to set proxy auth token", se);
        }
    }
    Element response = null;
    request.detach();
    if (isLocal && engine != null) {
        // executing on same server; just hand back to the SoapEngine
        Map<String, Object> contextTarget = new HashMap<String, Object>(context);
        contextTarget.put(SoapEngine.ZIMBRA_ENGINE, engine);
        contextTarget.put(SoapEngine.ZIMBRA_CONTEXT, zsc);
        if (ZimbraLog.soap.isDebugEnabled()) {
            ZimbraLog.soap.debug("Proxying request locally: targetServer=%s (id=%s) localHost=%s (id=%s)", server.getName(), server.getId(), LOCAL_HOST, LOCAL_HOST_ID);
        }
        response = engine.dispatchRequest(request, contextTarget, zsc);
        if (zsc.getResponseProtocol().isFault(response)) {
            zsc.getResponseProtocol().updateArgumentsForRemoteFault(response, zsc.getRequestedAccountId());
            throw new SoapFaultException("error in proxied request", true, response);
        }
    } else {
        // do any necessary operations before doing a cross-server proxy
        preProxy(request, context);
        // executing remotely; find our target and proxy there
        HttpServletRequest httpreq = (HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
        ProxyTarget proxy = new ProxyTarget(server.getId(), zsc.getAuthToken(), httpreq);
        if (proxyTimeout >= 0) {
            proxy.setTimeouts(proxyTimeout);
        }
        response = proxyWithNotification(request, proxy, zsc, (Session) context.get(SoapEngine.ZIMBRA_SESSION));
        // do any necessary operations after doing a cross-server proxy
        postProxy(request, response, context);
    }
    return response;
}
Also used : HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) SoapFaultException(com.zimbra.common.soap.SoapFaultException) HttpServletRequest(javax.servlet.http.HttpServletRequest) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) AuthToken(com.zimbra.cs.account.AuthToken) AdminSession(com.zimbra.cs.session.AdminSession) SoapSession(com.zimbra.cs.session.SoapSession) Session(com.zimbra.cs.session.Session)

Aggregations

Session (com.zimbra.cs.session.Session)21 SoapSession (com.zimbra.cs.session.SoapSession)11 ServiceException (com.zimbra.common.service.ServiceException)9 Element (com.zimbra.common.soap.Element)7 Mailbox (com.zimbra.cs.mailbox.Mailbox)5 AccountServiceException (com.zimbra.cs.account.AccountServiceException)4 Server (com.zimbra.cs.account.Server)4 AdminSession (com.zimbra.cs.session.AdminSession)4 AuthToken (com.zimbra.cs.account.AuthToken)3 Provisioning (com.zimbra.cs.account.Provisioning)3 DbSession (com.zimbra.cs.db.DbSession)3 ZFolder (com.zimbra.client.ZFolder)2 Account (com.zimbra.cs.account.Account)2 AuthTokenException (com.zimbra.cs.account.AuthTokenException)2 Folder (com.zimbra.cs.mailbox.Folder)2 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)2 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2