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.PortalInitable;
26  import com.liferay.portal.kernel.util.PropsKeys;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.util.PortalInstances;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portal.util.WebKeys;
32  
33  import javax.servlet.http.HttpSession;
34  import javax.servlet.http.HttpSessionEvent;
35  
36  import org.apache.struts.Globals;
37  
38  /**
39   * <a href="PortalSessionDestroyer.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Michael Young
42   */
43  public class PortalSessionDestroyer implements PortalInitable {
44  
45      public PortalSessionDestroyer(HttpSessionEvent event) {
46          _event = event;
47      }
48  
49      public void portalInit() {
50          if (PropsValues.SESSION_DISABLED) {
51              return;
52          }
53  
54          HttpSession session = _event.getSession();
55  
56          PortalSessionContext.remove(session.getId());
57  
58          try {
59              Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
60  
61              if (userIdObj == null) {
62                  _log.warn("User id is not in the session");
63              }
64  
65              if (userIdObj == null) {
66                  return;
67              }
68  
69              // Language
70  
71              session.removeAttribute(Globals.LOCALE_KEY);
72  
73              // Live users
74  
75              if (PropsValues.LIVE_USERS_ENABLED) {
76                  long userId = userIdObj.longValue();
77                  long companyId = getCompanyId(userId);
78                  String sessionId = session.getId();
79  
80                  JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
81  
82                  jsonObj.put("command", "signOut");
83                  jsonObj.put("companyId", companyId);
84                  jsonObj.put("userId", userId);
85                  jsonObj.put("sessionId", sessionId);
86  
87                  MessageBusUtil.sendMessage(
88                      DestinationNames.LIVE_USERS, jsonObj);
89              }
90          }
91          catch (IllegalStateException ise) {
92              _log.warn("Please upgrade to a servlet 2.4 compliant container");
93          }
94          catch (Exception e) {
95              _log.error(e, e);
96          }
97  
98          session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
99  
100         // Process session destroyed events
101 
102         try {
103             EventsProcessorUtil.process(
104                 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
105                 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
106         }
107         catch (ActionException ae) {
108             _log.error(ae, ae);
109         }
110     }
111 
112     protected long getCompanyId(long userId) throws Exception {
113         long[] companyIds = PortalInstances.getCompanyIds();
114 
115         long companyId = 0;
116 
117         if (companyIds.length == 1) {
118             companyId = companyIds[0];
119         }
120         else if (companyIds.length > 1) {
121             try {
122                 User user = UserLocalServiceUtil.getUserById(userId);
123 
124                 companyId = user.getCompanyId();
125             }
126             catch (Exception e) {
127                 if (_log.isWarnEnabled()) {
128                     _log.warn(
129                         "Unable to set the company id for user " + userId, e);
130                 }
131             }
132         }
133 
134         return companyId;
135     }
136 
137     private static Log _log = LogFactoryUtil.getLog(
138         PortalSessionDestroyer.class);
139 
140     private HttpSessionEvent _event;
141 
142 }