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