1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <!-- START SNIPPET: example -->
84 * <package name="public" extends="struts-default">
85 * <action name="login" class="...">
86 * <!-- Redirect to another namespace -->
87 * <result type="redirectAction">
88 * <param name="actionName">dashboard</param>
89 * <param name="namespace">/secure</param>
90 * </result>
91 * </action>
92 * </package>
93 *
94 * <package name="secure" extends="struts-default" namespace="/secure">
95 * <-- Redirect to an action in the same namespace -->
96 * <action name="dashboard" class="...">
97 * <result>dashboard.jsp</result>
98 * <result name="error" type="redirectAction">error</result>
99 * </action>
100 *
101 * <action name="error" class="...">
102 * <result>error.jsp</result>
103 * </action>
104 * </package>
105 *
106 * <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
107 * <-- Pass parameters (reportType, width and height) -->
108 * <!--
109 * The redirectAction url generated will be :
110 * /genReport/generateReport.action?reportType=pie&width=100&height=100
111 * -->
112 * <action name="gatherReportInfo" class="...">
113 * <result name="showReportResult" type="redirectAction">
114 * <param name="actionName">generateReport</param>
115 * <param name="namespace">/genReport</param>
116 * <param name="reportType">pie</param>
117 * <param name="width">100</param>
118 * <param name="height">100</param>
119 * </result>
120 * </action>
121 * </package>
122 *
123 *
124 * <!-- END SNIPPET: example -->
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 }