001: /*
002: * $Id: EventDispatchAction.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.actions;
022:
023: import java.util.StringTokenizer;
024:
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionMapping;
033: import org.apache.struts.action.ActionForward;
034:
035: /**
036: * <p>An <strong>Action</strong> that dispatches to to one of the public methods
037: * that are named in the <code>parameter</code> attribute of the corresponding
038: * ActionMapping and matches a submission parameter. This is useful for
039: * developers who prefer to use many submit buttons, images, or submit links
040: * on a single form and whose related actions exist in a single Action class.</p>
041: *
042: * <p>The method(s) must have the same signature (other than method name) of the
043: * standard Action.execute method.</p>
044: *
045: * <p>To configure the use of this action in your
046: * <code>struts-config.xml</code> file, create an entry like this:</p>
047: *
048: * <pre><code>
049: * <action path="/saveSubscription"
050: * type="org.example.SubscriptionAction"
051: * name="subscriptionForm"
052: * scope="request"
053: * input="/subscription.jsp"
054: * parameter="save,back,recalc=recalculate,default=save"/>
055: * </code></pre>
056: *
057: * <p>where <code>parameter</code> contains three possible methods and one
058: * default method if nothing matches (such as the user pressing the enter key).</p>
059: *
060: * <p>For utility purposes, you can use the <code>key=value</code> notation to
061: * alias methods so that they are exposed as different form element names, in the
062: * event of a naming conflict or otherwise. In this example, the <em>recalc</em>
063: * button (via a request parameter) will invoke the <code>recalculate</code>
064: * method. The security-minded person may find this feature valuable to
065: * obfuscate and not expose the methods.</p>
066: *
067: * <p>The <em>default</em> key is purely optional. If this is not specified
068: * and no parameters match the list of method keys, <code>null</code> is
069: * returned which means the <code>unspecified</code> method will be invoked.</p>
070: *
071: * <p>The order of the parameters are guaranteed to be iterated in the order
072: * specified. If multiple buttons were accidently submitted, the first match in
073: * the list will be dispatched.</p>
074: *
075: * @since Struts 1.2.9
076: */
077: public class EventDispatchAction extends DispatchAction {
078:
079: /**
080: * Commons Logging instance.
081: */
082: private static final Log LOG = LogFactory
083: .getLog(EventDispatchAction.class);
084:
085: /**
086: * The method key, if present, to use if other specified method keys
087: * do not match a request parameter.
088: */
089: private static final String DEFAULT_METHOD_KEY = "default";
090:
091: // --------------------------------------------------------- Protected Methods
092:
093: /**
094: * Method which is dispatched to when there is no value for specified
095: * request parameter included in the request. Subclasses of
096: * <code>DispatchAction</code> should override this method if they wish to
097: * provide default behavior different than throwing a ServletException.
098: *
099: * @param mapping The ActionMapping used to select this instance
100: * @param form The optional ActionForm bean for this request (if any)
101: * @param request The non-HTTP request we are processing
102: * @param response The non-HTTP response we are creating
103: * @return The forward to which control should be transferred, or
104: * <code>null</code> if the response has been completed.
105: * @throws Exception if the application business logic throws an
106: * exception.
107: */
108: protected ActionForward unspecified(ActionMapping mapping,
109: ActionForm form, HttpServletRequest request,
110: HttpServletResponse response) throws Exception {
111: String message = messages.getMessage("event.parameter", mapping
112: .getPath(), mapping.getParameter());
113:
114: LOG.error(message + " " + mapping.getParameter());
115:
116: throw new ServletException(message);
117: }
118:
119: /**
120: * Returns the method name, given a parameter's value.
121: *
122: * @param mapping The ActionMapping used to select this instance
123: * @param form The optional ActionForm bean for this request (if
124: * any)
125: * @param request The HTTP request we are processing
126: * @param response The HTTP response we are creating
127: * @param parameter The <code>ActionMapping</code> parameter's name
128: * @return The method's name.
129: * @throws Exception if an error occurs.
130: */
131: protected String getMethodName(ActionMapping mapping,
132: ActionForm form, HttpServletRequest request,
133: HttpServletResponse response, String parameter)
134: throws Exception {
135:
136: StringTokenizer st = new StringTokenizer(parameter, ",");
137: String defaultMethodName = null;
138:
139: while (st.hasMoreTokens()) {
140: String methodKey = st.nextToken().trim();
141: String methodName = methodKey;
142:
143: // The key can either be a direct method name or an alias
144: // to a method as indicated by a "key=value" signature
145: int equals = methodKey.indexOf('=');
146: if (equals > -1) {
147: methodName = methodKey.substring(equals + 1).trim();
148: methodKey = methodKey.substring(0, equals).trim();
149: }
150:
151: // Set the default if it passes by
152: if (methodKey.equals(DEFAULT_METHOD_KEY)) {
153: defaultMethodName = methodName;
154: }
155:
156: // If the method key exists as a standalone parameter or with
157: // the image suffixes (.x/.y), the method name has been found.
158: if ((request.getParameter(methodKey) != null)
159: || (request.getParameter(methodKey + ".x") != null)) {
160: return methodName;
161: }
162: }
163:
164: return defaultMethodName;
165: }
166: }
|