001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.struts.ActionConstants;
022    import com.liferay.portal.util.CookieKeys;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import javax.servlet.http.Cookie;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    import javax.servlet.http.HttpSession;
030    
031    import org.apache.struts.action.Action;
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class LogoutAction extends Action {
040    
041            public ActionForward execute(
042                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
043                            HttpServletResponse response)
044                    throws Exception {
045    
046                    try {
047                            HttpSession session = request.getSession();
048    
049                            EventsProcessorUtil.process(
050                                    PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
051                                    request, response);
052    
053                            String domain = CookieKeys.getDomain(request);
054    
055                            Cookie companyIdCookie = new Cookie(
056                                    CookieKeys.COMPANY_ID, StringPool.BLANK);
057    
058                            if (Validator.isNotNull(domain)) {
059                                    companyIdCookie.setDomain(domain);
060                            }
061    
062                            companyIdCookie.setMaxAge(0);
063                            companyIdCookie.setPath(StringPool.SLASH);
064    
065                            Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
066    
067                            if (Validator.isNotNull(domain)) {
068                                    idCookie.setDomain(domain);
069                            }
070    
071                            idCookie.setMaxAge(0);
072                            idCookie.setPath(StringPool.SLASH);
073    
074                            Cookie passwordCookie = new Cookie(
075                                    CookieKeys.PASSWORD, StringPool.BLANK);
076    
077                            if (Validator.isNotNull(domain)) {
078                                    passwordCookie.setDomain(domain);
079                            }
080    
081                            passwordCookie.setMaxAge(0);
082                            passwordCookie.setPath(StringPool.SLASH);
083    
084                            CookieKeys.addCookie(request, response, companyIdCookie);
085                            CookieKeys.addCookie(request, response, idCookie);
086                            CookieKeys.addCookie(request, response, passwordCookie);
087    
088                            try {
089                                    session.invalidate();
090                            }
091                            catch (Exception e) {
092                            }
093    
094                            EventsProcessorUtil.process(
095                                    PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
096                                    request, response);
097    
098                            return mapping.findForward(ActionConstants.COMMON_REFERER);
099                    }
100                    catch (Exception e) {
101                            PortalUtil.sendError(e, request, response);
102    
103                            return null;
104                    }
105            }
106    
107    }