View Javadoc

1   /*
2    * $Id: PortletActionRedirectResult.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.portlet.result;
23  
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.portlet.PortletMode;
31  
32  import org.apache.struts2.dispatcher.ServletActionRedirectResult;
33  import org.apache.struts2.dispatcher.mapper.ActionMapper;
34  import org.apache.struts2.dispatcher.mapper.ActionMapping;
35  import org.apache.struts2.portlet.PortletActionConstants;
36  import org.apache.struts2.views.util.UrlHelper;
37  
38  import com.opensymphony.xwork2.ActionInvocation;
39  import com.opensymphony.xwork2.config.entities.ResultConfig;
40  import com.opensymphony.xwork2.inject.Inject;
41  
42  /***
43   * 
44   * Portlet modification of the {@link ServletActionRedirectResult}.
45   * 
46   * <!-- START SNIPPET: description -->
47   * 
48   * This result uses the {@link ActionMapper} provided by the
49   * {@link ActionMapperFactory} to instruct the render phase to invoke the
50   * specified action and (optional) namespace. This is better than the
51   * {@link PortletResult} because it does not require you to encode the URL
52   * patterns processed by the {@link ActionMapper} in to your struts.xml
53   * configuration files. This means you can change your URL patterns at any point
54   * and your application will still work. It is strongly recommended that if you
55   * are redirecting to another action, you use this result rather than the
56   * standard redirect result.
57   * 
58   * See examples below for an example of how request parameters could be passed
59   * in.
60   * 
61   * <!-- END SNIPPET: description -->
62   * 
63   * <b>This result type takes the following parameters:</b>
64   * 
65   * <!-- START SNIPPET: params -->
66   * 
67   * <ul>
68   * 
69   * <li><b>actionName (default)</b> - the name of the action that will be
70   * redirect to</li>
71   * 
72   * <li><b>namespace</b> - used to determine which namespace the action is in
73   * that we're redirecting to . If namespace is null, this defaults to the
74   * current namespace</li>
75   * 
76   * </ul>
77   * 
78   * <!-- END SNIPPET: params -->
79   * 
80   * <b>Example:</b>
81   * 
82   * <pre>
83   * &lt;!-- START SNIPPET: example --&gt;
84   *  &lt;package name=&quot;public&quot; extends=&quot;struts-default&quot;&gt;
85   *      &lt;action name=&quot;login&quot; class=&quot;...&quot;&gt;
86   *          &lt;!-- Redirect to another namespace --&gt;
87   *          &lt;result type=&quot;redirectAction&quot;&gt;
88   *              &lt;param name=&quot;actionName&quot;&gt;dashboard&lt;/param&gt;
89   *              &lt;param name=&quot;namespace&quot;&gt;/secure&lt;/param&gt;
90   *          &lt;/result&gt;
91   *      &lt;/action&gt;
92   *  &lt;/package&gt;
93   * 
94   *  &lt;package name=&quot;secure&quot; extends=&quot;struts-default&quot; namespace=&quot;/secure&quot;&gt;
95   *      &lt;-- Redirect to an action in the same namespace --&gt;
96   *      &lt;action name=&quot;dashboard&quot; class=&quot;...&quot;&gt;
97   *          &lt;result&gt;dashboard.jsp&lt;/result&gt;
98   *          &lt;result name=&quot;error&quot; type=&quot;redirectAction&quot;&gt;error&lt;/result&gt;
99   *      &lt;/action&gt;
100  * 
101  *      &lt;action name=&quot;error&quot; class=&quot;...&quot;&gt;
102  *          &lt;result&gt;error.jsp&lt;/result&gt;
103  *      &lt;/action&gt;
104  *  &lt;/package&gt;
105  * 
106  *  &lt;package name=&quot;passingRequestParameters&quot; extends=&quot;struts-default&quot; namespace=&quot;/passingRequestParameters&quot;&gt;
107  *     &lt;-- Pass parameters (reportType, width and height) --&gt;
108  *     &lt;!--
109  *     The redirectAction url generated will be :
110  *     /genReport/generateReport.action?reportType=pie&amp;width=100&amp;height=100
111  *     --&gt;
112  *     &lt;action name=&quot;gatherReportInfo&quot; class=&quot;...&quot;&gt;
113  *        &lt;result name=&quot;showReportResult&quot; type=&quot;redirectAction&quot;&gt;
114  *           &lt;param name=&quot;actionName&quot;&gt;generateReport&lt;/param&gt;
115  *           &lt;param name=&quot;namespace&quot;&gt;/genReport&lt;/param&gt;
116  *           &lt;param name=&quot;reportType&quot;&gt;pie&lt;/param&gt;
117  *           &lt;param name=&quot;width&quot;&gt;100&lt;/param&gt;
118  *           &lt;param name=&quot;height&quot;&gt;100&lt;/param&gt;
119  *        &lt;/result&gt;
120  *     &lt;/action&gt;
121  *  &lt;/package&gt;
122  * 
123  * 
124  *  &lt;!-- END SNIPPET: example --&gt;
125  * </pre>
126  * 
127  * @see ActionMapper
128  */
129 public class PortletActionRedirectResult extends PortletResult {
130 
131 	private static final long serialVersionUID = -7627388936683562557L;
132 
133 	/*** The default parameter */
134 	public static final String DEFAULT_PARAM = "actionName";
135 
136 	protected String actionName;
137 
138 	protected String namespace;
139 
140 	protected String method;
141 
142 	private Map<String, String> requestParameters = new LinkedHashMap<String, String>();
143 
144 	private ActionMapper actionMapper;
145 
146 	public PortletActionRedirectResult() {
147 		super();
148 	}
149 
150 	public PortletActionRedirectResult(String actionName) {
151 		this(null, actionName, null);
152 	}
153 
154 	public PortletActionRedirectResult(String actionName, String method) {
155 		this(null, actionName, method);
156 	}
157 
158 	public PortletActionRedirectResult(String namespace, String actionName, String method) {
159 		super(null);
160 		this.namespace = namespace;
161 		this.actionName = actionName;
162 		this.method = method;
163 	}
164 
165 	protected List<String> prohibitedResultParam = Arrays.asList(new String[] { DEFAULT_PARAM, "namespace", "method",
166 			"encode", "parse", "location", "prependServletContext" });
167 
168 	@Inject
169 	public void setActionMapper(ActionMapper actionMapper) {
170 		this.actionMapper = actionMapper;
171 	}
172 
173 	/***
174 	 * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
175 	 */
176 	public void execute(ActionInvocation invocation) throws Exception {
177 		actionName = conditionalParse(actionName, invocation);
178 		if (portletMode != null) {
179 			Map<PortletMode, String> namespaceMap = (Map<PortletMode, String>) invocation.getInvocationContext().get(
180 					PortletActionConstants.MODE_NAMESPACE_MAP);
181 			namespace = namespaceMap.get(portletMode);
182 		}
183 		if (namespace == null) {
184 			namespace = invocation.getProxy().getNamespace();
185 		} else {
186 			namespace = conditionalParse(namespace, invocation);
187 		}
188 		if (method == null) {
189 			method = "";
190 		} else {
191 			method = conditionalParse(method, invocation);
192 		}
193 
194 		String resultCode = invocation.getResultCode();
195 		if (resultCode != null) {
196 			ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(resultCode);
197 			Map resultConfigParams = resultConfig.getParams();
198 			for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext();) {
199 				Map.Entry e = (Map.Entry) i.next();
200 				if (!prohibitedResultParam.contains(e.getKey())) {
201 					requestParameters.put(e.getKey().toString(), e.getValue() == null ? "" : conditionalParse(e
202 							.getValue().toString(), invocation));
203 				}
204 			}
205 		}
206 
207 		StringBuffer tmpLocation = new StringBuffer(actionMapper.getUriFromActionMapping(new ActionMapping(actionName,
208 				namespace, method, null)));
209 		UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");
210 
211 		setLocation(tmpLocation.toString());
212 
213 		super.execute(invocation);
214 	}
215 
216 	/***
217 	 * Sets the action name
218 	 * 
219 	 * @param actionName
220 	 *            The name
221 	 */
222 	public void setActionName(String actionName) {
223 		this.actionName = actionName;
224 	}
225 
226 	/***
227 	 * Sets the namespace
228 	 * 
229 	 * @param namespace
230 	 *            The namespace
231 	 */
232 	public void setNamespace(String namespace) {
233 		this.namespace = namespace;
234 	}
235 
236 	/***
237 	 * Sets the method
238 	 * 
239 	 * @param method
240 	 *            The method
241 	 */
242 	public void setMethod(String method) {
243 		this.method = method;
244 	}
245 
246 	/***
247 	 * Adds a request parameter to be added to the redirect url
248 	 * 
249 	 * @param key
250 	 *            The parameter name
251 	 * @param value
252 	 *            The parameter value
253 	 */
254 	public PortletActionRedirectResult addParameter(String key, Object value) {
255 		requestParameters.put(key, String.valueOf(value));
256 		return this;
257 	}
258 
259 }