package filter;

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;

public class CorsFilter implements Filter {

	public CorsFilter() {
		super();
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) res;
		HttpServletRequest request = (HttpServletRequest) req;

		// cors header
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods",
				"POST, DELETE, GET, PUT, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers",
				"Origin, X-Requested-With, Content-Type, Accept");
		// response options method check
		if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
			return;
		}
		chain.doFilter(req, res);
	}

	@Override
	public void destroy() {
	}

}

@@ -255,54 +255,8 @@
 se;%0A
-import javax.servlet.http.HttpServletRequest;%0A
 impo
@@ -350,125 +350,1124 @@
 %0A%0A%09p
-ublic CorsFilter() %7B%0A%09%09super();%0A%09%7D%0A%0A%09@Override%0A%09public void init(FilterConfig filterConfig) throws ServletException %7B
+rivate String accessControlAllowOrigin = %22*%22;%0A%09private String accessControlAllowMethods = %22POST, DELETE, GET, PUT, OPTIONS%22;%0A%09private String accessControlAllowHeaders = %22Origin, X-Requested-With, Content-Type, Accept%22;%0A%0A%09public CorsFilter() %7B%0A%09%09super();%0A%09%7D%0A%0A%09@Override%0A%09public void init(FilterConfig filterConfig) throws ServletException %7B%0A%09%09String accessControlAllowOrigin = filterConfig%0A%09%09%09%09.getInitParameter(%22Access-Control-Allow-Origin%22);%0A%09%09String accessControlAllowMethods = filterConfig%0A%09%09%09%09.getInitParameter(%22Access-Control-Allow-Methods%22);%0A%09%09String accessControlAllowHeaders = filterConfig%0A%09%09%09%09.getInitParameter(%22Access-Control-Allow-Headers%22);%0A%09%09if (accessControlAllowOrigin != null%0A%09%09%09%09&& accessControlAllowOrigin.length() %3E 0) %7B%0A%09%09%09this.accessControlAllowOrigin = accessControlAllowOrigin;%0A%09%09%7D%0A%09%09if (accessControlAllowMethods != null%0A%09%09%09%09&& accessControlAllowMethods.length() %3E 0) %7B%0A%09%09%09this.accessControlAllowMethods = accessControlAllowMethods;%0A%09%09%7D%0A%09%09if (accessControlAllowHeaders != null%0A%09%09%09%09&& accessControlAllowHeaders.length() %3E 0) %7B%0A%09%09%09this.accessControlAllowHeaders = accessControlAllowHeaders;%0A%09%09%7D
 %0A%09%7D%0A
@@ -1665,65 +1665,8 @@
 res;
-%0A%09%09HttpServletRequest request = (HttpServletRequest) req;
 %0A%0A%09%09
@@ -1731,20 +1731,45 @@
 Origin%22,
- %22*%22
+%0A%09%09%09%09accessControlAllowOrigin
 );%0A%09%09res
@@ -1824,41 +1824,33 @@
 %09%09%09%09
-%22POST, DELETE, GET, PUT, OPTIONS%22
+accessControlAllowMethods
 );%0A%09
@@ -1909,165 +1909,36 @@
 %09%09%09%09
-%22Origin, X-Requested-With, Content-Type, Accept%22);%0A%09%09// response options method check%0A%09%09if (%22OPTIONS%22.equalsIgnoreCase(request.getMethod())) %7B%0A%09%09%09return;%0A%09%09%7D
+accessControlAllowHeaders);%0A
 %0A%09%09c

move config into init parameters