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.messaging.DestinationNames;
30 import com.liferay.portal.kernel.messaging.MessageBusUtil;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.UserLocalServiceUtil;
33 import com.liferay.portal.util.PortalInstances;
34 import com.liferay.portal.util.PropsKeys;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.portal.util.WebKeys;
37
38 import javax.servlet.http.HttpSession;
39 import javax.servlet.http.HttpSessionEvent;
40 import javax.servlet.http.HttpSessionListener;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
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 if (PropsValues.LIVE_USERS_ENABLED) {
96 long userId = userIdObj.longValue();
97 long companyId = getCompanyId(userId);
98 String sessionId = session.getId();
99
100 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
101
102 jsonObj.put("command", "signOut");
103 jsonObj.put("companyId", companyId);
104 jsonObj.put("userId", userId);
105 jsonObj.put("sessionId", sessionId);
106
107 MessageBusUtil.sendMessage(
108 DestinationNames.LIVE_USERS, jsonObj.toString());
109 }
110
111 session.removeAttribute(Globals.LOCALE_KEY);
112 }
113 catch (IllegalStateException ise) {
114 _log.warn("Please upgrade to a servlet 2.4 compliant container");
115 }
116 catch (Exception e) {
117 _log.error(e, e);
118 }
119
120 session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
121
122
124 try {
125 EventsProcessor.process(
126 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
127 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
128 }
129 catch (ActionException ae) {
130 _log.error(ae, ae);
131 }
132 }
133
134 protected long getCompanyId(long userId) throws Exception {
135 long[] companyIds = PortalInstances.getCompanyIds();
136
137 long companyId = 0;
138
139 if (companyIds.length == 1) {
140 companyId = companyIds[0];
141 }
142 else if (companyIds.length > 1) {
143 try {
144 User user = UserLocalServiceUtil.getUserById(userId);
145
146 companyId = user.getCompanyId();
147 }
148 catch (Exception e) {
149 if (_log.isWarnEnabled()) {
150 _log.warn(
151 "Unable to set the company id for user " + userId, e);
152 }
153 }
154 }
155
156 return companyId;
157 }
158
159 private static Log _log = LogFactory.getLog(PortalSessionListener.class);
160
161 }