Author: nilsga Date: Sun Aug 12 00:43:13 2007 New Revision: 565009 URL: http://svn.apache.org/viewvc?view=rev&rev=565009 Log: WW-2105 Added new PortletActionRedirectResult class to handle the same syntax as for the ServletActionRedirectResult
Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java Modified: struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java?view=auto&rev=565009 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java Sun Aug 12 00:43:13 2007 @@ -0,0 +1,238 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.portlet.result; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.struts2.dispatcher.ServletActionRedirectResult; +import org.apache.struts2.dispatcher.mapper.ActionMapper; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.views.util.UrlHelper; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.inject.Inject; + +/** + * + * Portlet modification of the [EMAIL PROTECTED] ServletActionRedirectResult}. + * + * <!-- START SNIPPET: description --> + + * This result uses the [EMAIL PROTECTED] ActionMapper} provided by the [EMAIL PROTECTED] ActionMapperFactory} to instruct the render phase to + * invoke the specified action and (optional) namespace. This is better than the [EMAIL PROTECTED] PortletResult} + * because it does not require you to encode the URL patterns processed by the [EMAIL PROTECTED] ActionMapper} in to your struts.xml + * configuration files. This means you can change your URL patterns at any point and your application will still work. + * It is strongly recommended that if you are redirecting to another action, you use this result rather than the + * standard redirect result. + * + * See examples below for an example of how request parameters could be passed in. + * + * <!-- END SNIPPET: description --> + * + * <b>This result type takes the following parameters:</b> + * + * <!-- START SNIPPET: params --> + * + * <ul> + * + * <li><b>actionName (default)</b> - the name of the action that will be redirect to</li> + * + * <li><b>namespace</b> - used to determine which namespace the action is in that we're redirecting to . If namespace is + * null, this defaults to the current namespace</li> + * + * </ul> + * + * <!-- END SNIPPET: params --> + * + * <b>Example:</b> + * + * <pre><!-- START SNIPPET: example --> + * <package name="public" extends="struts-default"> + * <action name="login" class="..."> + * <!-- Redirect to another namespace --> + * <result type="redirect-action"> + * <param name="actionName">dashboard</param> + * <param name="namespace">/secure</param> + * </result> + * </action> + * </package> + * + * <package name="secure" extends="struts-default" namespace="/secure"> + * <-- Redirect to an action in the same namespace --> + * <action name="dashboard" class="..."> + * <result>dashboard.jsp</result> + * <result name="error" type="redirect-action">error</result> + * </action> + * + * <action name="error" class="..."> + * <result>error.jsp</result> + * </action> + * </package> + * + * <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters"> + * <-- Pass parameters (reportType, width and height) --> + * <!-- + * The redirect-action url generated will be : + * /genReport/generateReport.action?reportType=pie&width=100&height=100 + * --> + * <action name="gatherReportInfo" class="..."> + * <result name="showReportResult" type="redirect-action"> + * <param name="actionName">generateReport</param> + * <param name="namespace">/genReport</param> + * <param name="reportType">pie</param> + * <param name="width">100</param> + * <param name="height">100</param> + * </result> + * </action> + * </package> + * + * + * <!-- END SNIPPET: example --></pre> + * + * @see ActionMapper + */ +public class PortletActionRedirectResult extends PortletResult { + + private static final long serialVersionUID = -7627388936683562557L; + + /** The default parameter */ + public static final String DEFAULT_PARAM = "actionName"; + + protected String actionName; + protected String namespace; + protected String method; + + private Map<String, String> requestParameters = new LinkedHashMap<String, String>(); + + private ActionMapper actionMapper; + + public PortletActionRedirectResult() { + super(); + } + + public PortletActionRedirectResult(String actionName) { + this(null, actionName, null); + } + + public PortletActionRedirectResult(String actionName, String method) { + this(null, actionName, method); + } + + public PortletActionRedirectResult(String namespace, String actionName, String method) { + super(null); + this.namespace = namespace; + this.actionName = actionName; + this.method = method; + } + + protected List<String> prohibitedResultParam = Arrays.asList(new String[] { + DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", + "prependServletContext" }); + + @Inject + public void setActionMapper(ActionMapper actionMapper) { + this.actionMapper = actionMapper; + } + + /** + * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation) + */ + public void execute(ActionInvocation invocation) throws Exception { + actionName = conditionalParse(actionName, invocation); + if (namespace == null) { + namespace = invocation.getProxy().getNamespace(); + } else { + namespace = conditionalParse(namespace, invocation); + } + if (method == null) { + method = ""; + } + else { + method = conditionalParse(method, invocation); + } + + String resultCode = invocation.getResultCode(); + if (resultCode != null) { + ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get( + resultCode); + Map resultConfigParams = resultConfig.getParams(); + for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) { + Map.Entry e = (Map.Entry) i.next(); + if (! prohibitedResultParam.contains(e.getKey())) { + requestParameters.put(e.getKey().toString(), + e.getValue() == null ? "": + conditionalParse(e.getValue().toString(), invocation)); + } + } + } + + StringBuffer tmpLocation = new StringBuffer(actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null))); + UrlHelper.buildParametersString(requestParameters, tmpLocation, "&"); + + setLocation(tmpLocation.toString()); + + super.execute(invocation); + } + + /** + * Sets the action name + * + * @param actionName The name + */ + public void setActionName(String actionName) { + this.actionName = actionName; + } + + /** + * Sets the namespace + * + * @param namespace The namespace + */ + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + /** + * Sets the method + * + * @param method The method + */ + public void setMethod(String method) { + this.method = method; + } + + /** + * Adds a request parameter to be added to the redirect url + * + * @param key The parameter name + * @param value The parameter value + */ + public PortletActionRedirectResult addParameter(String key, Object value) { + requestParameters.put(key, String.valueOf(value)); + return this; + } + +} Modified: struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml?view=diff&rev=565009&r1=565008&r2=565009 ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml (original) +++ struts/struts2/trunk/plugins/portlet/src/main/resources/struts-plugin.xml Sun Aug 12 00:43:13 2007 @@ -36,7 +36,8 @@ <result-types> <result-type name="dispatcher" class="org.apache.struts2.portlet.result.PortletResult" default="true"/> - <result-type name="redirectAction" class="org.apache.struts2.portlet.result.PortletResult"/> + <result-type name="redirect" class="org.apache.struts2.portlet.result.PortletResult"/> + <result-type name="redirectAction" class="org.apache.struts2.portlet.result.PortletActionRedirectResult"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.PortletFreemarkerResult"/> <result-type name="velocity" class="org.apache.struts2.portlet.result.PortletVelocityResult"/> </result-types>