View Javadoc

1   /*
2    * $Id: ServletActionRedirectResult.java 720222 2008-11-24 16:41:07Z musachy $
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.dispatcher;
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 org.apache.struts2.dispatcher.mapper.ActionMapper;
31  import org.apache.struts2.dispatcher.mapper.ActionMapping;
32  import org.apache.struts2.views.util.UrlHelper;
33  
34  import com.opensymphony.xwork2.ActionInvocation;
35  import com.opensymphony.xwork2.config.entities.ResultConfig;
36  import com.opensymphony.xwork2.util.logging.Logger;
37  import com.opensymphony.xwork2.util.logging.LoggerFactory;
38  import com.opensymphony.xwork2.util.reflection.ReflectionException;
39  import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler;
40  
41  /***
42   * <!-- START SNIPPET: description -->
43   *
44   * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a
45   * URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult}
46   * because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your struts.xml
47   * configuration files. This means you can change your URL patterns at any point and your application will still work.
48   * It is strongly recommended that if you are redirecting to another action, you use this result rather than the
49   * standard redirect result.
50   *
51   * See examples below for an example of how request parameters could be passed in.
52   *
53   * <!-- END SNIPPET: description -->
54   *
55   * <b>This result type takes the following parameters:</b>
56   *
57   * <!-- START SNIPPET: params -->
58   *
59   * <ul>
60   *
61   * <li><b>actionName (default)</b> - the name of the action that will be redirect to</li>
62   *
63   * <li><b>namespace</b> - used to determine which namespace the action is in that we're redirecting to . If namespace is
64   * null, this defaults to the current namespace</li>
65   *
66   * <li><b>supressEmptyParameters</b> - optional boolean (defaults to false) that can prevent parameters with no values
67   * from being included in the redirect URL.</li>
68   *
69   * </ul>
70   *
71   * <!-- END SNIPPET: params -->
72   *
73   * <b>Example:</b>
74   *
75   * <pre><!-- START SNIPPET: example -->
76   * &lt;package name="public" extends="struts-default"&gt;
77   *     &lt;action name="login" class="..."&gt;
78   *         &lt;!-- Redirect to another namespace --&gt;
79   *         &lt;result type="redirectAction"&gt;
80   *             &lt;param name="actionName"&gt;dashboard&lt;/param&gt;
81   *             &lt;param name="namespace"&gt;/secure&lt;/param&gt;
82   *         &lt;/result&gt;
83   *     &lt;/action&gt;
84   * &lt;/package&gt;
85   *
86   * &lt;package name="secure" extends="struts-default" namespace="/secure"&gt;
87   *     &lt;-- Redirect to an action in the same namespace --&gt;
88   *     &lt;action name="dashboard" class="..."&gt;
89   *         &lt;result&gt;dashboard.jsp&lt;/result&gt;
90   *         &lt;result name="error" type="redirectAction"&gt;error&lt;/result&gt;
91   *     &lt;/action&gt;
92   *
93   *     &lt;action name="error" class="..."&gt;
94   *         &lt;result&gt;error.jsp&lt;/result&gt;
95   *     &lt;/action&gt;
96   * &lt;/package&gt;
97   *
98   * &lt;package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters"&gt;
99   *    &lt;-- Pass parameters (reportType, width and height) --&gt;
100  *    &lt;!--
101  *    The redirectAction url generated will be :
102  *    /genReport/generateReport.action?reportType=pie&width=100&height=100
103  *    --&gt;
104  *    &lt;action name="gatherReportInfo" class="..."&gt;
105  *       &lt;result name="showReportResult" type="redirectAction"&gt;
106  *          &lt;param name="actionName"&gt;generateReport&lt;/param&gt;
107  *          &lt;param name="namespace"&gt;/genReport&lt;/param&gt;
108  *          &lt;param name="reportType"&gt;pie&lt;/param&gt;
109  *          &lt;param name="width"&gt;100&lt;/param&gt;
110  *          &lt;param name="height"&gt;100&lt;/param&gt;
111  *          &lt;param name="empty"&gt;&lt;/param&gt;
112  *          &lt;param name="supressEmptyParameters"&gt;true&lt;/param&gt;
113  *       &lt;/result&gt;
114  *    &lt;/action&gt;
115  * &lt;/package&gt;
116  *
117  *
118  * <!-- END SNIPPET: example --></pre>
119  *
120  * @see ActionMapper
121  */
122 public class ServletActionRedirectResult extends ServletRedirectResult implements ReflectionExceptionHandler {
123 
124     private static final long serialVersionUID = -9042425229314584066L;
125 
126     /*** The default parameter */
127     public static final String DEFAULT_PARAM = "actionName";
128 
129     private static final Logger LOG = LoggerFactory.getLogger(ServletActionRedirectResult.class);
130 
131     protected String actionName;
132     protected String namespace;
133     protected String method;
134     protected boolean supressEmptyParameters = false;
135 
136     private Map<String, String> requestParameters = new LinkedHashMap<String, String>();
137 
138     public ServletActionRedirectResult() {
139         super();
140     }
141 
142     public ServletActionRedirectResult(String actionName) {
143         this(null, actionName, null);
144     }
145 
146     public ServletActionRedirectResult(String actionName, String method) {
147         this(null, actionName, method);
148     }
149 
150     public ServletActionRedirectResult(String namespace, String actionName, String method) {
151         super(null);
152         this.namespace = namespace;
153         this.actionName = actionName;
154         this.method = method;
155     }
156 
157     protected List<String> prohibitedResultParam = Arrays.asList(new String[] {
158             DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location",
159             "prependServletContext", "supressEmptyParameters" });
160 
161     /***
162      * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)
163      */
164     public void execute(ActionInvocation invocation) throws Exception {
165         actionName = conditionalParse(actionName, invocation);
166         if (namespace == null) {
167             namespace = invocation.getProxy().getNamespace();
168         } else {
169             namespace = conditionalParse(namespace, invocation);
170         }
171         if (method == null) {
172             method = "";
173         }
174         else {
175             method = conditionalParse(method, invocation);
176         }
177 
178         String resultCode = invocation.getResultCode();
179         if (resultCode != null) {
180             ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(
181                     resultCode);
182             Map resultConfigParams = resultConfig.getParams();
183             for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) {
184                 Map.Entry e = (Map.Entry) i.next();
185                 if (! prohibitedResultParam.contains(e.getKey())) {
186                     requestParameters.put(e.getKey().toString(),
187                             e.getValue() == null ? "":
188                                 conditionalParse(e.getValue().toString(), invocation));
189                     String potentialValue = e.getValue() == null ? "": conditionalParse(e.getValue().toString(), invocation);
190                     if (!supressEmptyParameters || ((potentialValue != null) && (potentialValue.length() > 0))) {
191                       requestParameters.put(e.getKey().toString(), potentialValue);
192                     }
193                 }
194             }
195         }
196 
197         StringBuilder tmpLocation = new StringBuilder(actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)));
198         UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");
199 
200         setLocation(tmpLocation.toString());
201 
202         super.execute(invocation);
203     }
204 
205     /***
206      * Sets the action name
207      *
208      * @param actionName The name
209      */
210     public void setActionName(String actionName) {
211         this.actionName = actionName;
212     }
213 
214     /***
215      * Sets the namespace
216      *
217      * @param namespace The namespace
218      */
219     public void setNamespace(String namespace) {
220         this.namespace = namespace;
221     }
222 
223     /***
224      * Sets the method
225      *
226      * @param method The method
227      */
228     public void setMethod(String method) {
229         this.method = method;
230     }
231 
232     /***
233      * Sets the supressEmptyParameters option
234      *
235      * @param suppress The new value for this option
236      */
237     public void setSupressEmptyParameters(boolean supressEmptyParameters) {
238         this.supressEmptyParameters = supressEmptyParameters;
239     }
240 
241     /***
242      * Adds a request parameter to be added to the redirect url
243      *
244      * @param key The parameter name
245      * @param value The parameter value
246      */
247     public ServletActionRedirectResult addParameter(String key, Object value) {
248         requestParameters.put(key, String.valueOf(value));
249         return this;
250     }
251 
252     public void handle(ReflectionException ex) {
253         // Only log as debug as they are probably parameters to be appended to the url
254         LOG.debug(ex.getMessage(), ex);
255     }
256 
257 }