001/* 002 * (C) Copyright 2013 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-2.1.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 * Arnaud Kervern 016 */ 017 018package org.nuxeo.ecm.platform.web.common.requestcontroller.filter; 019 020import java.io.IOException; 021import javax.servlet.FilterChain; 022import javax.servlet.FilterConfig; 023import javax.servlet.ServletException; 024import javax.servlet.ServletRequest; 025import javax.servlet.ServletResponse; 026import javax.servlet.http.HttpServletRequest; 027import org.nuxeo.ecm.platform.web.common.requestcontroller.service.RequestControllerManager; 028import org.nuxeo.runtime.api.Framework; 029import com.thetransactioncompany.cors.CORSFilter; 030 031/** 032 * Nuxeo CORS filter wrapper to com.thetransactioncompany.cors.CORSFilter allowing to configure cors filter depending of 033 * the request url. Each time a request matchs a contribution is found, CORSFilter had to be re-initialized to change 034 * his configurations. 035 * 036 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 037 * @since 5.7.2 038 */ 039public class NuxeoCorsFilter extends CORSFilter { 040 041 @Override 042 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 043 ServletException { 044 FilterConfig filterConfig = getFilterConfigFrom(request); 045 if (filterConfig != null) { 046 super.init(filterConfig); 047 super.doFilter(request, response, chain); 048 } else { 049 chain.doFilter(request, response); 050 } 051 } 052 053 protected FilterConfig getFilterConfigFrom(ServletRequest request) { 054 if (!(request instanceof HttpServletRequest)) { 055 return null; 056 } 057 return Framework.getLocalService(RequestControllerManager.class).getCorsConfigForRequest( 058 (HttpServletRequest) request); 059 } 060}