001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
019    import com.liferay.portal.kernel.cluster.ClusterNode;
020    import com.liferay.portal.kernel.events.ActionException;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.messaging.DestinationNames;
026    import com.liferay.portal.kernel.messaging.MessageBusUtil;
027    import com.liferay.portal.kernel.security.auth.AuthenticatedUserUUIDStoreUtil;
028    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
029    import com.liferay.portal.kernel.servlet.PortalSessionContext;
030    import com.liferay.portal.kernel.util.BasePortalLifecycle;
031    import com.liferay.portal.kernel.util.PropsKeys;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.util.WebKeys;
034    import com.liferay.portal.util.PropsValues;
035    
036    import javax.servlet.http.HttpSession;
037    import javax.servlet.http.HttpSessionEvent;
038    
039    /**
040     * @author Michael Young
041     */
042    public class PortalSessionDestroyer extends BasePortalLifecycle {
043    
044            public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
045                    _httpSessionEvent = httpSessionEvent;
046    
047                    registerPortalLifecycle(METHOD_INIT);
048            }
049    
050            @Override
051            protected void doPortalDestroy() {
052            }
053    
054            @Override
055            protected void doPortalInit() {
056                    if (PropsValues.SESSION_DISABLED) {
057                            return;
058                    }
059    
060                    HttpSession session = _httpSessionEvent.getSession();
061    
062                    PortalSessionContext.remove(session.getId());
063    
064                    try {
065                            Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
066    
067                            if (userIdObj == null) {
068                                    if (_log.isWarnEnabled()) {
069                                            _log.warn("User id is not in the session");
070                                    }
071                            }
072    
073                            if (userIdObj == null) {
074                                    return;
075                            }
076    
077                            // Live users
078    
079                            if (PropsValues.LIVE_USERS_ENABLED) {
080                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
081    
082                                    ClusterNode clusterNode =
083                                            ClusterExecutorUtil.getLocalClusterNode();
084    
085                                    if (clusterNode != null) {
086                                            jsonObject.put(
087                                                    "clusterNodeId", clusterNode.getClusterNodeId());
088                                    }
089    
090                                    jsonObject.put("command", "signOut");
091    
092                                    long userId = userIdObj.longValue();
093    
094                                    long companyId = CompanyLocalServiceUtil.getCompanyIdByUserId(
095                                            userId);
096    
097                                    jsonObject.put("companyId", companyId);
098                                    jsonObject.put("sessionId", session.getId());
099                                    jsonObject.put("userId", userId);
100    
101                                    MessageBusUtil.sendMessage(
102                                            DestinationNames.LIVE_USERS, jsonObject.toString());
103                            }
104    
105                            String userUUID = (String)session.getAttribute(WebKeys.USER_UUID);
106    
107                            if (Validator.isNotNull(userUUID)) {
108                                    AuthenticatedUserUUIDStoreUtil.unregister(userUUID);
109                            }
110                    }
111                    catch (IllegalStateException ise) {
112                            if (_log.isWarnEnabled()) {
113                                    _log.warn(
114                                            "Please upgrade to a Servlet 2.4 compliant container");
115                            }
116                    }
117                    catch (Exception e) {
118                            _log.error(e, e);
119                    }
120    
121                    // Process session destroyed events
122    
123                    try {
124                            EventsProcessorUtil.process(
125                                    PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
126                                    PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
127                    }
128                    catch (ActionException ae) {
129                            _log.error(ae, ae);
130                    }
131            }
132    
133            private static final Log _log = LogFactoryUtil.getLog(
134                    PortalSessionDestroyer.class);
135    
136            private final HttpSessionEvent _httpSessionEvent;
137    
138    }