View Javadoc

1   /*
2    * $Id: ActionMappingParametersInteceptor.java 676182 2008-07-12 14:43:27Z mrdon $
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.interceptor;
23  
24  import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
25  import com.opensymphony.xwork2.ActionContext;
26  
27  import java.util.Map;
28  import java.util.Collections;
29  import java.util.TreeMap;
30  
31  import org.apache.struts2.ServletActionContext;
32  import org.apache.struts2.dispatcher.mapper.ActionMapping;
33  
34  /***
35   * <!-- START SNIPPET: description -->
36   * This interceptor sets all parameters from the action mapping, for this request, on the value stack.  It operates
37   * exactly like {@link ParametersInterceptor}, only the parameters come from the {@link ActionMapping}, not the
38   * {@link ActionContext#getParameters()} method.
39   * <!-- END SNIPPET: description -->
40   * <p/>
41   * <p/> <u>Interceptor parameters:</u>
42   * <p/>
43   * <!-- START SNIPPET: parameters -->
44   * <p/>
45   * <ul>
46   * <p/>
47   * <li>ordered - set to true if you want the top-down property setter behaviour</li>
48   * <p/>
49   * </ul>
50   * <p/>
51   * <!-- END SNIPPET: parameters -->
52   * <p/>
53   * <p/> <u>Extending the interceptor:</u>
54   * <p/>
55   * <!-- START SNIPPET: extending -->
56   * <p/>
57   * <p/> The best way to add behavior to this interceptor is to utilize the {@link com.opensymphony.xwork2.interceptor.ParameterNameAware} interface in your
58   * actions. However, if you wish to apply a global rule that isn't implemented in your action, then you could extend
59   * this interceptor and override the {@link #acceptableName(String)} method.
60   * <p/>
61   * <!-- END SNIPPET: extending -->
62   * <p/>
63   * <p/> <u>Example code:</u>
64   * <p/>
65   * <pre>
66   * <!-- START SNIPPET: example -->
67   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
68   *     &lt;interceptor-ref name="mappingParams"/&gt;
69   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
70   * &lt;/action&gt;
71   * <!-- END SNIPPET: example -->
72   * </pre>
73   */
74  public class ActionMappingParametersInteceptor extends ParametersInterceptor {
75  
76      /***
77       * @param ac The action context
78       * @return the parameters from the action mapping in the context.  If none found, returns
79       *         an empty map.
80       */
81      @Override
82      protected Map retrieveParameters(ActionContext ac) {
83          ActionMapping mapping = (ActionMapping) ac.get(ServletActionContext.ACTION_MAPPING);
84          if (mapping != null) {
85              return mapping.getParams();
86          } else {
87              return Collections.EMPTY_MAP;
88          }
89      }
90  
91      /***
92       * Adds the parameters into context's ParameterMap
93       *
94       * @param ac        The action context
95       * @param newParams The parameter map to apply
96       *                  <p/>
97       *                  In this class this is a no-op, since the parameters were fetched from the same location.
98       *                  In subclasses both retrieveParameters() and addParametersToContext() should be overridden.
99       */
100     @Override
101     protected void addParametersToContext(ActionContext ac, Map newParams) {
102         Map previousParams = ac.getParameters();
103         Map combinedParams = null;
104         if (previousParams != null) {
105             combinedParams = new TreeMap(previousParams);
106         } else {
107             combinedParams = new TreeMap();
108         }
109         combinedParams.putAll(newParams);
110 
111         ac.setParameters(combinedParams);
112     }
113 }