View Javadoc

1   /*
2    * $Id: AbstractPopulateActionForm.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2003-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  package org.apache.struts.chain.commands;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.chain.contexts.ActionContext;
23  import org.apache.struts.config.ActionConfig;
24  
25  import java.util.Map;
26  
27  /***
28   * <p>Populate the form bean (if any) for this request.</p>
29   *
30   * @version $Rev: 421119 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
31   *          $
32   */
33  public abstract class AbstractPopulateActionForm extends ActionCommandBase {
34      // ---------------------------------------------------------- Public Methods
35  
36      /***
37       * <p>Populate the form bean (if any) for this request.</p>
38       *
39       * @param actionCtx The <code>Context</code> for the current request
40       * @return <code>false</code> so that processing continues
41       * @throws Exception On an unexpected error
42       */
43      public boolean execute(ActionContext actionCtx)
44          throws Exception {
45          // Is there a form bean for this request?
46          ActionForm actionForm = actionCtx.getActionForm();
47  
48          if (actionForm == null) {
49              return (false);
50          }
51  
52          // Reset the form bean property values
53          ActionConfig actionConfig = actionCtx.getActionConfig();
54  
55          reset(actionCtx, actionConfig, actionForm);
56  
57          populate(actionCtx, actionConfig, actionForm);
58  
59          handleCancel(actionCtx, actionConfig, actionForm);
60  
61          return (false);
62      }
63  
64      // ------------------------------------------------------- Protected Methods
65  
66      /***
67       * <p>Call the <code>reset()</code> method on the specified form
68       * bean.</p>
69       *
70       * @param context      The context for this request
71       * @param actionConfig The actionConfig for this request
72       * @param actionForm   The form bean for this request
73       */
74      protected abstract void reset(ActionContext context,
75          ActionConfig actionConfig, ActionForm actionForm);
76  
77      /***
78       * <p> Populate the given <code>ActionForm</code> with request parameter
79       * values, taking into account any prefix/suffix values configured on the
80       * given <code>ActionConfig</code>. </p>
81       *
82       * @param context      The ActionContext we are processing
83       * @param actionConfig The ActionConfig we are processing
84       * @param actionForm   The ActionForm we are processing
85       * @throws Exception On an unexpected error
86       */
87      protected abstract void populate(ActionContext context,
88          ActionConfig actionConfig, ActionForm actionForm)
89          throws Exception;
90  
91      // original implementation casting context to WebContext is not safe
92      // when the input value is an ActionContext.
93  
94      /***
95       * <p>For a given request parameter name, trim off any prefix and/or
96       * suffix which are defined in <code>actionConfig</code> and return what
97       * remains. If either prefix or suffix is defined, then return null for
98       * <code>name</code> values which do not begin or end accordingly.</p>
99       *
100      * @param actionConfig The ActionConfig we are processing
101      * @param name         The request parameter name to proceess
102      * @return The request parameter name trimmed of any suffix or prefix
103      */
104     protected String trimParameterName(ActionConfig actionConfig, String name) {
105         String stripped = name;
106         String prefix = actionConfig.getPrefix();
107         String suffix = actionConfig.getSuffix();
108 
109         if (prefix != null) {
110             if (!stripped.startsWith(prefix)) {
111                 return null;
112             }
113 
114             stripped = stripped.substring(prefix.length());
115         }
116 
117         if (suffix != null) {
118             if (!stripped.endsWith(suffix)) {
119                 return null;
120             }
121 
122             stripped =
123                 stripped.substring(0, stripped.length() - suffix.length());
124         }
125 
126         return stripped;
127     }
128 
129     /***
130      * <p>Take into account whether the request includes any defined value for
131      * the global "cancel" parameter.</p> <p> An issue was raised (but I don't
132      * think a Bugzilla ticket created) about the security implications of
133      * using a well-known cancel property which skips form validation, as you
134      * may not write your actions to deal with the cancellation case. </p>
135      *
136      * @param context      The ActionContext we are processing
137      * @param actionConfig The ActionConfig we are processing
138      * @param actionForm   The ActionForm we are processing
139      * @throws Exception On an unexpected error
140      * @see Globals.CANCEL_PROPERTY
141      * @see Globals.CANCEL_PROPERTY_X
142      */
143     protected void handleCancel(ActionContext context,
144         ActionConfig actionConfig, ActionForm actionForm)
145         throws Exception {
146         Map paramValues = context.getParameterMap();
147 
148         // Set the cancellation attribute if appropriate
149         if ((paramValues.get(Globals.CANCEL_PROPERTY) != null)
150             || (paramValues.get(Globals.CANCEL_PROPERTY_X) != null)) {
151             context.setCancelled(Boolean.TRUE);
152         } else {
153             context.setCancelled(Boolean.FALSE);
154         }
155     }
156 }