View Javadoc

1   package com.atlassian.core.filters.legacy;
2   
3   import com.atlassian.core.util.StringUtils;
4   
5   import javax.servlet.http.HttpServletRequestWrapper;
6   import javax.servlet.http.HttpServletRequest;
7   import java.util.Map;
8   import java.util.HashMap;
9   
10  /**
11   * Uses {@link StringUtils#escapeCP1252(String, String)} to replace high-bit punctuation characters
12   * with ASCII equivalents in request parameter values.
13   * <p/>
14   * This is here for legacy functionality if required, but its use is <b>not</b> recommended. Rather,
15   * applications should be configured with Unicode request and response encoding.
16   *
17   * @since 4.0
18   */
19  public final class WordCurlyQuotesRequestWrapper extends HttpServletRequestWrapper
20  {
21      private final String encoding;
22  
23      /**
24       * Caches the escaped parameter values for a particular parameter, once {@link #getParameterValues(String)}
25       * is called for that parameter. 
26       */
27      private final Map<String, String[]> parameterValueCache = new HashMap<String, String[]>();
28  
29      /**
30       * Caches the escaped parameter map once {@link #getParameterMap()} is called.
31       */
32      private Map<String, String[]> parameterMap = null;
33  
34      public WordCurlyQuotesRequestWrapper(HttpServletRequest servletRequest, String encoding)
35      {
36          super(servletRequest);
37          this.encoding = encoding;
38      }
39  
40      public final String getParameter(String string)
41      {
42          return escapeString(super.getParameter(string));
43      }
44  
45      private String escapeString(String string)
46      {
47          return StringUtils.escapeCP1252(string, encoding);
48      }
49  
50  
51      public final Map getParameterMap()
52      {
53          if (parameterMap == null)
54          {
55              //noinspection unchecked
56              Map<String, Object> original = (Map<String, Object>) super.getParameterMap();
57  
58              parameterMap = new HashMap<String, String[]>();
59              for (String key : original.keySet())
60              {
61                  parameterMap.put(key, getParameterValues(key));
62              }
63          }
64          return parameterMap;
65      }
66  
67  
68      public final String[] getParameterValues(String string)
69      {
70          String[] returnValue = parameterValueCache.get(string);
71  
72          //if we haven't yet cached this - look it up.
73          if (returnValue == null)
74          {
75              String[] parameterValues = super.getParameterValues(string);
76  
77              //values could be null - don't bother converting them
78              if (parameterValues == null)
79                  return null;
80  
81              for (int i = 0; i < parameterValues.length; i++)
82              {
83                  String parameterValue = escapeString(parameterValues[i]);
84                  parameterValues[i] = parameterValue;
85              }
86  
87              parameterValueCache.put(string, parameterValues);
88              returnValue = parameterValues;
89  
90          }
91          return returnValue;
92      }
93  }