Java tutorial
/** * Copyright (c) 2000-2005 Liferay, LLC. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.liferay.portlet.admin.action; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.servlet.http.HttpSession; import javax.servlet.jsp.PageContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import com.liferay.portal.auth.PrincipalException; import com.liferay.portal.servlet.PortalSessionContext; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.util.Constants; import com.liferay.portal.util.OmniadminUtil; import com.liferay.portal.util.PortalUtil; import com.liferay.portal.util.WebKeys; import com.liferay.util.ParamUtil; import com.liferay.util.StringUtil; import com.liferay.util.servlet.SessionErrors; /** * <a href="KillSessionAction.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * @version $Revision: 1.2 $ * */ public class KillSessionAction extends PortletAction { public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, ActionResponse res) throws Exception { if (!OmniadminUtil.isOmniadmin(PortalUtil.getUser(req).getUserId())) { SessionErrors.add(req, PrincipalException.class.getName()); setForward(req, "portlet.admin.error"); } else { try { _killSession(req, res); } catch (Exception e) { if (e != null && e instanceof PrincipalException) { SessionErrors.add(req, e.getClass().getName()); setForward(req, "portlet.admin.error"); } else { req.setAttribute(PageContext.EXCEPTION, e); setForward(req, Constants.COMMON_ERROR); } } setForward(req, "portlet.admin.list_sessions"); } } private void _killSession(ActionRequest req, ActionResponse res) throws Exception { String sessionId = ParamUtil.getString(req, "session_id"); HttpSession userSession = PortalSessionContext.get(sessionId); if (userSession != null) { try { String companyId = PortalUtil.getCompanyId(req); String sesCompanyId = (String) userSession.getAttribute(WebKeys.COMPANY_ID); if ((!req.getPortletSession().getId().equals(sessionId)) && (companyId.equals(sesCompanyId))) { userSession.invalidate(); } } catch (Exception e) { _log.error(StringUtil.stackTrace(e)); } } // Send redirect res.sendRedirect(ParamUtil.getString(req, "redirect")); } private static final Log _log = LogFactory.getLog(KillSessionAction.class); }