001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.events.EventsProcessorUtil;
018 import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
019 import com.liferay.portal.kernel.cluster.ClusterNode;
020 import com.liferay.portal.kernel.events.ActionException;
021 import com.liferay.portal.kernel.json.JSONFactoryUtil;
022 import com.liferay.portal.kernel.json.JSONObject;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.messaging.DestinationNames;
026 import com.liferay.portal.kernel.messaging.MessageBusUtil;
027 import com.liferay.portal.kernel.servlet.PortalSessionContext;
028 import com.liferay.portal.kernel.util.BasePortalLifecycle;
029 import com.liferay.portal.kernel.util.PropsKeys;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.security.auth.AuthenticatedUserUUIDStoreUtil;
032 import com.liferay.portal.service.CompanyLocalServiceUtil;
033 import com.liferay.portal.util.PropsValues;
034 import com.liferay.portal.util.WebKeys;
035
036 import javax.servlet.http.HttpSession;
037 import javax.servlet.http.HttpSessionEvent;
038
039 import org.apache.struts.Globals;
040
041
044 public class PortalSessionDestroyer extends BasePortalLifecycle {
045
046 public PortalSessionDestroyer(HttpSessionEvent httpSessionEvent) {
047 _httpSessionEvent = httpSessionEvent;
048
049 registerPortalLifecycle(METHOD_INIT);
050 }
051
052 @Override
053 protected void doPortalDestroy() {
054 }
055
056 @Override
057 protected void doPortalInit() {
058 if (PropsValues.SESSION_DISABLED) {
059 return;
060 }
061
062 HttpSession session = _httpSessionEvent.getSession();
063
064 PortalSessionContext.remove(session.getId());
065
066 try {
067 Long userIdObj = (Long)session.getAttribute(WebKeys.USER_ID);
068
069 if (userIdObj == null) {
070 if (_log.isWarnEnabled()) {
071 _log.warn("User id is not in the session");
072 }
073 }
074
075 if (userIdObj == null) {
076 return;
077 }
078
079
080
081 session.removeAttribute(Globals.LOCALE_KEY);
082
083
084
085 if (PropsValues.LIVE_USERS_ENABLED) {
086 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
087
088 ClusterNode clusterNode =
089 ClusterExecutorUtil.getLocalClusterNode();
090
091 if (clusterNode != null) {
092 jsonObject.put(
093 "clusterNodeId", clusterNode.getClusterNodeId());
094 }
095
096 jsonObject.put("command", "signOut");
097
098 long userId = userIdObj.longValue();
099
100 long companyId = CompanyLocalServiceUtil.getCompanyIdByUserId(
101 userId);
102
103 jsonObject.put("companyId", companyId);
104 jsonObject.put("sessionId", session.getId());
105 jsonObject.put("userId", userId);
106
107 MessageBusUtil.sendMessage(
108 DestinationNames.LIVE_USERS, jsonObject.toString());
109 }
110
111 String userUUID = (String)session.getAttribute(WebKeys.USER_UUID);
112
113 if (Validator.isNotNull(userUUID)) {
114 AuthenticatedUserUUIDStoreUtil.unregister(userUUID);
115 }
116 }
117 catch (IllegalStateException ise) {
118 if (_log.isWarnEnabled()) {
119 _log.warn(
120 "Please upgrade to a Servlet 2.4 compliant container");
121 }
122 }
123 catch (Exception e) {
124 _log.error(e, e);
125 }
126
127
128
129 try {
130 EventsProcessorUtil.process(
131 PropsKeys.SERVLET_SESSION_DESTROY_EVENTS,
132 PropsValues.SERVLET_SESSION_DESTROY_EVENTS, session);
133 }
134 catch (ActionException ae) {
135 _log.error(ae, ae);
136 }
137 }
138
139 private static Log _log = LogFactoryUtil.getLog(
140 PortalSessionDestroyer.class);
141
142 private HttpSessionEvent _httpSessionEvent;
143
144 }