View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.struts.webapp.example2;
19  
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts.action.Action;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  
31  
32  /***
33   * Implementation of <strong>Action</strong> that processes a
34   * user logoff.
35   *
36   * @author Craig R. McClanahan
37   * @version $Rev: 421494 $ $Date: 2006-07-12 20:55:17 -0700 (Wed, 12 Jul 2006) $
38   */
39  
40  public final class LogoffAction extends Action {
41  
42  
43      // ----------------------------------------------------- Instance Variables
44  
45  
46      /***
47       * The <code>Log</code> instance for this application.
48       */
49      private Log log =
50          LogFactory.getLog("org.apache.struts.webapp.Example");
51  
52  
53      // --------------------------------------------------------- Public Methods
54  
55  
56      /***
57       * Process the specified HTTP request, and create the corresponding HTTP
58       * response (or forward to another web component that will create it).
59       * Return an <code>ActionForward</code> instance describing where and how
60       * control should be forwarded, or <code>null</code> if the response has
61       * already been completed.
62       *
63       * @param mapping The ActionMapping used to select this instance
64       * @param form The optional ActionForm bean for this request (if any)
65       * @param request The HTTP request we are processing
66       * @param response The HTTP response we are creating
67       *
68       * @exception Exception if business logic throws an exception
69       */
70      public ActionForward execute(ActionMapping mapping,
71  				 ActionForm form,
72  				 HttpServletRequest request,
73  				 HttpServletResponse response)
74  	throws Exception {
75  
76  	// Extract attributes we will need
77  	HttpSession session = request.getSession();
78  	User user = (User) session.getAttribute(Constants.USER_KEY);
79  
80  	// Process this user logoff
81  	if (user != null) {
82              if (log.isDebugEnabled()) {
83                  log.debug("LogoffAction: User '" + user.getUsername() +
84                            "' logged off in session " + session.getId());
85              }
86  	} else {
87              if (log.isDebugEnabled()) {
88                  log.debug("LogoffActon: User logged off in session " +
89                            session.getId());
90              }
91  	}
92  	session.removeAttribute(Constants.SUBSCRIPTION_KEY);
93  	session.removeAttribute(Constants.USER_KEY);
94  	session.invalidate();
95  
96  	// Forward control to the specified success URI
97  	return (mapping.findForward("success"));
98  
99      }
100 
101 
102 }