View Javadoc

1   package com.atlassian.xwork.interceptors;
2   
3   import com.opensymphony.xwork.interceptor.AroundInterceptor;
4   import com.opensymphony.xwork.ActionInvocation;
5   import com.opensymphony.xwork.ActionContext;
6   
7   import java.util.*;
8   
9   /**
10   * This interceptor looks for a map in the ActionContext context, and if it finds it BUT
11   * those parameters are NOT in the existing Http Request, then it calls them as setters on the action.
12   */
13  public class RequestParameterHackInterceptor extends AroundInterceptor
14  {
15      private static ThreadLocal hack = new ThreadLocal();
16  
17      public static void setHackMap(Map hackMap)
18      {
19          com.atlassian.xwork.interceptors.RequestParameterHackInterceptor.hack.set(hackMap);
20      }
21  
22      protected void before(ActionInvocation invocation) throws Exception
23      {
24          Map hackMap = (Map) com.atlassian.xwork.interceptors.RequestParameterHackInterceptor.hack.get();
25  
26          if (hackMap != null)
27          {
28              Map acParameters = ActionContext.getContext().getParameters();
29  
30              List parametersToSetOnAction = new ArrayList(hackMap.size());
31              for (Iterator iterator = hackMap.keySet().iterator(); iterator.hasNext();)
32              {
33                  String key = (String) iterator.next();
34                  if (!acParameters.containsKey(key))
35                  {
36                      parametersToSetOnAction.add(key);
37                  }
38              }
39  
40              // if we have hack parameters, let's replace the webwork parameters map completely
41              if (parametersToSetOnAction.size() > 0)
42              {
43                  Map parameters = new HashMap(acParameters);
44                  for (Iterator iterator = parametersToSetOnAction.iterator(); iterator.hasNext();)
45                  {
46                      String key = (String) iterator.next();
47                      parameters.put(key, hackMap.get(key));
48                  }
49  
50                  ActionContext.getContext().setParameters(parameters);
51              }
52  
53              com.atlassian.xwork.interceptors.RequestParameterHackInterceptor.setHackMap(null);
54          }
55      }
56  
57      protected void after(ActionInvocation actionInvocation, String s) throws Exception
58      {
59      }
60  }