1
22
23 package com.liferay.portal.servlet;
24
25 import com.liferay.portal.events.EventsProcessor;
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
52 public class PortalSessionListener implements HttpSessionListener {
53
54 public void sessionCreated(HttpSessionEvent event) {
55 if (PropsValues.SESSION_DISABLED) {
56 return;
57 }
58
59 HttpSession session = event.getSession();
60
61 PortalSessionContext.put(session.getId(), session);
62
63
65 try {
66 EventsProcessor.process(
67 PropsKeys.SERVLET_SESSION_CREATE_EVENTS,
68 PropsValues.SERVLET_SESSION_CREATE_EVENTS, session);
69 }
70 catch (ActionException ae) {
71 _log.error(ae, ae);
72 }
73 }
74
75 public void sessionDestroyed(HttpSessionEvent event) {
76 if (PropsValues.SESSION_DISABLED) {
77 return;
78 }
79
80 HttpSession session = event.getSession();
81
82 PortalSessionContext.remove(session.getId());
83
84 try {
85 Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
86
87 if (userIdObj == null) {
88 _log.warn("User id is not in the session");
89 }
90
91 if (userIdObj == null) {
92 return;
93 }
94
95
97 session.removeAttribute(Globals.LOCALE_KEY);
98
99
101 if (PropsValues.LIVE_USERS_ENABLED) {
102 long userId = userIdObj.longValue();
103 long companyId = getCompanyId(userId);
104 String sessionId = session.getId();
105
106 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
107
108 jsonObj.put("command", "signOut");
109 jsonObj.put("companyId", companyId);
110 jsonObj.put("userId", userId);
111 jsonObj.put("sessionId", sessionId);
112
113 MessageBusUtil.sendMessage(
114 DestinationNames.LIVE_USERS, jsonObj);
115 }
116 }
117 catch (IllegalStateException ise) {
118 _log.warn("Please upgrade to a servlet 2.4 compliant container");
119 }
120 catch (Exception e) {
121 _log.error(e, e);
122 }
123
124 session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
125
126
128 try {
129 EventsProcessor.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 =
164 LogFactoryUtil.getLog(PortalSessionListener.class);
165
166 }