1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.action;
21  
22  import com.liferay.portal.events.EventsProcessorUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.struts.ActionConstants;
26  import com.liferay.portal.util.CookieKeys;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsValues;
30  
31  import javax.servlet.http.Cookie;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.struts.action.Action;
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="LogoutAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LogoutAction extends Action {
48  
49      public ActionForward execute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              HttpSession session = request.getSession();
56  
57              EventsProcessorUtil.process(
58                  PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
59                  request, response);
60  
61              String domain = CookieKeys.getDomain(request);
62  
63              Cookie companyIdCookie = new Cookie(
64                  CookieKeys.COMPANY_ID, StringPool.BLANK);
65  
66              if (Validator.isNotNull(domain)) {
67                  companyIdCookie.setDomain(domain);
68              }
69  
70              companyIdCookie.setMaxAge(0);
71              companyIdCookie.setPath(StringPool.SLASH);
72  
73              Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
74  
75              if (Validator.isNotNull(domain)) {
76                  idCookie.setDomain(domain);
77              }
78  
79              idCookie.setMaxAge(0);
80              idCookie.setPath(StringPool.SLASH);
81  
82              Cookie passwordCookie = new Cookie(
83                  CookieKeys.PASSWORD, StringPool.BLANK);
84  
85              if (Validator.isNotNull(domain)) {
86                  passwordCookie.setDomain(domain);
87              }
88  
89              passwordCookie.setMaxAge(0);
90              passwordCookie.setPath(StringPool.SLASH);
91  
92              CookieKeys.addCookie(request, response, companyIdCookie);
93              CookieKeys.addCookie(request, response, idCookie);
94              CookieKeys.addCookie(request, response, passwordCookie);
95  
96              try {
97                  session.invalidate();
98              }
99              catch (Exception e) {
100             }
101 
102             EventsProcessorUtil.process(
103                 PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
104                 request, response);
105 
106             return mapping.findForward(ActionConstants.COMMON_REFERER);
107         }
108         catch (Exception e) {
109             PortalUtil.sendError(e, request, response);
110 
111             return null;
112         }
113     }
114 
115 }