1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.events.EventsProcessor;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.struts.ActionConstants;
29  import com.liferay.portal.util.CookieKeys;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsValues;
33  
34  import javax.servlet.http.Cookie;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import javax.servlet.http.HttpSession;
38  
39  import org.apache.struts.action.Action;
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="LogoutAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class LogoutAction extends Action {
51  
52      public ActionForward execute(
53              ActionMapping mapping, ActionForm form, HttpServletRequest request,
54              HttpServletResponse response)
55          throws Exception {
56  
57          try {
58              HttpSession session = request.getSession();
59  
60              EventsProcessor.process(
61                  PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
62                  request, response);
63  
64              String domain = CookieKeys.getDomain(request);
65  
66              Cookie companyIdCookie = new Cookie(
67                  CookieKeys.COMPANY_ID, StringPool.BLANK);
68  
69              if (Validator.isNotNull(domain)) {
70                  companyIdCookie.setDomain(domain);
71              }
72  
73              companyIdCookie.setMaxAge(0);
74              companyIdCookie.setPath(StringPool.SLASH);
75  
76              Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
77  
78              if (Validator.isNotNull(domain)) {
79                  idCookie.setDomain(domain);
80              }
81  
82              idCookie.setMaxAge(0);
83              idCookie.setPath(StringPool.SLASH);
84  
85              Cookie passwordCookie = new Cookie(
86                  CookieKeys.PASSWORD, StringPool.BLANK);
87  
88              if (Validator.isNotNull(domain)) {
89                  passwordCookie.setDomain(domain);
90              }
91  
92              passwordCookie.setMaxAge(0);
93              passwordCookie.setPath(StringPool.SLASH);
94  
95              CookieKeys.addCookie(request, response, companyIdCookie);
96              CookieKeys.addCookie(request, response, idCookie);
97              CookieKeys.addCookie(request, response, passwordCookie);
98  
99              try {
100                 session.invalidate();
101             }
102             catch (Exception e) {
103             }
104 
105             EventsProcessor.process(
106                 PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
107                 request, response);
108 
109             return mapping.findForward(ActionConstants.COMMON_REFERER);
110         }
111         catch (Exception e) {
112             PortalUtil.sendError(e, request, response);
113 
114             return null;
115         }
116     }
117 
118 }