View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package examples.token;
23  
24  import jakarta.servlet.http.HttpServletRequest;
25  import jakarta.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionErrors;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.apache.struts.action.ActionMessage;
33  import org.apache.struts.action.ActionMessages;
34  
35  /**
36   * Retrieve and process data from the submitted form
37   *
38   * @version $Rev$ $Date$
39   */
40  public class ProcessTokenAction extends Action {
41      private static final long serialVersionUID = -5017052903191285282L;
42  
43      // ------------------------------------------------------------ Constructors
44  
45      /**
46       * Constructor for ProcessOptionsAction.
47       */
48      public ProcessTokenAction() {
49          super();
50      }
51  
52      // ---------------------------------------------------------- Action Methods
53  
54      /**
55       * Process the request and return an <code>ActionForward</code> instance
56       * describing where and how control should be forwarded, or
57       * <code>null</code>if the response has already been completed.
58       *
59       * @param mapping The ActionMapping used to select this instance
60       * @param form The optional ActionForm bean for this request (if any)
61       * @param request The HTTP request we are processing
62       * @param response The HTTP response we are creating
63       *
64       * @exception Exception if the application logic throws an exception
65       *
66       * @return the ActionForward for the next view
67       */
68      public ActionForward execute(
69          ActionMapping mapping,
70          ActionForm form,
71          HttpServletRequest request,
72          HttpServletResponse response)
73          throws Exception {
74  
75          // If user pressed 'Cancel' button,
76          // return to home page
77          if (isCancelled(request)) {
78              return mapping.findForward("home");
79          }
80  
81          ActionErrors errors = new ActionErrors();
82  
83          // Prevent unintentional duplication submissions by checking
84          // that we have not received this token previously
85          if (!isTokenValid(request)) {
86              errors.add(
87                  ActionMessages.GLOBAL_MESSAGE,
88                  new ActionMessage("errors.token"));
89          }
90          resetToken(request);
91  
92          // Report any errors we have discovered back to the original form
93          if (!errors.isEmpty()) {
94              saveErrors(request, errors);
95              saveToken(request);
96              return (mapping.getInputForward());
97          }
98  
99          // Forward to result page
100         return mapping.findForward("success");
101     }
102 }