package org.ngrinder.infra.config;

import com.navercorp.lucy.security.xss.servletfilter.XssEscapeServletFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;

@Configuration
public class ServletFilterConfig {
	@Bean
	public FilterRegistrationBean characterEncodingFilterRRegisterBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		registrationBean.setFilter(characterEncodingFilter());
		registrationBean.setOrder(1);
		return registrationBean;
	}

	@Bean
	public CharacterEncodingFilter characterEncodingFilter(){
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		return characterEncodingFilter;
	}

	@Bean
	public FilterRegistrationBean xssEscapeServletFilter() {
		FilterRegistrationBean xssEscapeServletFilter = new FilterRegistrationBean(new XssEscapeServletFilter());
		String[] urlMappings = {"/login/*", "/perftest/*", "/user/*", "/script/*"};
		xssEscapeServletFilter.addUrlPatterns(urlMappings);
		xssEscapeServletFilter.setOrder(2);
		return xssEscapeServletFilter;
	}

	@Bean
	public FilterRegistrationBean httpPutFormContentFilter() {
		FilterRegistrationBean httpPutFormContentFilter = new FilterRegistrationBean(new HttpPutFormContentFilter());
		httpPutFormContentFilter.addUrlPatterns("/*");
		httpPutFormContentFilter.setOrder(3);
		return httpPutFormContentFilter;
	}

	@Bean
	public FilterRegistrationBean springOpenEntityManagerInViewFilter() {
		FilterRegistrationBean springOpenEntityManagerInViewFilter = new FilterRegistrationBean(new OpenEntityManagerInViewFilter());
		springOpenEntityManagerInViewFilter.addUrlPatterns("/*");
		springOpenEntityManagerInViewFilter.setOrder(4);
		return springOpenEntityManagerInViewFilter;
	}

	@Bean
	public FilterRegistrationBean hiddenHttpMethodFilter() {
		FilterRegistrationBean hiddenHttpMethodFilter = new FilterRegistrationBean(new HiddenHttpMethodFilter());
		hiddenHttpMethodFilter.addUrlPatterns("/*");
		hiddenHttpMethodFilter.setOrder(5);
		return hiddenHttpMethodFilter;
	}

	@Bean
	public FilterRegistrationBean pluggableServletFilterRegister() {
		FilterRegistrationBean pluggableServletFilter = new FilterRegistrationBean(new DelegatingFilterProxy());
		pluggableServletFilter.addUrlPatterns("/*");
		pluggableServletFilter.setName("pluggableServletFilter");
		pluggableServletFilter.setOrder(6);
		return pluggableServletFilter;
	}
}

@@ -98,32 +98,91 @@
 eServletFilter;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import org.sprin
@@ -675,22 +675,83 @@
 r;%0A%0A
-@Configuration
+import javax.servlet.Filter;%0A%0A@Configuration%0A@SuppressWarnings(%22unchecked%22)
 %0Apub
@@ -786,190 +786,103 @@
 g %7B%0A
+%0A
 %09@
-Bean%0A%09public FilterRegistrationBean characterEncodingFilterRRegisterBean() %7B%0A%09%09FilterRegistrationBean registrationBean = new FilterRegistrationBean();%0A%09%09registrationBean.set
+Value(%22$%7Bserver.default-encoding%7D%22)%0A%09private String defaultEncoding;%0A%0A%09@Bean%0A%09public 
 Filter
-(
+ 
 char
@@ -906,138 +906,9 @@
 er()
-);%0A%09%09registrationBean.setOrder(1);%0A%09%09return registrationBean;%0A%09%7D%0A%0A%09@Bean%0A%09public CharacterEncodingFilter characterEncodingFilter()
+ 
 %7B%0A%09%09
@@ -1017,16 +1017,21 @@
 lter.set
+Force
 Encoding
@@ -1035,15 +1035,68 @@
 ing(
-%22UTF-8%22
+true);%0A%09%09characterEncodingFilter.setEncoding(defaultEncoding
 );%0A%09
@@ -1475,9 +1475,9 @@
 der(
-2
+1
 );%0A%09
@@ -1780,9 +1780,9 @@
 der(
-3
+2
 );%0A%09
@@ -2136,9 +2136,9 @@
 der(
-4
+3
 );%0A%09
@@ -2444,9 +2444,9 @@
 der(
-5
+4
 );%0A%09
@@ -2806,9 +2806,9 @@
 der(
-6
+5
 );%0A%09

Fix character encoding filter registration