1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.events.EventsProcessorUtil;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.messaging.DestinationNames;
24  import com.liferay.portal.kernel.messaging.MessageBusUtil;
25  import com.liferay.portal.kernel.util.PropsKeys;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.util.PortalInstances;
29  import com.liferay.portal.util.PropsValues;
30  import com.liferay.portal.util.WebKeys;
31  
32  import javax.servlet.http.HttpSession;
33  import javax.servlet.http.HttpSessionEvent;
34  import javax.servlet.http.HttpSessionListener;
35  
36  import org.apache.struts.Globals;
37  
38  /**
39   * <a href="PortalSessionListener.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class PortalSessionListener implements HttpSessionListener {
44  
45      public void sessionCreated(HttpSessionEvent event) {
46          if (PropsValues.SESSION_DISABLED) {
47              return;
48          }
49  
50          HttpSession session = event.getSession();
51  
52          PortalSessionContext.put(session.getId(), session);
53  
54          // Process session created events
55  
56          try {
57              EventsProcessorUtil.process(
58                  PropsKeys.SERVLET_SESSION_CREATE_EVENTS,
59                  PropsValues.SERVLET_SESSION_CREATE_EVENTS, session);
60          }
61          catch (ActionException ae) {
62              _log.error(ae, ae);
63          }
64      }
65  
66      public void sessionDestroyed(HttpSessionEvent event) {
67          if (PropsValues.SESSION_DISABLED) {
68              return;
69          }
70  
71          HttpSession session = event.getSession();
72  
73          PortalSessionContext.remove(session.getId());
74  
75          try {
76              Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
77  
78              if (userIdObj == null) {
79                  _log.warn("User id is not in the session");
80              }
81  
82              if (userIdObj == null) {
83                  return;
84              }
85  
86              // Language
87  
88              session.removeAttribute(Globals.LOCALE_KEY);
89  
90              // Live users
91  
92              if (PropsValues.LIVE_USERS_ENABLED) {
93                  long userId = userIdObj.longValue();
94                  long companyId = getCompanyId(userId);
95                  String sessionId = session.getId();
96  
97                  JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
98  
99                  jsonObj.put("command", "signOut");
100                 jsonObj.put("companyId", companyId);
101                 jsonObj.put("userId", userId);
102                 jsonObj.put("sessionId", sessionId);
103 
104                 MessageBusUtil.sendMessage(
105                     DestinationNames.LIVE_USERS, jsonObj);
106             }
107         }
108         catch (IllegalStateException ise) {
109             _log.warn("Please upgrade to a servlet 2.4 compliant container");
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113         }
114 
115         session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
116 
117         // Process session destroyed events
118 
119         try {
120             EventsProcessorUtil.process(
121                 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
122                 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
123         }
124         catch (ActionException ae) {
125             _log.error(ae, ae);
126         }
127     }
128 
129     protected long getCompanyId(long userId) throws Exception {
130         long[] companyIds = PortalInstances.getCompanyIds();
131 
132         long companyId = 0;
133 
134         if (companyIds.length == 1) {
135             companyId = companyIds[0];
136         }
137         else if (companyIds.length > 1) {
138             try {
139                 User user = UserLocalServiceUtil.getUserById(userId);
140 
141                 companyId = user.getCompanyId();
142             }
143             catch (Exception e) {
144                 if (_log.isWarnEnabled()) {
145                     _log.warn(
146                         "Unable to set the company id for user " + userId, e);
147                 }
148             }
149         }
150 
151         return companyId;
152     }
153 
154     private static Log _log = LogFactoryUtil.getLog(
155         PortalSessionListener.class);
156 
157 }