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