001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.messaging.DestinationNames;
024    import com.liferay.portal.kernel.messaging.MessageBusUtil;
025    import com.liferay.portal.kernel.servlet.PortalSessionContext;
026    import com.liferay.portal.kernel.servlet.PortletSessionTracker;
027    import com.liferay.portal.kernel.util.BasePortalLifecycle;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.security.auth.AuthenticatedUserUUIDStoreUtil;
031    import com.liferay.portal.service.CompanyLocalServiceUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.WebKeys;
034    
035    import javax.servlet.http.HttpSession;
036    import javax.servlet.http.HttpSessionEvent;
037    
038    import org.apache.struts.Globals;
039    
040    /**
041     * @author Michael Young
042     */
043    public class PortalSessionDestroyer extends BasePortalLifecycle {
044    
045            public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
046                    _httpSessionEvent = httpSessionEvent;
047    
048                    registerPortalLifecycle(METHOD_INIT);
049            }
050    
051            @Override
052            protected void doPortalDestroy() {
053            }
054    
055            @Override
056            protected void doPortalInit() {
057                    if (PropsValues.SESSION_DISABLED) {
058                            return;
059                    }
060    
061                    HttpSession session = _httpSessionEvent.getSession();
062    
063                    PortalSessionContext.remove(session.getId());
064    
065                    try {
066                            Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
067    
068                            if (userIdObj == null) {
069                                    if (_log.isWarnEnabled()) {
070                                            _log.warn("User id is not in the session");
071                                    }
072                            }
073    
074                            if (userIdObj == null) {
075                                    return;
076                            }
077    
078                            // Language
079    
080                            session.removeAttribute(Globals.LOCALE_KEY);
081    
082                            // Live users
083    
084                            if (PropsValues.LIVE_USERS_ENABLED) {
085                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
086    
087                                    jsonObject.put("command", "signOut");
088    
089                                    long userId = userIdObj.longValue();
090    
091                                    long companyId = CompanyLocalServiceUtil.getCompanyIdByUserId(
092                                            userId);
093    
094                                    jsonObject.put("companyId", companyId);
095                                    jsonObject.put("userId", userId);
096                                    jsonObject.put("sessionId", session.getId());
097    
098                                    MessageBusUtil.sendMessage(
099                                            DestinationNames.LIVE_USERS, jsonObject.toString());
100                            }
101    
102                            String userUUID = (String)session.getAttribute(WebKeys.USER_UUID);
103    
104                            if (Validator.isNotNull(userUUID)) {
105                                    AuthenticatedUserUUIDStoreUtil.unregister(userUUID);
106                            }
107                    }
108                    catch (IllegalStateException ise) {
109                            if (_log.isWarnEnabled()) {
110                                    _log.warn(
111                                            "Please upgrade to a Servlet 2.4 compliant container");
112                            }
113                    }
114                    catch (Exception e) {
115                            _log.error(e, e);
116                    }
117    
118                    try {
119                            PortletSessionTracker portletSessionTracker =
120                                    (PortletSessionTracker)session.getAttribute(
121                                            WebKeys.PORTLET_SESSION_TRACKER);
122    
123                            if (portletSessionTracker != null) {
124                                    PortletSessionTracker.invalidate(session);
125    
126                                    session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
127                            }
128                    }
129                    catch (IllegalStateException ise) {
130                            if (_log.isWarnEnabled()) {
131                                    _log.warn(ise, ise);
132                            }
133                    }
134    
135                    // Process session destroyed events
136    
137                    try {
138                            EventsProcessorUtil.process(
139                                    PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
140                                    PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
141                    }
142                    catch (ActionException ae) {
143                            _log.error(ae, ae);
144                    }
145            }
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    PortalSessionDestroyer.class);
149    
150            private HttpSessionEvent _httpSessionEvent;
151    
152    }