You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ni...@apache.org on 2007/09/27 21:45:47 UTC

svn commit: r580136 - in /struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result: PortletActionRedirectResult.java PortletResult.java

Author: nilsga
Date: Thu Sep 27 12:45:46 2007
New Revision: 580136

URL: http://svn.apache.org/viewvc?rev=580136&view=rev
Log:
WW-2211 Added support for switching portlet mode in the redirect action result type

Modified:
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletResult.java

Modified: 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?rev=580136&r1=580135&r2=580136&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletActionRedirectResult.java Thu Sep 27 12:45:46 2007
@@ -26,9 +26,12 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.portlet.PortletMode;
+
 import org.apache.struts2.dispatcher.ServletActionRedirectResult;
 import org.apache.struts2.dispatcher.mapper.ActionMapper;
 import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.apache.struts2.portlet.PortletActionConstants;
 import org.apache.struts2.views.util.UrlHelper;
 
 import com.opensymphony.xwork2.ActionInvocation;
@@ -40,78 +43,86 @@
  * Portlet modification of the {@link ServletActionRedirectResult}.
  * 
  * <!-- START SNIPPET: description -->
-
- * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to instruct the render phase to
- * invoke the specified action and (optional) namespace. This is better than the {@link PortletResult}
- * because it does not require you to encode the URL patterns processed by the {@link 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
+ * 
+ * This result uses the {@link ActionMapper} provided by the
+ * {@link ActionMapperFactory} to instruct the render phase to invoke the
+ * specified action and (optional) namespace. This is better than the
+ * {@link PortletResult} because it does not require you to encode the URL
+ * patterns processed by the {@link 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.
- *
+ * 
+ * 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>
- *
+ * 
+ * <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 -->
- * &lt;package name="public" extends="struts-default"&gt;
- *     &lt;action name="login" class="..."&gt;
- *         &lt;!-- Redirect to another namespace --&gt;
- *         &lt;result type="redirect-action"&gt;
- *             &lt;param name="actionName"&gt;dashboard&lt;/param&gt;
- *             &lt;param name="namespace"&gt;/secure&lt;/param&gt;
- *         &lt;/result&gt;
- *     &lt;/action&gt;
- * &lt;/package&gt;
- *
- * &lt;package name="secure" extends="struts-default" namespace="/secure"&gt;
- *     &lt;-- Redirect to an action in the same namespace --&gt;
- *     &lt;action name="dashboard" class="..."&gt;
- *         &lt;result&gt;dashboard.jsp&lt;/result&gt;
- *         &lt;result name="error" type="redirect-action"&gt;error&lt;/result&gt;
- *     &lt;/action&gt;
- *
- *     &lt;action name="error" class="..."&gt;
- *         &lt;result&gt;error.jsp&lt;/result&gt;
+ * 
+ * <pre>
+ * &lt;!-- START SNIPPET: example --&gt;
+ *  &lt;package name=&quot;public&quot; extends=&quot;struts-default&quot;&gt;
+ *      &lt;action name=&quot;login&quot; class=&quot;...&quot;&gt;
+ *          &lt;!-- Redirect to another namespace --&gt;
+ *          &lt;result type=&quot;redirect-action&quot;&gt;
+ *              &lt;param name=&quot;actionName&quot;&gt;dashboard&lt;/param&gt;
+ *              &lt;param name=&quot;namespace&quot;&gt;/secure&lt;/param&gt;
+ *          &lt;/result&gt;
+ *      &lt;/action&gt;
+ *  &lt;/package&gt;
+ * 
+ *  &lt;package name=&quot;secure&quot; extends=&quot;struts-default&quot; namespace=&quot;/secure&quot;&gt;
+ *      &lt;-- Redirect to an action in the same namespace --&gt;
+ *      &lt;action name=&quot;dashboard&quot; class=&quot;...&quot;&gt;
+ *          &lt;result&gt;dashboard.jsp&lt;/result&gt;
+ *          &lt;result name=&quot;error&quot; type=&quot;redirect-action&quot;&gt;error&lt;/result&gt;
+ *      &lt;/action&gt;
+ * 
+ *      &lt;action name=&quot;error&quot; class=&quot;...&quot;&gt;
+ *          &lt;result&gt;error.jsp&lt;/result&gt;
+ *      &lt;/action&gt;
+ *  &lt;/package&gt;
+ * 
+ *  &lt;package name=&quot;passingRequestParameters&quot; extends=&quot;struts-default&quot; namespace=&quot;/passingRequestParameters&quot;&gt;
+ *     &lt;-- Pass parameters (reportType, width and height) --&gt;
+ *     &lt;!--
+ *     The redirect-action url generated will be :
+ *     /genReport/generateReport.action?reportType=pie&amp;width=100&amp;height=100
+ *     --&gt;
+ *     &lt;action name=&quot;gatherReportInfo&quot; class=&quot;...&quot;&gt;
+ *        &lt;result name=&quot;showReportResult&quot; type=&quot;redirect-action&quot;&gt;
+ *           &lt;param name=&quot;actionName&quot;&gt;generateReport&lt;/param&gt;
+ *           &lt;param name=&quot;namespace&quot;&gt;/genReport&lt;/param&gt;
+ *           &lt;param name=&quot;reportType&quot;&gt;pie&lt;/param&gt;
+ *           &lt;param name=&quot;width&quot;&gt;100&lt;/param&gt;
+ *           &lt;param name=&quot;height&quot;&gt;100&lt;/param&gt;
+ *        &lt;/result&gt;
  *     &lt;/action&gt;
- * &lt;/package&gt;
- *
- * &lt;package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters"&gt;
- *    &lt;-- Pass parameters (reportType, width and height) --&gt;
- *    &lt;!--
- *    The redirect-action url generated will be :
- *    /genReport/generateReport.action?reportType=pie&width=100&height=100
- *    --&gt;
- *    &lt;action name="gatherReportInfo" class="..."&gt;
- *       &lt;result name="showReportResult" type="redirect-action"&gt;
- *          &lt;param name="actionName"&gt;generateReport&lt;/param&gt;
- *          &lt;param name="namespace"&gt;/genReport&lt;/param&gt;
- *          &lt;param name="reportType"&gt;pie&lt;/param&gt;
- *          &lt;param name="width"&gt;100&lt;/param&gt;
- *          &lt;param name="height"&gt;100&lt;/param&gt;
- *       &lt;/result&gt;
- *    &lt;/action&gt;
- * &lt;/package&gt;
- *
- *
- * <!-- END SNIPPET: example --></pre>
- *
+ *  &lt;/package&gt;
+ * 
+ * 
+ *  &lt;!-- END SNIPPET: example --&gt;
+ * </pre>
+ * 
  * @see ActionMapper
  */
 public class PortletActionRedirectResult extends PortletResult {
@@ -119,120 +130,129 @@
 	private static final long serialVersionUID = -7627388936683562557L;
 
 	/** The default parameter */
-    public static final String DEFAULT_PARAM = "actionName";
+	public static final String DEFAULT_PARAM = "actionName";
+
+	protected String actionName;
+
+	protected String namespace;
+
+	protected String method;
 
-    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;
-    }
+	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 (portletMode != null) {
+			Map<PortletMode, String> namespaceMap = (Map<PortletMode, String>) invocation.getInvocationContext().get(
+					PortletActionConstants.MODE_NAMESPACE_MAP);
+			namespace = namespaceMap.get(portletMode);
+		}
+		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/java/org/apache/struts2/portlet/result/PortletResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletResult.java?rev=580136&r1=580135&r2=580136&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletResult.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/result/PortletResult.java Thu Sep 27 12:45:46 2007
@@ -27,6 +27,7 @@
 import javax.portlet.ActionResponse;
 import javax.portlet.PortletContext;
 import javax.portlet.PortletException;
+import javax.portlet.PortletMode;
 import javax.portlet.PortletRequestDispatcher;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
@@ -66,6 +67,8 @@
 	private String contentType = "text/html";
 
 	private String title;
+	
+	protected PortletMode portletMode;
 
 	public PortletResult() {
 		super();
@@ -121,7 +124,7 @@
 	 * @param finalLocation
 	 * @param invocation
 	 */
-	protected void executeActionResult(String finalLocation, ActionInvocation invocation) {
+	protected void executeActionResult(String finalLocation, ActionInvocation invocation) throws Exception {
 		LOG.debug("Executing result in Event phase");
 		ActionResponse res = PortletActionContext.getActionResponse();
 		Map sessionMap = invocation.getInvocationContext().getSession();
@@ -139,8 +142,14 @@
 			res.setRenderParameter(ACTION_PARAM, "renderDirect");
 			sessionMap.put(RENDER_DIRECT_LOCATION, finalLocation);
 		}
-		res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode()
-				.toString());
+		if(portletMode != null) {
+			res.setPortletMode(portletMode);
+			res.setRenderParameter(PortletActionConstants.MODE_PARAM, portletMode.toString());
+		}
+		else {
+			res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode()
+					.toString());
+		}
 	}
 
 	/**
@@ -210,6 +219,12 @@
 	 */
 	public void setTitle(String title) {
 		this.title = title;
+	}
+	
+	public void setPortletMode(String portletMode) {
+		if(portletMode != null) {
+			this.portletMode = new PortletMode(portletMode);
+		}
 	}
 
 	@Inject("struts.portlet.useDispatcherServlet")