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.util.PortalInitable;
26 import com.liferay.portal.kernel.util.PropsKeys;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.service.UserLocalServiceUtil;
29 import com.liferay.portal.util.PortalInstances;
30 import com.liferay.portal.util.PropsValues;
31 import com.liferay.portal.util.WebKeys;
32
33 import javax.servlet.http.HttpSession;
34 import javax.servlet.http.HttpSessionEvent;
35
36 import org.apache.struts.Globals;
37
38
43 public class PortalSessionDestroyer implements PortalInitable {
44
45 public PortalSessionDestroyer(HttpSessionEvent event) {
46 _event = event;
47 }
48
49 public void portalInit() {
50 if (PropsValues.SESSION_DISABLED) {
51 return;
52 }
53
54 HttpSession session = _event.getSession();
55
56 PortalSessionContext.remove(session.getId());
57
58 try {
59 Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
60
61 if (userIdObj == null) {
62 if (_log.isWarnEnabled()) {
63 _log.warn("User id is not in the session");
64 }
65 }
66
67 if (userIdObj == null) {
68 return;
69 }
70
71
73 session.removeAttribute(Globals.LOCALE_KEY);
74
75
77 if (PropsValues.LIVE_USERS_ENABLED) {
78 long userId = userIdObj.longValue();
79 long companyId = getCompanyId(userId);
80 String sessionId = session.getId();
81
82 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
83
84 jsonObj.put("command", "signOut");
85 jsonObj.put("companyId", companyId);
86 jsonObj.put("userId", userId);
87 jsonObj.put("sessionId", sessionId);
88
89 MessageBusUtil.sendMessage(
90 DestinationNames.LIVE_USERS, jsonObj);
91 }
92 }
93 catch (IllegalStateException ise) {
94 if (_log.isWarnEnabled()) {
95 _log.warn(
96 "Please upgrade to a Servlet 2.4 compliant container");
97 }
98 }
99 catch (Exception e) {
100 _log.error(e, e);
101 }
102
103 try {
104 session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
105 }
106 catch (IllegalStateException ise) {
107 if (_log.isWarnEnabled()) {
108 _log.warn(ise, ise);
109 }
110 }
111
112
114 try {
115 EventsProcessorUtil.process(
116 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
117 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
118 }
119 catch (ActionException ae) {
120 _log.error(ae, ae);
121 }
122 }
123
124 protected long getCompanyId(long userId) throws Exception {
125 long[] companyIds = PortalInstances.getCompanyIds();
126
127 long companyId = 0;
128
129 if (companyIds.length == 1) {
130 companyId = companyIds[0];
131 }
132 else if (companyIds.length > 1) {
133 try {
134 User user = UserLocalServiceUtil.getUserById(userId);
135
136 companyId = user.getCompanyId();
137 }
138 catch (Exception e) {
139 if (_log.isWarnEnabled()) {
140 _log.warn(
141 "Unable to set the company id for user " + userId, e);
142 }
143 }
144 }
145
146 return companyId;
147 }
148
149 private static Log _log = LogFactoryUtil.getLog(
150 PortalSessionDestroyer.class);
151
152 private HttpSessionEvent _event;
153
154 }