1
14
15 package com.liferay.portal.servlet;
16
17 import com.liferay.portal.events.EventsProcessorUtil;
18 import com.liferay.portal.kernel.events.ActionException;
19 import com.liferay.portal.kernel.json.JSONFactoryUtil;
20 import com.liferay.portal.kernel.json.JSONObject;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.messaging.DestinationNames;
24 import com.liferay.portal.kernel.messaging.MessageBusUtil;
25 import com.liferay.portal.kernel.servlet.PortletSessionTracker;
26 import com.liferay.portal.kernel.util.BasePortalLifecycle;
27 import com.liferay.portal.kernel.util.PropsKeys;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.util.PortalInstances;
31 import com.liferay.portal.util.PropsValues;
32 import com.liferay.portal.util.WebKeys;
33
34 import javax.servlet.http.HttpSession;
35 import javax.servlet.http.HttpSessionEvent;
36
37 import org.apache.struts.Globals;
38
39
44 public class PortalSessionDestroyer extends BasePortalLifecycle {
45
46 public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
47 _httpSessionEvent = httpSessionEvent;
48
49 registerPortalLifecycle(METHOD_INIT);
50 }
51
52 protected void doPortalDestroy() {
53 }
54
55 protected void doPortalInit() {
56 if (PropsValues.SESSION_DISABLED) {
57 return;
58 }
59
60 HttpSession session = _httpSessionEvent.getSession();
61
62 PortalSessionContext.remove(session.getId());
63
64 try {
65 Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
66
67 if (userIdObj == null) {
68 if (_log.isWarnEnabled()) {
69 _log.warn("User id is not in the session");
70 }
71 }
72
73 if (userIdObj == null) {
74 return;
75 }
76
77
79 session.removeAttribute(Globals.LOCALE_KEY);
80
81
83 if (PropsValues.LIVE_USERS_ENABLED) {
84 long userId = userIdObj.longValue();
85 long companyId = getCompanyId(userId);
86 String sessionId = session.getId();
87
88 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
89
90 jsonObject.put("command", "signOut");
91 jsonObject.put("companyId", companyId);
92 jsonObject.put("userId", userId);
93 jsonObject.put("sessionId", sessionId);
94
95 MessageBusUtil.sendMessage(
96 DestinationNames.LIVE_USERS, jsonObject.toString());
97 }
98 }
99 catch (IllegalStateException ise) {
100 if (_log.isWarnEnabled()) {
101 _log.warn(
102 "Please upgrade to a Servlet 2.4 compliant container");
103 }
104 }
105 catch (Exception e) {
106 _log.error(e, e);
107 }
108
109 try {
110 PortletSessionTracker portletSessionTracker =
111 (PortletSessionTracker)session.getAttribute(
112 WebKeys.PORTLET_SESSION_TRACKER);
113
114 if (portletSessionTracker != null) {
115 PortletSessionTracker.invalidate(session);
116
117 session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
118 }
119 }
120 catch (IllegalStateException ise) {
121 if (_log.isWarnEnabled()) {
122 _log.warn(ise, ise);
123 }
124 }
125
126
128 try {
129 EventsProcessorUtil.process(
130 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
131 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
132 }
133 catch (ActionException ae) {
134 _log.error(ae, ae);
135 }
136 }
137
138 protected long getCompanyId(long userId) throws Exception {
139 long[] companyIds = PortalInstances.getCompanyIds();
140
141 long companyId = 0;
142
143 if (companyIds.length == 1) {
144 companyId = companyIds[0];
145 }
146 else if (companyIds.length > 1) {
147 try {
148 User user = UserLocalServiceUtil.getUserById(userId);
149
150 companyId = user.getCompanyId();
151 }
152 catch (Exception e) {
153 if (_log.isWarnEnabled()) {
154 _log.warn(
155 "Unable to set the company id for user " + userId, e);
156 }
157 }
158 }
159
160 return companyId;
161 }
162
163 private static Log _log = LogFactoryUtil.getLog(
164 PortalSessionDestroyer.class);
165
166 private HttpSessionEvent _httpSessionEvent;
167
168 }