1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.events.EventsProcessorUtil;
23  import com.liferay.portal.kernel.events.ActionException;
24  import com.liferay.portal.kernel.json.JSONFactoryUtil;
25  import com.liferay.portal.kernel.json.JSONObject;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.messaging.DestinationNames;
29  import com.liferay.portal.kernel.messaging.MessageBusUtil;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PortalInstances;
33  import com.liferay.portal.util.PropsKeys;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portal.util.WebKeys;
36  
37  import javax.servlet.http.HttpSession;
38  import javax.servlet.http.HttpSessionEvent;
39  import javax.servlet.http.HttpSessionListener;
40  
41  import org.apache.struts.Globals;
42  
43  /**
44   * <a href="PortalSessionListener.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class PortalSessionListener implements HttpSessionListener {
50  
51      public void sessionCreated(HttpSessionEvent event) {
52          if (PropsValues.SESSION_DISABLED) {
53              return;
54          }
55  
56          HttpSession session = event.getSession();
57  
58          PortalSessionContext.put(session.getId(), session);
59  
60          // Process session created events
61  
62          try {
63              EventsProcessorUtil.process(
64                  PropsKeys.SERVLET_SESSION_CREATE_EVENTS,
65                  PropsValues.SERVLET_SESSION_CREATE_EVENTS, session);
66          }
67          catch (ActionException ae) {
68              _log.error(ae, ae);
69          }
70      }
71  
72      public void sessionDestroyed(HttpSessionEvent event) {
73          if (PropsValues.SESSION_DISABLED) {
74              return;
75          }
76  
77          HttpSession session = event.getSession();
78  
79          PortalSessionContext.remove(session.getId());
80  
81          try {
82              Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
83  
84              if (userIdObj == null) {
85                  _log.warn("User id is not in the session");
86              }
87  
88              if (userIdObj == null) {
89                  return;
90              }
91  
92              // Language
93  
94              session.removeAttribute(Globals.LOCALE_KEY);
95  
96              // Live users
97  
98              if (PropsValues.LIVE_USERS_ENABLED) {
99                  long userId = userIdObj.longValue();
100                 long companyId = getCompanyId(userId);
101                 String sessionId = session.getId();
102 
103                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
104 
105                 jsonObj.put("command", "signOut");
106                 jsonObj.put("companyId", companyId);
107                 jsonObj.put("userId", userId);
108                 jsonObj.put("sessionId", sessionId);
109 
110                 MessageBusUtil.sendMessage(
111                     DestinationNames.LIVE_USERS, jsonObj);
112             }
113         }
114         catch (IllegalStateException ise) {
115             _log.warn("Please upgrade to a servlet 2.4 compliant container");
116         }
117         catch (Exception e) {
118             _log.error(e, e);
119         }
120 
121         session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
122 
123         // Process session destroyed events
124 
125         try {
126             EventsProcessorUtil.process(
127                 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
128                 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
129         }
130         catch (ActionException ae) {
131             _log.error(ae, ae);
132         }
133     }
134 
135     protected long getCompanyId(long userId) throws Exception {
136         long[] companyIds = PortalInstances.getCompanyIds();
137 
138         long companyId = 0;
139 
140         if (companyIds.length == 1) {
141             companyId = companyIds[0];
142         }
143         else if (companyIds.length > 1) {
144             try {
145                 User user = UserLocalServiceUtil.getUserById(userId);
146 
147                 companyId = user.getCompanyId();
148             }
149             catch (Exception e) {
150                 if (_log.isWarnEnabled()) {
151                     _log.warn(
152                         "Unable to set the company id for user " + userId, e);
153                 }
154             }
155         }
156 
157         return companyId;
158     }
159 
160     private static Log _log =
161         LogFactoryUtil.getLog(PortalSessionListener.class);
162 
163 }