package com.bocloud.ims.booter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.bocloud.cmp.boot.interceptor.AccessInterceptor;
import com.bocloud.cmp.boot.interceptor.LicenseInterceptor;
import com.bocloud.cmp.boot.interceptor.LogInterceptor;

/**
 * 拦截器配置类
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年3月30日
 */
@Component
public class InterceptorConfiguration implements WebMvcConfigurer {

	@Autowired
	private AccessInterceptor accessInterceptor;
	@Autowired
	private LogInterceptor logInterceptor;
	@Autowired
	private LicenseInterceptor licenseInterceptor;
	final String[] pathPatterns = { "/v1/vms", "/v1/hosts" };

	/**
	 * 添加拦截器 (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer#
	 * addInterceptors(org.springframework.web.servlet.config.annotation.
	 * InterceptorRegistry)
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(accessInterceptor).addPathPatterns("/**").order(0).excludePathPatterns("/status",
				"/status/**", "/v1/logview", "/v1/logzip", "/status/db", "/v1/tenants/**", "/favicon.ico", "/doc.html",
				"/swagger-resources/**", "/swagger-ui.html/**", "/webjars/**", "/v2/**", "/error", "/v1/config/**",
				"/v1/status/**", "/v1/other/**");
		registry.addInterceptor(logInterceptor).addPathPatterns("/**")
				.excludePathPatterns("/v1/logview", "/v1/logzip", "/favicon.ico", "/doc.html", "/swagger-resources/**",
						"/swagger-ui.html/**", "/webjars/**", "/v2/**", "/error")
				.order(1);
		registry.addInterceptor(licenseInterceptor).addPathPatterns(pathPatterns)
				.excludePathPatterns("/v1/logview", "/v1/logzip", "/v1/config/captcha/install", "/v1/config/license",
						"/status", "/status/db", "/status/sid", "/status/sids", "/v1/status/sid", "/v1/status/sids",
						"/favicon.ico", "/doc.html", "/swagger-resources/**", "/swagger-ui.html/**", "/webjars/**",
						"/v2/**", "/error")
				.order(2);
	}

}
