001: /*
002: * $Id: ServletActionRedirectResult.java 543174 2007-05-31 16:04:00Z musachy $
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.struts2.dispatcher;
022:
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedHashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.struts2.dispatcher.mapper.ActionMapper;
031: import org.apache.struts2.dispatcher.mapper.ActionMapping;
032: import org.apache.struts2.views.util.UrlHelper;
033:
034: import com.opensymphony.xwork2.ActionInvocation;
035: import com.opensymphony.xwork2.config.entities.ResultConfig;
036: import com.opensymphony.xwork2.inject.Inject;
037:
038: /**
039: * <!-- START SNIPPET: description -->
040: *
041: * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a
042: * URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult}
043: * because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your struts.xml
044: * configuration files. This means you can change your URL patterns at any point and your application will still work.
045: * It is strongly recommended that if you are redirecting to another action, you use this result rather than the
046: * standard redirect result.
047: *
048: * <p/>
049: *
050: * See examples below for an example of how request parameters could be passed in.
051: *
052: * <!-- END SNIPPET: description -->
053: *
054: * <b>This result type takes the following parameters:</b>
055: *
056: * <!-- START SNIPPET: params -->
057: *
058: * <ul>
059: *
060: * <li><b>actionName (default)</b> - the name of the action that will be redirect to</li>
061: *
062: * <li><b>namespace</b> - used to determine which namespace the action is in that we're redirecting to . If namespace is
063: * null, this defaults to the current namespace</li>
064: *
065: * </ul>
066: *
067: * <!-- END SNIPPET: params -->
068: *
069: * <b>Example:</b>
070: *
071: * <pre><!-- START SNIPPET: example -->
072: * <package name="public" extends="struts-default">
073: * <action name="login" class="...">
074: * <!-- Redirect to another namespace -->
075: * <result type="redirect-action">
076: * <param name="actionName">dashboard</param>
077: * <param name="namespace">/secure</param>
078: * </result>
079: * </action>
080: * </package>
081: *
082: * <package name="secure" extends="struts-default" namespace="/secure">
083: * <-- Redirect to an action in the same namespace -->
084: * <action name="dashboard" class="...">
085: * <result>dashboard.jsp</result>
086: * <result name="error" type="redirect-action>error</result>
087: * </action>
088: *
089: * <action name="error" class="...">
090: * <result>error.jsp</result>
091: * </action>
092: * </package>
093: *
094: * <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
095: * <-- Pass parameters (reportType, width and height) -->
096: * <!--
097: * The redirect-action url generated will be :
098: * /genReport/generateReport.action?reportType=pie&width=100&height=100
099: * -->
100: * <action name="gatherReportInfo" class="...">
101: * <result name="showReportResult" type="redirect-action">
102: * <param name="actionName">generateReport</param>
103: * <param name="namespace">/genReport</param>
104: * <param name="reportType">pie</param>
105: * <param name="width">100</param>
106: * <param name="height">100</param>
107: * </result>
108: * </action>
109: * </package>
110: *
111: *
112: * <!-- END SNIPPET: example --></pre>
113: *
114: * @see ActionMapper
115: */
116: public class ServletActionRedirectResult extends ServletRedirectResult {
117:
118: private static final long serialVersionUID = -9042425229314584066L;
119:
120: /** The default parameter */
121: public static final String DEFAULT_PARAM = "actionName";
122:
123: protected String actionName;
124: protected String namespace;
125: protected String method;
126:
127: private Map<String, String> requestParameters = new LinkedHashMap<String, String>();
128:
129: public ServletActionRedirectResult() {
130: super ();
131: }
132:
133: public ServletActionRedirectResult(String actionName) {
134: this (null, actionName, null);
135: }
136:
137: public ServletActionRedirectResult(String actionName, String method) {
138: this (null, actionName, method);
139: }
140:
141: public ServletActionRedirectResult(String namespace,
142: String actionName, String method) {
143: super (null);
144: this .namespace = namespace;
145: this .actionName = actionName;
146: this .method = method;
147: }
148:
149: protected List<String> prohibitedResultParam = Arrays
150: .asList(new String[] { DEFAULT_PARAM, "namespace",
151: "method", "encode", "parse", "location",
152: "prependServletContext" });
153:
154: /**
155: * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
156: */
157: public void execute(ActionInvocation invocation) throws Exception {
158: actionName = conditionalParse(actionName, invocation);
159: if (namespace == null) {
160: namespace = invocation.getProxy().getNamespace();
161: } else {
162: namespace = conditionalParse(namespace, invocation);
163: }
164: if (method == null) {
165: method = "";
166: } else {
167: method = conditionalParse(method, invocation);
168: }
169:
170: String resultCode = invocation.getResultCode();
171: if (resultCode != null) {
172: ResultConfig resultConfig = invocation.getProxy()
173: .getConfig().getResults().get(resultCode);
174: Map resultConfigParams = resultConfig.getParams();
175: for (Iterator i = resultConfigParams.entrySet().iterator(); i
176: .hasNext();) {
177: Map.Entry e = (Map.Entry) i.next();
178: if (!prohibitedResultParam.contains(e.getKey())) {
179: requestParameters.put(e.getKey().toString(), e
180: .getValue() == null ? ""
181: : conditionalParse(e.getValue().toString(),
182: invocation));
183: }
184: }
185: }
186:
187: StringBuffer tmpLocation = new StringBuffer(actionMapper
188: .getUriFromActionMapping(new ActionMapping(actionName,
189: namespace, method, null)));
190: UrlHelper.buildParametersString(requestParameters, tmpLocation,
191: "&");
192:
193: setLocation(tmpLocation.toString());
194:
195: super .execute(invocation);
196: }
197:
198: /**
199: * Sets the action name
200: *
201: * @param actionName The name
202: */
203: public void setActionName(String actionName) {
204: this .actionName = actionName;
205: }
206:
207: /**
208: * Sets the namespace
209: *
210: * @param namespace The namespace
211: */
212: public void setNamespace(String namespace) {
213: this .namespace = namespace;
214: }
215:
216: /**
217: * Sets the method
218: *
219: * @param method The method
220: */
221: public void setMethod(String method) {
222: this .method = method;
223: }
224:
225: /**
226: * Adds a request parameter to be added to the redirect url
227: *
228: * @param key The parameter name
229: * @param value The parameter value
230: */
231: public ServletActionRedirectResult addParameter(String key,
232: Object value) {
233: requestParameters.put(key, String.valueOf(value));
234: return this;
235: }
236:
237: }
|