1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.EventsProcessorUtil;
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  public class PortalSessionListener implements HttpSessionListener {
52  
53      public void sessionCreated(HttpSessionEvent event) {
54          if (PropsValues.SESSION_DISABLED) {
55              return;
56          }
57  
58          HttpSession session = event.getSession();
59  
60          PortalSessionContext.put(session.getId(), session);
61  
62          // Process session created events
63  
64          try {
65              EventsProcessorUtil.process(
66                  PropsKeys.SERVLET_SESSION_CREATE_EVENTS,
67                  PropsValues.SERVLET_SESSION_CREATE_EVENTS, session);
68          }
69          catch (ActionException ae) {
70              _log.error(ae, ae);
71          }
72      }
73  
74      public void sessionDestroyed(HttpSessionEvent event) {
75          if (PropsValues.SESSION_DISABLED) {
76              return;
77          }
78  
79          HttpSession session = event.getSession();
80  
81          PortalSessionContext.remove(session.getId());
82  
83          try {
84              Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
85  
86              if (userIdObj == null) {
87                  _log.warn("User id is not in the session");
88              }
89  
90              if (userIdObj == null) {
91                  return;
92              }
93  
94              // Language
95  
96              session.removeAttribute(Globals.LOCALE_KEY);
97  
98              // Live users
99  
100             if (PropsValues.LIVE_USERS_ENABLED) {
101                 long userId = userIdObj.longValue();
102                 long companyId = getCompanyId(userId);
103                 String sessionId = session.getId();
104 
105                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
106 
107                 jsonObj.put("command", "signOut");
108                 jsonObj.put("companyId", companyId);
109                 jsonObj.put("userId", userId);
110                 jsonObj.put("sessionId", sessionId);
111 
112                 MessageBusUtil.sendMessage(
113                     DestinationNames.LIVE_USERS, jsonObj);
114             }
115         }
116         catch (IllegalStateException ise) {
117             _log.warn("Please upgrade to a servlet 2.4 compliant container");
118         }
119         catch (Exception e) {
120             _log.error(e, e);
121         }
122 
123         session.removeAttribute(WebKeys.PORTLET_SESSION_TRACKER);
124 
125         // Process session destroyed events
126 
127         try {
128             EventsProcessorUtil.process(
129                 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
130                 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
131         }
132         catch (ActionException ae) {
133             _log.error(ae, ae);
134         }
135     }
136 
137     protected long getCompanyId(long userId) throws Exception {
138         long[] companyIds = PortalInstances.getCompanyIds();
139 
140         long companyId = 0;
141 
142         if (companyIds.length == 1) {
143             companyId = companyIds[0];
144         }
145         else if (companyIds.length > 1) {
146             try {
147                 User user = UserLocalServiceUtil.getUserById(userId);
148 
149                 companyId = user.getCompanyId();
150             }
151             catch (Exception e) {
152                 if (_log.isWarnEnabled()) {
153                     _log.warn(
154                         "Unable to set the company id for user " + userId, e);
155                 }
156             }
157         }
158 
159         return companyId;
160     }
161 
162     private static Log _log =
163         LogFactoryUtil.getLog(PortalSessionListener.class);
164 
165 }