001    /**
002     * Copyright (c) 2000-2013 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                                    if (clusterNode != null) {
093                                            jsonObject.put(
094                                                    "clusterNodeId", clusterNode.getClusterNodeId());
095                                    }
096    
097                                    jsonObject.put("command", "signOut");
098    
099                                    long userId = userIdObj.longValue();
100    
101                                    long companyId = CompanyLocalServiceUtil.getCompanyIdByUserId(
102                                            userId);
103    
104                                    jsonObject.put("companyId", companyId);
105                                    jsonObject.put("sessionId", session.getId());
106                                    jsonObject.put("userId", userId);
107    
108                                    MessageBusUtil.sendMessage(
109                                            DestinationNames.LIVE_USERS, jsonObject.toString());
110                            }
111    
112                            String userUUID = (String)session.getAttribute(WebKeys.USER_UUID);
113    
114                            if (Validator.isNotNull(userUUID)) {
115                                    AuthenticatedUserUUIDStoreUtil.unregister(userUUID);
116                            }
117                    }
118                    catch (IllegalStateException ise) {
119                            if (_log.isWarnEnabled()) {
120                                    _log.warn(
121                                            "Please upgrade to a Servlet 2.4 compliant container");
122                            }
123                    }
124                    catch (Exception e) {
125                            _log.error(e, e);
126                    }
127    
128                    try {
129                            PortletSessionTracker portletSessionTracker =
130                                    (PortletSessionTracker)session.getAttribute(
131                                            WebKeys.PORTLET_SESSION_TRACKER);
132    
133                            if (portletSessionTracker != null) {
134                                    PortletSessionTracker.invalidate(session);
135    
136                                    session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
137                            }
138                    }
139                    catch (IllegalStateException ise) {
140                            if (_log.isWarnEnabled()) {
141                                    _log.warn(ise, ise);
142                            }
143                    }
144    
145                    // Process session destroyed events
146    
147                    try {
148                            EventsProcessorUtil.process(
149                                    PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
150                                    PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
151                    }
152                    catch (ActionException ae) {
153                            _log.error(ae, ae);
154                    }
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(
158                    PortalSessionDestroyer.class);
159    
160            private HttpSessionEvent _httpSessionEvent;
161    
162    }