/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.management.security.config.basic.filter;

import io.gravitee.common.http.HttpHeaders;
import io.gravitee.common.http.HttpMethod;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Titouan COMPIEGNE
 * 
 * TODO : fix filter order between Jersey Filter (CORSResponseFilter) and Spring Security Filter
 * TODO : remove this filter or CORSResponseFilter when the problem will be solved
 *
 */
public class CORSFilter implements Filter {
	
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) res;
		HttpServletRequest request = (HttpServletRequest) req;
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, PUT, GET, OPTIONS, DELETE, X-XSRF-TOKEN");
		response.setHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "1209600");

		if (!HttpMethod.OPTIONS.equals(request.getMethod())) {
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig filterConfig) {
	}

	public void destroy() {
	}

}

@@ -778,37 +778,8 @@
 d;%0A%0A
-import java.io.IOException;%0A%0A
 impo
@@ -795,22 +795,17 @@
 servlet.
-Filter
+*
 ;%0Aimport
@@ -823,168 +823,31 @@
 let.
-FilterChain;%0Aimport javax.servlet.FilterConfig;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequest;%0Aimport javax.servlet.ServletResponse
+http.HttpServletRequest
 ;%0Aim
@@ -875,37 +875,38 @@
 tp.HttpServletRe
-quest
+sponse
 ;%0Aimport javax.s
@@ -906,42 +906,23 @@
 java
-x.servlet.http.HttpServletResponse
+.io.IOException
 ;%0A%0A/
@@ -1498,11 +1498,35 @@
 IN, 
-%22*%22
+request.getHeader(%22origin%22)
 );%0A%09

Add header CORS filter