001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.web.common.encoding;
018
019import java.io.IOException;
020import java.io.UnsupportedEncodingException;
021
022import javax.servlet.Filter;
023import javax.servlet.FilterChain;
024import javax.servlet.FilterConfig;
025import javax.servlet.ServletException;
026import javax.servlet.ServletRequest;
027import javax.servlet.ServletResponse;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.platform.web.common.requestcontroller.service.RequestControllerManager;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Filter that sets encoding to UTF-8, before any other filter tries to parse the request. Also set the X-UA-Compatible
037 * meta for browsers.
038 * <p>
039 * See NXP-5555: the first parsing of the request is cached, so it should be done with the right encoding. See
040 * NXP-12862: we must pass the X-UA-Compatible meta in the header.
041 *
042 * @author Anahide Tchertchian
043 * @since 5.4.2
044 */
045public class NuxeoEncodingFilter implements Filter {
046
047    private static final Log log = LogFactory.getLog(NuxeoEncodingFilter.class);
048
049    @Override
050    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
051            ServletException {
052        if (request != null) {
053            // NXP-5555: set encoding to UTF-8 in case this method is called
054            // before
055            // encoding is set to UTF-8 on the request
056            if (request.getCharacterEncoding() == null) {
057                try {
058                    request.setCharacterEncoding("UTF-8");
059                } catch (UnsupportedEncodingException e) {
060                    log.error(e, e);
061                }
062            }
063
064            RequestControllerManager rcm = Framework.getLocalService(RequestControllerManager.class);
065            for (String headerName : rcm.getResponseHeaders().keySet()) {
066                if (response instanceof HttpServletResponse
067                        && !((HttpServletResponse) response).containsHeader(headerName)) {
068                    ((HttpServletResponse) response).addHeader(headerName, rcm.getResponseHeaders().get(headerName));
069                }
070            }
071        }
072
073        chain.doFilter(request, response);
074    }
075
076    @Override
077    public void destroy() {
078    }
079
080    @Override
081    public void init(FilterConfig filterConfig) throws ServletException {
082    }
083
084}