View Javadoc

1   /**
2    * This file Copyright (c) 2003-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.beans.config.MIMEMapping;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.context.MgnlContext;
39  
40  import java.io.IOException;
41  import java.io.UnsupportedEncodingException;
42  import java.net.URLDecoder;
43  
44  import javax.servlet.FilterChain;
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  
52  /**
53   * TODO : rename this filter. What it really does is initialize and setup the basic,
54   * non-content related attributes of the AggregationState. ContentType could become an
55   * attribute of the AggregationState too and could be set later.
56   *
57   * FIXME: the original uri should not be reset, MAGNOLIA-3204
58   *
59   * @author Sameer Charles
60   * @author Fabrizio Giustina
61   * @author gjoseph
62   * @version $Id: ContentTypeFilter.java 34573 2010-05-20 07:49:54Z had $
63   */
64  public class ContentTypeFilter extends AbstractMgnlFilter {
65  
66      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ContentTypeFilter.class);
67  
68      /**
69       * If set we have to reset the aggregation state before setting the original URI/URL with new values
70       */
71      private static final String AGGREGATION_STATE_INITIALIZED = ContentTypeFilter.class.getName() + ".aggregationStateInitialized";
72  
73      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
74          // we will set the original uri, to avoid conflicts we have to reset the aggregation state
75          // this will mainly reset the original uri and keep all other information
76          if(request.getAttribute(AGGREGATION_STATE_INITIALIZED) != null){
77              MgnlContext.resetAggregationState();
78          }
79          else{
80              request.setAttribute(AGGREGATION_STATE_INITIALIZED, Boolean.TRUE);
81          }
82  
83          final String originalUri = request.getRequestURI();
84          final String ext = getUriExtension(originalUri);
85          StringBuffer url = request.getRequestURL();
86          String query = request.getQueryString();
87          if (!StringUtils.isEmpty(query))
88          {
89              url.append("?").append(query);
90          }
91  
92          final String characterEncoding = setupContentTypeAndCharacterEncoding(ext, request, response);
93          final AggregationState aggregationState = MgnlContext.getAggregationState();
94          aggregationState.setCharacterEncoding(characterEncoding);
95  
96          aggregationState.setOriginalURI(URLDecoder.decode(originalUri, characterEncoding));
97          aggregationState.setOriginalURL(URLDecoder.decode(url.toString(), characterEncoding));
98          aggregationState.setOriginalBrowserURI(originalUri);
99          aggregationState.setOriginalBrowserURL(url.toString());
100         aggregationState.setExtension(ext);
101 
102         chain.doFilter(request, response);
103     }
104 
105     // TODO : test + simplification (substringAfterLast(uri, ".") probably does the trick !?
106     protected String getUriExtension(String originalUri) {
107         final String fileName = StringUtils.substringAfterLast(originalUri, "/");
108         return StringUtils.substringAfterLast(fileName, ".");
109     }
110 
111     protected String setupContentTypeAndCharacterEncoding(String extension, HttpServletRequest req, HttpServletResponse resp) {
112         final String mimeType = MIMEMapping.getMIMETypeOrDefault(extension);
113         final String characterEncoding = MIMEMapping.getContentEncodingOrDefault(mimeType);
114 
115         try {
116             // let's not override the request encoding if set by the app server
117             if (req.getCharacterEncoding() == null) {
118                 req.setCharacterEncoding(characterEncoding);
119             }
120         } catch (UnsupportedEncodingException e) {
121             log.error("Can't set character encoding for the request (extension=" + extension + ",mimetype=" + mimeType + ")", e);
122         }
123 
124         resp.setContentType(mimeType);
125         resp.setCharacterEncoding(characterEncoding);
126 
127         return characterEncoding;
128     }
129 
130 }