001    /**
002     * Copyright (c) 2000-2012 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.servlet.PortalSessionContext;
028    import com.liferay.portal.kernel.servlet.PortletSessionTracker;
029    import com.liferay.portal.kernel.util.BasePortalLifecycle;
030    import com.liferay.portal.kernel.util.PropsKeys;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.security.auth.AuthenticatedUserUUIDStoreUtil;
033    import com.liferay.portal.service.CompanyLocalServiceUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portal.util.WebKeys;
036    
037    import javax.servlet.http.HttpSession;
038    import javax.servlet.http.HttpSessionEvent;
039    
040    import org.apache.struts.Globals;
041    
042    /**
043     * @author Michael Young
044     */
045    public class PortalSessionDestroyer extends BasePortalLifecycle {
046    
047            public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
048                    _httpSessionEvent = httpSessionEvent;
049    
050                    registerPortalLifecycle(METHOD_INIT);
051            }
052    
053            @Override
054            protected void doPortalDestroy() {
055            }
056    
057            @Override
058            protected void doPortalInit() {
059                    if (PropsValues.SESSION_DISABLED) {
060                            return;
061                    }
062    
063                    HttpSession session = _httpSessionEvent.getSession();
064    
065                    PortalSessionContext.remove(session.getId());
066    
067                    try {
068                            Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
069    
070                            if (userIdObj == null) {
071                                    if (_log.isWarnEnabled()) {
072                                            _log.warn("User id is not in the session");
073                                    }
074                            }
075    
076                            if (userIdObj == null) {
077                                    return;
078                            }
079    
080                            // Language
081    
082                            session.removeAttribute(Globals.LOCALE_KEY);
083    
084                            // Live users
085    
086                            if (PropsValues.LIVE_USERS_ENABLED) {
087                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
088    
089                                    ClusterNode clusterNode =
090                                            ClusterExecutorUtil.getLocalClusterNode();
091    
092                                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
093                                    jsonObject.put("command", "signOut");
094    
095                                    long userId = userIdObj.longValue();
096    
097                                    long companyId = CompanyLocalServiceUtil.getCompanyIdByUserId(
098                                            userId);
099    
100                                    jsonObject.put("companyId", companyId);
101                                    jsonObject.put("sessionId", session.getId());
102                                    jsonObject.put("userId", userId);
103    
104                                    MessageBusUtil.sendMessage(
105                                            DestinationNames.LIVE_USERS, jsonObject.toString());
106                            }
107    
108                            String userUUID = (String)session.getAttribute(WebKeys.USER_UUID);
109    
110                            if (Validator.isNotNull(userUUID)) {
111                                    AuthenticatedUserUUIDStoreUtil.unregister(userUUID);
112                            }
113                    }
114                    catch (IllegalStateException ise) {
115                            if (_log.isWarnEnabled()) {
116                                    _log.warn(
117                                            "Please upgrade to a Servlet 2.4 compliant container");
118                            }
119                    }
120                    catch (Exception e) {
121                            _log.error(e, e);
122                    }
123    
124                    try {
125                            PortletSessionTracker portletSessionTracker =
126                                    (PortletSessionTracker)session.getAttribute(
127                                            WebKeys.PORTLET_SESSION_TRACKER);
128    
129                            if (portletSessionTracker != null) {
130                                    PortletSessionTracker.invalidate(session);
131    
132                                    session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
133                            }
134                    }
135                    catch (IllegalStateException ise) {
136                            if (_log.isWarnEnabled()) {
137                                    _log.warn(ise, ise);
138                            }
139                    }
140    
141                    // Process session destroyed events
142    
143                    try {
144                            EventsProcessorUtil.process(
145                                    PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
146                                    PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
147                    }
148                    catch (ActionException ae) {
149                            _log.error(ae, ae);
150                    }
151            }
152    
153            private static Log _log = LogFactoryUtil.getLog(
154                    PortalSessionDestroyer.class);
155    
156            private HttpSessionEvent _httpSessionEvent;
157    
158    }