1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
46 ActionForm actionForm = actionCtx.getActionForm();
47
48 if (actionForm == null) {
49 return (false);
50 }
51
52
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
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
92
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
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 }