001
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.PortletSessionTracker;
026 import com.liferay.portal.kernel.util.BasePortalLifecycle;
027 import com.liferay.portal.kernel.util.PropsKeys;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portal.util.PortalInstances;
031 import com.liferay.portal.util.PropsValues;
032 import com.liferay.portal.util.WebKeys;
033
034 import javax.servlet.http.HttpSession;
035 import javax.servlet.http.HttpSessionEvent;
036
037 import org.apache.struts.Globals;
038
039
042 public class PortalSessionDestroyer extends BasePortalLifecycle {
043
044 public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
045 _httpSessionEvent = httpSessionEvent;
046
047 registerPortalLifecycle(METHOD_INIT);
048 }
049
050 protected void doPortalDestroy() {
051 }
052
053 protected void doPortalInit() {
054 if (PropsValues.SESSION_DISABLED) {
055 return;
056 }
057
058 HttpSession session = _httpSessionEvent.getSession();
059
060 PortalSessionContext.remove(session.getId());
061
062 try {
063 Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
064
065 if (userIdObj == null) {
066 if (_log.isWarnEnabled()) {
067 _log.warn("User id is not in the session");
068 }
069 }
070
071 if (userIdObj == null) {
072 return;
073 }
074
075
076
077 session.removeAttribute(Globals.LOCALE_KEY);
078
079
080
081 if (PropsValues.LIVE_USERS_ENABLED) {
082 long userId = userIdObj.longValue();
083 long companyId = getCompanyId(userId);
084 String sessionId = session.getId();
085
086 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
087
088 jsonObject.put("command", "signOut");
089 jsonObject.put("companyId", companyId);
090 jsonObject.put("userId", userId);
091 jsonObject.put("sessionId", sessionId);
092
093 MessageBusUtil.sendMessage(
094 DestinationNames.LIVE_USERS, jsonObject.toString());
095 }
096 }
097 catch (IllegalStateException ise) {
098 if (_log.isWarnEnabled()) {
099 _log.warn(
100 "Please upgrade to a Servlet 2.4 compliant container");
101 }
102 }
103 catch (Exception e) {
104 _log.error(e, e);
105 }
106
107 try {
108 PortletSessionTracker portletSessionTracker =
109 (PortletSessionTracker)session.getAttribute(
110 WebKeys.PORTLET_SESSION_TRACKER);
111
112 if (portletSessionTracker != null) {
113 PortletSessionTracker.invalidate(session);
114
115 session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
116 }
117 }
118 catch (IllegalStateException ise) {
119 if (_log.isWarnEnabled()) {
120 _log.warn(ise, ise);
121 }
122 }
123
124
125
126 try {
127 EventsProcessorUtil.process(
128 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
129 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
130 }
131 catch (ActionException ae) {
132 _log.error(ae, ae);
133 }
134 }
135
136 protected long getCompanyId(long userId) throws Exception {
137 long[] companyIds = PortalInstances.getCompanyIds();
138
139 long companyId = 0;
140
141 if (companyIds.length == 1) {
142 companyId = companyIds[0];
143 }
144 else if (companyIds.length > 1) {
145 try {
146 User user = UserLocalServiceUtil.getUserById(userId);
147
148 companyId = user.getCompanyId();
149 }
150 catch (Exception e) {
151 if (_log.isWarnEnabled()) {
152 _log.warn(
153 "Unable to set the company id for user " + userId, e);
154 }
155 }
156 }
157
158 return companyId;
159 }
160
161 private static Log _log = LogFactoryUtil.getLog(
162 PortalSessionDestroyer.class);
163
164 private HttpSessionEvent _httpSessionEvent;
165
166 }