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