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