001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            CookieKeys.addCookie(request, response, companyIdCookie);
087                            CookieKeys.addCookie(request, response, idCookie);
088                            CookieKeys.addCookie(request, response, passwordCookie);
089    
090                            try {
091                                    session.invalidate();
092                            }
093                            catch (Exception e) {
094                            }
095    
096                            EventsProcessorUtil.process(
097                                    PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
098                                    request, response);
099    
100                            request.setAttribute(WebKeys.LOGOUT, true);
101    
102                            return mapping.findForward(ActionConstants.COMMON_REFERER);
103                    }
104                    catch (Exception e) {
105                            PortalUtil.sendError(e, request, response);
106    
107                            return null;
108                    }
109            }
110    
111    }