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