001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.web.common.encoding; 020 021import java.io.IOException; 022import java.io.UnsupportedEncodingException; 023 024import javax.servlet.Filter; 025import javax.servlet.FilterChain; 026import javax.servlet.FilterConfig; 027import javax.servlet.ServletException; 028import javax.servlet.ServletRequest; 029import javax.servlet.ServletResponse; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.nuxeo.ecm.platform.web.common.requestcontroller.service.RequestControllerManager; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Filter that sets encoding to UTF-8, before any other filter tries to parse the request. Also set the X-UA-Compatible 039 * meta for browsers. 040 * <p> 041 * See NXP-5555: the first parsing of the request is cached, so it should be done with the right encoding. See 042 * NXP-12862: we must pass the X-UA-Compatible meta in the header. 043 * 044 * @author Anahide Tchertchian 045 * @since 5.4.2 046 */ 047public class NuxeoEncodingFilter implements Filter { 048 049 private static final Log log = LogFactory.getLog(NuxeoEncodingFilter.class); 050 051 @Override 052 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 053 ServletException { 054 if (request != null) { 055 // NXP-5555: set encoding to UTF-8 in case this method is called 056 // before 057 // encoding is set to UTF-8 on the request 058 if (request.getCharacterEncoding() == null) { 059 try { 060 request.setCharacterEncoding("UTF-8"); 061 } catch (UnsupportedEncodingException e) { 062 log.error(e, e); 063 } 064 } 065 066 RequestControllerManager rcm = Framework.getService(RequestControllerManager.class); 067 for (String headerName : rcm.getResponseHeaders().keySet()) { 068 if (response instanceof HttpServletResponse 069 && !((HttpServletResponse) response).containsHeader(headerName)) { 070 ((HttpServletResponse) response).addHeader(headerName, rcm.getResponseHeaders().get(headerName)); 071 } 072 } 073 } 074 075 chain.doFilter(request, response); 076 } 077 078 @Override 079 public void destroy() { 080 } 081 082 @Override 083 public void init(FilterConfig filterConfig) throws ServletException { 084 } 085 086}