package com.gyee.frame.common.interceptor;

import com.gyee.frame.common.conf.ImgConfiguration;
import com.gyee.frame.common.conf.V2Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 拦截器
* @ClassName: MyWebAppConfigurer
* @author gyee
* @date 2018年6月3日
*
 */
@Configuration
public class MyWebAppConfigurer  extends  WebMvcConfigurationSupport  {
	private static final Logger log = LoggerFactory.getLogger(MyWebAppConfigurer.class);
	/** 解决跨域问题 **/
	@Override
	public void addCorsMappings(CorsRegistry registry){
		/*
			registry.addMapping("/**")
			// 设置允许跨域请求的域名
			.allowedOrigins("*")
			// 是否允许证书
			.allowCredentials(true)
			// 设置允许的方法
			.allowedMethods("GET", "POST", "DELETE", "PUT")
			// 设置允许的header属性
			.allowedHeaders("*")
			// 跨域允许时间
			.maxAge(3600);
			super.addCorsMappings(registry);
		*/
	}
	
	/** 添加拦截器 **/
	@Override
	protected void addInterceptors(InterceptorRegistry registry){
		registry.addInterceptor(new MyInterceptor());
		super.addInterceptors(registry);
	}
	
	/** 这里配置视图解析器 **/
	@Override
	protected void configureViewResolvers(ViewResolverRegistry registry){
		super.configureViewResolvers(registry);
	}
	
	/** 配置内容裁决的一些选项 **/
	@Override
	protected void configureContentNegotiation(ContentNegotiationConfigurer configurer){
		super.configureContentNegotiation(configurer);
	}
	
	/** 视图跳转控制器 **/
	@Override
	protected void addViewControllers(ViewControllerRegistry registry) {
		
		super.addViewControllers(registry);
	}
	
	
	/** 静态资源处理 **/
	@Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

		String imgurl= V2Config.imgurl;
		imgurl="file:"+imgurl;
		log.info("**************初始化图片存储路径******************");
		log.info(imgurl);
		log.info("********************************");
		registry.addResourceHandler("/IMGURL/**").addResourceLocations(imgurl);

		String audurl=V2Config.audurl;
		audurl="file:"+audurl;
		log.info("**************初始化音频存储路径******************");
		log.info(audurl);
		log.info("********************************");
		registry.addResourceHandler("/ADUURL/**").addResourceLocations(audurl);


		String iconurl=V2Config.iconurl;
		iconurl="file:"+iconurl;
		log.info("**************初始化头像存储路径******************");
		log.info(iconurl);
		log.info("********************************");
		registry.addResourceHandler("/ICONURL/**").addResourceLocations(iconurl);


		String videourl=V2Config.videourl;
		videourl="file:"+videourl;
		log.info("**************初始化视频存储路径******************");
		log.info(videourl);
		log.info("********************************");
		registry.addResourceHandler("/VIDEOURL/**").addResourceLocations(videourl);




		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

    }
	/** 默认静态资源处理器 **/
	
	protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		//super.configureDefaultServletHandling(configurer);
		//configurer.enable("stati");
		configurer.enable();
	}
	
	
	
	
}
