1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.EventsProcessorUtil;
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  public class LogoutAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          try {
57              HttpSession session = request.getSession();
58  
59              EventsProcessorUtil.process(
60                  PropsKeys.LOGOUT_EVENTS_PRE, PropsValues.LOGOUT_EVENTS_PRE,
61                  request, response);
62  
63              String domain = CookieKeys.getDomain(request);
64  
65              Cookie companyIdCookie = new Cookie(
66                  CookieKeys.COMPANY_ID, StringPool.BLANK);
67  
68              if (Validator.isNotNull(domain)) {
69                  companyIdCookie.setDomain(domain);
70              }
71  
72              companyIdCookie.setMaxAge(0);
73              companyIdCookie.setPath(StringPool.SLASH);
74  
75              Cookie idCookie = new Cookie(CookieKeys.ID, StringPool.BLANK);
76  
77              if (Validator.isNotNull(domain)) {
78                  idCookie.setDomain(domain);
79              }
80  
81              idCookie.setMaxAge(0);
82              idCookie.setPath(StringPool.SLASH);
83  
84              Cookie passwordCookie = new Cookie(
85                  CookieKeys.PASSWORD, StringPool.BLANK);
86  
87              if (Validator.isNotNull(domain)) {
88                  passwordCookie.setDomain(domain);
89              }
90  
91              passwordCookie.setMaxAge(0);
92              passwordCookie.setPath(StringPool.SLASH);
93  
94              CookieKeys.addCookie(request, response, companyIdCookie);
95              CookieKeys.addCookie(request, response, idCookie);
96              CookieKeys.addCookie(request, response, passwordCookie);
97  
98              try {
99                  session.invalidate();
100             }
101             catch (Exception e) {
102             }
103 
104             EventsProcessorUtil.process(
105                 PropsKeys.LOGOUT_EVENTS_POST, PropsValues.LOGOUT_EVENTS_POST,
106                 request, response);
107 
108             return mapping.findForward(ActionConstants.COMMON_REFERER);
109         }
110         catch (Exception e) {
111             PortalUtil.sendError(e, request, response);
112 
113             return null;
114         }
115     }
116 
117 }