001: /*
002: * $Id: AbstractPopulateActionForm.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.chain.contexts.ActionContext;
026: import org.apache.struts.config.ActionConfig;
027:
028: import java.util.Map;
029:
030: /**
031: * <p>Populate the form bean (if any) for this request.</p>
032: *
033: * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
034: * $
035: */
036: public abstract class AbstractPopulateActionForm extends
037: ActionCommandBase {
038: // ---------------------------------------------------------- Public Methods
039:
040: /**
041: * <p>Populate the form bean (if any) for this request.</p>
042: *
043: * @param actionCtx The <code>Context</code> for the current request
044: * @return <code>false</code> so that processing continues
045: * @throws Exception On an unexpected error
046: */
047: public boolean execute(ActionContext actionCtx) throws Exception {
048: // Is there a form bean for this request?
049: ActionForm actionForm = actionCtx.getActionForm();
050:
051: if (actionForm == null) {
052: return (false);
053: }
054:
055: // Reset the form bean property values
056: ActionConfig actionConfig = actionCtx.getActionConfig();
057:
058: reset(actionCtx, actionConfig, actionForm);
059:
060: populate(actionCtx, actionConfig, actionForm);
061:
062: handleCancel(actionCtx, actionConfig, actionForm);
063:
064: return (false);
065: }
066:
067: // ------------------------------------------------------- Protected Methods
068:
069: /**
070: * <p>Call the <code>reset()</code> method on the specified form
071: * bean.</p>
072: *
073: * @param context The context for this request
074: * @param actionConfig The actionConfig for this request
075: * @param actionForm The form bean for this request
076: */
077: protected abstract void reset(ActionContext context,
078: ActionConfig actionConfig, ActionForm actionForm);
079:
080: /**
081: * <p> Populate the given <code>ActionForm</code> with request parameter
082: * values, taking into account any prefix/suffix values configured on the
083: * given <code>ActionConfig</code>. </p>
084: *
085: * @param context The ActionContext we are processing
086: * @param actionConfig The ActionConfig we are processing
087: * @param actionForm The ActionForm we are processing
088: * @throws Exception On an unexpected error
089: */
090: protected abstract void populate(ActionContext context,
091: ActionConfig actionConfig, ActionForm actionForm)
092: throws Exception;
093:
094: // original implementation casting context to WebContext is not safe
095: // when the input value is an ActionContext.
096:
097: /**
098: * <p>For a given request parameter name, trim off any prefix and/or
099: * suffix which are defined in <code>actionConfig</code> and return what
100: * remains. If either prefix or suffix is defined, then return null for
101: * <code>name</code> values which do not begin or end accordingly.</p>
102: *
103: * @param actionConfig The ActionConfig we are processing
104: * @param name The request parameter name to proceess
105: * @return The request parameter name trimmed of any suffix or prefix
106: */
107: protected String trimParameterName(ActionConfig actionConfig,
108: String name) {
109: String stripped = name;
110: String prefix = actionConfig.getPrefix();
111: String suffix = actionConfig.getSuffix();
112:
113: if (prefix != null) {
114: if (!stripped.startsWith(prefix)) {
115: return null;
116: }
117:
118: stripped = stripped.substring(prefix.length());
119: }
120:
121: if (suffix != null) {
122: if (!stripped.endsWith(suffix)) {
123: return null;
124: }
125:
126: stripped = stripped.substring(0, stripped.length()
127: - suffix.length());
128: }
129:
130: return stripped;
131: }
132:
133: /**
134: * <p>Take into account whether the request includes any defined value for
135: * the global "cancel" parameter.</p> <p> An issue was raised (but I don't
136: * think a Bugzilla ticket created) about the security implications of
137: * using a well-known cancel property which skips form validation, as you
138: * may not write your actions to deal with the cancellation case. </p>
139: *
140: * @param context The ActionContext we are processing
141: * @param actionConfig The ActionConfig we are processing
142: * @param actionForm The ActionForm we are processing
143: * @throws Exception On an unexpected error
144: * @see Globals.CANCEL_PROPERTY
145: * @see Globals.CANCEL_PROPERTY_X
146: */
147: protected void handleCancel(ActionContext context,
148: ActionConfig actionConfig, ActionForm actionForm)
149: throws Exception {
150: Map paramValues = context.getParameterMap();
151:
152: // Set the cancellation attribute if appropriate
153: if ((paramValues.get(Globals.CANCEL_PROPERTY) != null)
154: || (paramValues.get(Globals.CANCEL_PROPERTY_X) != null)) {
155: context.setCancelled(Boolean.TRUE);
156: } else {
157: context.setCancelled(Boolean.FALSE);
158: }
159: }
160: }
|