1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
47   * <a href="PortalSessionListener.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
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          // Process session created events
64  
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         // Process session destroyed events
123 
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 }