1   /**
2    * Copyright (c) 2000-2009 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.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  /**
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              // Language
96  
97              session.removeAttribute(Globals.LOCALE_KEY);
98  
99              // Live users
100 
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         // Process session destroyed events
127 
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 }