1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.events.EventsProcessorUtil;
18  import com.liferay.portal.kernel.util.PropsKeys;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.struts.ActionConstants;
22  import com.liferay.portal.util.CookieKeys;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.struts.action.Action;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  
36  /**
37   * <a href="LogoutAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class LogoutAction extends Action {
42  
43      public ActionForward execute(
44              ActionMapping mapping, ActionForm form, HttpServletRequest request,
45              HttpServletResponse response)
46          throws Exception {
47  
48          try {
49              HttpSession session = request.getSession();
50  
51              EventsProcessorUtil.process(
52                  PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
53                  request, response);
54  
55              String domain = CookieKeys.getDomain(request);
56  
57              Cookie companyIdCookie = new Cookie(
58                  CookieKeys.COMPANY_ID, StringPool.BLANK);
59  
60              if (Validator.isNotNull(domain)) {
61                  companyIdCookie.setDomain(domain);
62              }
63  
64              companyIdCookie.setMaxAge(0);
65              companyIdCookie.setPath(StringPool.SLASH);
66  
67              Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
68  
69              if (Validator.isNotNull(domain)) {
70                  idCookie.setDomain(domain);
71              }
72  
73              idCookie.setMaxAge(0);
74              idCookie.setPath(StringPool.SLASH);
75  
76              Cookie passwordCookie = new Cookie(
77                  CookieKeys.PASSWORD, StringPool.BLANK);
78  
79              if (Validator.isNotNull(domain)) {
80                  passwordCookie.setDomain(domain);
81              }
82  
83              passwordCookie.setMaxAge(0);
84              passwordCookie.setPath(StringPool.SLASH);
85  
86              CookieKeys.addCookie(request, response, companyIdCookie);
87              CookieKeys.addCookie(request, response, idCookie);
88              CookieKeys.addCookie(request, response, passwordCookie);
89  
90              try {
91                  session.invalidate();
92              }
93              catch (Exception e) {
94              }
95  
96              EventsProcessorUtil.process(
97                  PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
98                  request, response);
99  
100             return mapping.findForward(ActionConstants.COMMON_REFERER);
101         }
102         catch (Exception e) {
103             PortalUtil.sendError(e, request, response);
104 
105             return null;
106         }
107     }
108 
109 }