View Javadoc

1   /*
2    * $Id: ProcessTokenAction.java 421486 2006-07-13 03:37:08Z wsmoak $
3    *
4    * Copyright 2005 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 examples.token;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.struts.action.Action;
25  import org.apache.struts.action.ActionErrors;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.apache.struts.action.ActionMessage;
30  import org.apache.struts.action.ActionMessages;
31  
32  /***
33   * Retrieve and process data from the submitted form
34   *
35   * @version $Rev: 421486 $ $Date: 2006-07-12 20:37:08 -0700 (Wed, 12 Jul 2006) $
36   */
37  public class ProcessTokenAction extends Action {
38  
39      // ------------------------------------------------------------ Constructors
40  
41      /***
42       * Constructor for ProcessOptionsAction.
43       */
44      public ProcessTokenAction() {
45          super();
46      }
47  
48      // ---------------------------------------------------------- Action Methods
49  
50      /***
51       * Process the request and return an <code>ActionForward</code> instance
52       * describing where and how control should be forwarded, or
53       * <code>null</code>if the response has already been completed.
54       *
55       * @param mapping The ActionMapping used to select this instance
56       * @param form The optional ActionForm bean for this request (if any)
57       * @param request The HTTP request we are processing
58       * @param response The HTTP response we are creating
59       *
60       * @exception Exception if the application logic throws an exception
61       *
62       * @return the ActionForward for the next view
63       */
64      public ActionForward execute(
65          ActionMapping mapping,
66          ActionForm form,
67          HttpServletRequest request,
68          HttpServletResponse response)
69          throws Exception {
70  
71          // If user pressed 'Cancel' button,
72          // return to home page
73          if (isCancelled(request)) {
74              return mapping.findForward("home");
75          }
76  
77          ActionErrors errors = new ActionErrors();
78  
79          // Prevent unintentional duplication submissions by checking
80          // that we have not received this token previously
81          if (!isTokenValid(request)) {
82              errors.add(
83                  ActionMessages.GLOBAL_MESSAGE,
84                  new ActionMessage("errors.token"));
85          }
86          resetToken(request);
87  
88          // Report any errors we have discovered back to the original form
89          if (!errors.isEmpty()) {
90              saveErrors(request, errors);
91              saveToken(request);
92              return (mapping.getInputForward());
93          }
94  
95          // Forward to result page
96          return mapping.findForward("success");
97      }
98  
99  }