View Javadoc

1   /*
2    * $Id: RegistrationAction.java 421488 2006-07-13 03:43:08Z wsmoak $
3    *
4    * Copyright 2000-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.webapp.validator;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  
32  /***
33   * Implementation of <strong>Action</strong> that validates a registration form.
34   *
35   */
36  public final class RegistrationAction extends Action {
37  
38      /***
39       * Commons Logging instance.
40       */
41      private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
42  
43      /***
44       * Process the specified HTTP request, and create the corresponding HTTP
45       * response (or forward to another web component that will create it).
46       * Return an <code>ActionForward</code> instance describing where and how
47       * control should be forwarded, or <code>null</code> if the response has
48       * already been completed.
49       *
50       * @param mapping The ActionMapping used to select this instance
51       * @param form The optional ActionForm bean for this request (if any)
52       * @param request The HTTP request we are processing
53       * @param response The HTTP response we are creating
54       *
55       * @return Action to forward to
56       * @exception Exception if an input/output error or servlet exception occurs
57       */
58      public ActionForward execute(
59          ActionMapping mapping,
60          ActionForm form,
61          HttpServletRequest request,
62          HttpServletResponse response)
63          throws Exception {
64  
65          // Was this transaction cancelled?
66          if (isCancelled(request)) {
67              if (log.isInfoEnabled()) {
68                  log.info(
69                      " "
70                          + mapping.getAttribute()
71                          + " - Registration transaction was cancelled");
72              }
73  
74              removeFormBean(mapping, request);
75  
76              return (mapping.findForward("success"));
77          }
78  
79          return mapping.findForward("success");
80      }
81  
82      /***
83       * Convenience method for removing the obsolete form bean.
84       *
85       * @param mapping The ActionMapping used to select this instance
86       * @param request The HTTP request we are processing
87       */
88      protected void removeFormBean(
89          ActionMapping mapping,
90          HttpServletRequest request) {
91  
92          // Remove the obsolete form bean
93          if (mapping.getAttribute() != null) {
94              if ("request".equals(mapping.getScope())) {
95                  request.removeAttribute(mapping.getAttribute());
96              } else {
97                  HttpSession session = request.getSession();
98                  session.removeAttribute(mapping.getAttribute());
99              }
100         }
101     }
102 }