001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.util.WebKeys;
026    
027    import javax.servlet.http.Cookie;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import javax.servlet.http.HttpSession;
031    
032    import org.apache.struts.action.Action;
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class LogoutAction extends Action {
041    
042            @Override
043            public ActionForward execute(
044                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
045                            HttpServletResponse response)
046                    throws Exception {
047    
048                    try {
049                            HttpSession session = request.getSession();
050    
051                            EventsProcessorUtil.process(
052                                    PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
053                                    request, response);
054    
055                            String domain = CookieKeys.getDomain(request);
056    
057                            Cookie companyIdCookie = new Cookie(
058                                    CookieKeys.COMPANY_ID, StringPool.BLANK);
059    
060                            if (Validator.isNotNull(domain)) {
061                                    companyIdCookie.setDomain(domain);
062                            }
063    
064                            companyIdCookie.setMaxAge(0);
065                            companyIdCookie.setPath(StringPool.SLASH);
066    
067                            Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
068    
069                            if (Validator.isNotNull(domain)) {
070                                    idCookie.setDomain(domain);
071                            }
072    
073                            idCookie.setMaxAge(0);
074                            idCookie.setPath(StringPool.SLASH);
075    
076                            Cookie passwordCookie = new Cookie(
077                                    CookieKeys.PASSWORD, StringPool.BLANK);
078    
079                            if (Validator.isNotNull(domain)) {
080                                    passwordCookie.setDomain(domain);
081                            }
082    
083                            passwordCookie.setMaxAge(0);
084                            passwordCookie.setPath(StringPool.SLASH);
085    
086                            Cookie rememberMeCookie = new Cookie(
087                                    CookieKeys.REMEMBER_ME, StringPool.BLANK);
088    
089                            if (Validator.isNotNull(domain)) {
090                                    rememberMeCookie.setDomain(domain);
091                            }
092    
093                            rememberMeCookie.setMaxAge(0);
094                            rememberMeCookie.setPath(StringPool.SLASH);
095    
096                            CookieKeys.addCookie(request, response, companyIdCookie);
097                            CookieKeys.addCookie(request, response, idCookie);
098                            CookieKeys.addCookie(request, response, passwordCookie);
099                            CookieKeys.addCookie(request, response, rememberMeCookie);
100    
101                            try {
102                                    session.invalidate();
103                            }
104                            catch (Exception e) {
105                            }
106    
107                            EventsProcessorUtil.process(
108                                    PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
109                                    request, response);
110    
111                            request.setAttribute(WebKeys.LOGOUT, true);
112    
113                            return mapping.findForward(ActionConstants.COMMON_REFERER);
114                    }
115                    catch (Exception e) {
116                            PortalUtil.sendError(e, request, response);
117    
118                            return null;
119                    }
120            }
121    
122    }