package com.rrtx.services.common.configure;

import com.rrtx.services.common.properties.ConfigProperties;
import com.rrtx.services.common.properties.SwaggerProperties;
import com.rrtx.services.common.xss.XssFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * ClassName: WebConfigure<br>
 * Description: WebConfigure <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 4:43 PM  xugz    文件初始创建
 */
@Configuration
public class WebConfigure {

	@Autowired
	private ConfigProperties properties;

	@Bean("fapAsyncThreadPool")
	public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(5);
		executor.setMaxPoolSize(20);
		executor.setQueueCapacity(200);
		executor.setKeepAliveSeconds(30);
		executor.setThreadNamePrefix("fap-Async-Thread");
		executor.setWaitForTasksToCompleteOnShutdown(true);
		executor.setAwaitTerminationSeconds(60);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.initialize();
		return executor;
	}

	/**
	 * XssFilter Bean
	 */
	@Bean
	@SuppressWarnings("all")
	public FilterRegistrationBean xssFilterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new XssFilter());
		filterRegistrationBean.setOrder(1);
		filterRegistrationBean.setEnabled(true);
		filterRegistrationBean.addUrlPatterns("/*");
		Map<String, String> initParameters = new HashMap<>();
		initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
		initParameters.put("isIncludeRichText", "true");
		filterRegistrationBean.setInitParameters(initParameters);
		return filterRegistrationBean;
	}

	@Bean
	public Docket swaggerApi() {
		SwaggerProperties swagger = properties.getSwagger();
		return new Docket(DocumentationType.SWAGGER_2)
				.select()
				.apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage()))
				.paths(PathSelectors.any())
				.build()
				.apiInfo(apiInfo(swagger));
	}

	private ApiInfo apiInfo(SwaggerProperties swagger) {
		return new ApiInfo(
				swagger.getTitle(),
				swagger.getDescription(),
				swagger.getVersion(),
				null,
				new Contact(swagger.getAuthor(), swagger.getUrl(), null),
				null, null, Collections.emptyList());
	}

}
