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   * Normalizes the current URI to the NFC form which is used internally.
56   * @see UnicodeNormalizer
57   *
58   * @author Luca Boati
59   * @version $Id: $
60   */
61  public class UnicodeNormalizationFilter extends AbstractMgnlFilter
62  {
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
69          throws IOException, ServletException
70      {
71          final AggregationState aggregationState = MgnlContext.getAggregationState();
72          String originalBrowserURI = MgnlContext.getContextPath() + aggregationState.getOriginalBrowserURI();
73          String originalBrowserURL = aggregationState.getOriginalBrowserURL();
74          String originalURINormalized = MgnlContext.getContextPath() + UnicodeNormalizer.normalizeNFC(aggregationState.getOriginalURI());
75          String originalURLNormalized = UnicodeNormalizer.normalizeNFC(aggregationState.getOriginalURL());
76          String currentURI = MgnlContext.getContextPath() + aggregationState.getCurrentURI();
77  
78          // reset uri of the aggregationState in order to set new values for decoded uris
79          MgnlContext.resetAggregationState();
80  
81          // restore some values
82          MgnlContext.getAggregationState().setOriginalBrowserURI(originalBrowserURI);
83          MgnlContext.getAggregationState().setOriginalBrowserURL(originalBrowserURL);
84          MgnlContext.getAggregationState().setCurrentURI(currentURI);
85  
86          // set new values for original uri and url
87          MgnlContext.getAggregationState().setOriginalURI(originalURINormalized);
88          MgnlContext.getAggregationState().setOriginalURL(originalURLNormalized);
89  
90          HttpServletRequest unicodeRequest = new UnicodeNormalizerRequestWrapper(request);
91          MgnlContext.push(unicodeRequest, response);
92  
93          if (MgnlContext.getPostedForm() != null) {
94              // if it is a multipart form, request is already wrapped and parameters are read from multipartform object;
95              // parameters are sometimes read by form.getParameter (deprecated) so we have to convert values in
96              // multipartform.paramters map
97              for (Object key : MgnlContext.getPostedForm().getParameters().keySet()) {
98                  String[] value = transform((String[]) MgnlContext.getPostedForm().getParameters().get(key));
99                  MgnlContext.getPostedForm().getParameters().put((String) key, value);
100             }
101         }
102 
103         chain.doFilter(unicodeRequest, response);
104 
105         MgnlContext.pop();
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public boolean isEnabled()
113     {
114         // @todo this filter is enabled only if utf8 support is enabled. remove it when the check of this property is
115         // not needed anymore.
116         return super.isEnabled() && SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED);
117     }
118 
119     private static String[] transform(String[] input)
120     {
121         String[] toNormalize = input;
122         if (toNormalize != null && toNormalize.length > 0)
123         {
124             for (int i = 0; i < toNormalize.length; i++)
125             {
126                 toNormalize[i] = UnicodeNormalizer.normalizeNFC(toNormalize[i]);
127             }
128         }
129         return toNormalize;
130     }
131 
132     /**
133      * Normalizes the parameter strings.
134      */
135     public class UnicodeNormalizerRequestWrapper extends HttpServletRequestWrapper
136     {
137 
138         private HttpServletRequest original;
139 
140         private Map parameters;
141 
142         /**
143          * @param request
144          */
145         public UnicodeNormalizerRequestWrapper(HttpServletRequest request)
146         {
147             super(request);
148             original = request;
149         }
150 
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public String getParameter(String name)
156         {
157             String[] values = getParameterValues(name);
158             if (values != null && values.length > 0)
159             {
160                 return values[0];
161             }
162             return null;
163         }
164 
165         /**
166          * {@inheritDoc}
167          */
168         @Override
169         public Map getParameterMap()
170         {
171             if (parameters == null)
172             {
173                 parameters = new HashMap<String, String[]>();
174                 for (Object key : original.getParameterMap().keySet())
175                 {
176                     String[] value = transform((String[]) original.getParameterMap().get(key));
177                     parameters.put(key, value);
178                 }
179             }
180             return parameters;
181         }
182 
183         /**
184          * {@inheritDoc}
185          */
186         @Override
187         public String[] getParameterValues(String name)
188         {
189             return (String[]) getParameterMap().get(name);
190         }
191 
192         /**
193          * {@inheritDoc}
194          */
195         @Override
196         public String getHeader(String name) {
197             String header = null;
198             try {
199                 header = super.getHeader(name);
200                 if (header != null) {
201                     header = URLDecoder.decode(header, getCharacterEncoding());
202                 }
203             }
204             catch (UnsupportedEncodingException e) {
205                 header = super.getHeader(name);
206             }
207             return header;
208         }
209 
210         public HttpServletRequest getOriginal() {
211             return original;
212         }
213 
214     }
215 
216 }