View Javadoc

1   /**
2    * This file Copyright (c) 2009-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.filters;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.cms.core.SystemProperty;
38  import info.magnolia.cms.util.UnicodeNormalizer;
39  import info.magnolia.context.MgnlContext;
40  
41  import java.io.IOException;
42  import java.io.UnsupportedEncodingException;
43  import java.net.URLDecoder;
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  import javax.servlet.FilterChain;
48  import javax.servlet.ServletException;
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletRequestWrapper;
51  import javax.servlet.http.HttpServletResponse;
52  
53  
54  /**
55   * @author Luca Boati
56   * @version $Id: $
57   */
58  public class UnicodeNormalizationFilter extends AbstractMgnlFilter
59  {
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
66          throws IOException, ServletException
67      {
68          final AggregationState aggregationState = MgnlContext.getAggregationState();
69          String originalBrowserURI = MgnlContext.getContextPath() + aggregationState.getOriginalBrowserURI();
70          String originalBrowserURL = aggregationState.getOriginalBrowserURL();
71          String originalURINormalized = MgnlContext.getContextPath() + UnicodeNormalizer.normalizeNFC(aggregationState.getOriginalURI());
72          String originalURLNormalized = UnicodeNormalizer.normalizeNFC(aggregationState.getOriginalURL());
73          String currentURI = MgnlContext.getContextPath() + aggregationState.getCurrentURI();
74  
75          // reset uri of the aggregationState in order to set new values for decoded uris
76          MgnlContext.resetAggregationState();
77  
78          // restore some values
79          MgnlContext.getAggregationState().setOriginalBrowserURI(originalBrowserURI);
80          MgnlContext.getAggregationState().setOriginalBrowserURL(originalBrowserURL);
81          MgnlContext.getAggregationState().setCurrentURI(currentURI);
82  
83          // set new values for original uri and url
84          MgnlContext.getAggregationState().setOriginalURI(originalURINormalized);
85          MgnlContext.getAggregationState().setOriginalURL(originalURLNormalized);
86  
87          HttpServletRequest unicodeRequest = new UnicodeNormalizerRequestWrapper(request);
88          MgnlContext.push(unicodeRequest, response);
89  
90          if (MgnlContext.getPostedForm() != null) {
91              // if it is a multipart form, request is already wrapped and parameters are read from multipartform object;
92              // parameters are sometimes read by form.getParameter (deprecated) so we have to convert values in
93              // multipartform.paramters map
94              for (Object key : MgnlContext.getPostedForm().getParameters().keySet()) {
95                  String[] value = transform((String[]) MgnlContext.getPostedForm().getParameters().get(key));
96                  MgnlContext.getPostedForm().getParameters().put((String) key, value);
97              }
98          }
99  
100         chain.doFilter(unicodeRequest, response);
101         
102         MgnlContext.pop();
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public boolean isEnabled()
110     {
111         // @todo this filter is enabled only if utf8 support is enabled. remove it when the check of this property is
112         // not needed anymore.
113         return super.isEnabled() && SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED);
114     }
115 
116     private static String[] transform(String[] input)
117     {
118         String[] toNormalize = input;
119         if (toNormalize != null && toNormalize.length > 0)
120         {
121             for (int i = 0; i < toNormalize.length; i++)
122             {
123                 toNormalize[i] = UnicodeNormalizer.normalizeNFC(toNormalize[i]);
124             }
125         }
126         return toNormalize;
127     }
128 
129     public class UnicodeNormalizerRequestWrapper extends HttpServletRequestWrapper
130     {
131 
132         private HttpServletRequest original;
133 
134         private Map parameters;
135 
136         /**
137          * @param request
138          */
139         public UnicodeNormalizerRequestWrapper(HttpServletRequest request)
140         {
141             super(request);
142             original = request;
143         }
144 
145         /**
146          * {@inheritDoc}
147          */
148         @Override
149         public String getParameter(String name)
150         {
151             String[] values = getParameterValues(name);
152             if (values != null && values.length > 0)
153             {
154                 return values[0];
155             }
156             return null;
157         }
158 
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         public Map getParameterMap()
164         {
165             if (parameters == null)
166             {
167                 parameters = new HashMap<String, String[]>();
168                 for (Object key : original.getParameterMap().keySet())
169                 {
170                     String[] value = transform((String[]) original.getParameterMap().get(key));
171                     parameters.put(key, value);
172                 }
173             }
174             return parameters;
175         }
176 
177         /**
178          * {@inheritDoc}
179          */
180         @Override
181         public String[] getParameterValues(String name)
182         {
183             return (String[]) getParameterMap().get(name);
184         }
185 
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public String getHeader(String name) {
191             String header = null;
192             try {
193                 header = super.getHeader(name);
194                 if (header != null) {
195                     header = URLDecoder.decode(header, getCharacterEncoding());
196                 }
197             }
198             catch (UnsupportedEncodingException e) {
199                 header = super.getHeader(name);
200             }
201             return header;
202         }
203 
204     }
205 
206 }