package com.persagy.revit

import com.alibaba.fastjson.PropertyNamingStrategy
import com.persagy.service.interceptors.SPageInterceptor
import com.persagy.service.json.SJsonHttpMessageConverter
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter

/**
 *
 *
 * @author jxing
 */
@Configuration
open class WebMvcConfg: WebMvcConfigurer {

    /**
     * 设置接口超时时间
     */
//    override fun configureAsyncSupport(configurer : AsyncSupportConfigurer) {
//        configurer.setDefaultTimeout(1000 * 60 * 60)
//        configurer.registerCallableInterceptors(timeoutInterceptor())
//    }
//    @Bean
//    open fun timeoutInterceptor() : TimeoutCallableProcessingInterceptor {
//        return TimeoutCallableProcessingInterceptor()
//    }

    /**
     * 配置拦截器
     *
     * @param   registry        拦截器注册器
     */
    override fun addInterceptors(registry : InterceptorRegistry) {
        //registry.addInterceptor(AccessControlAllowOriginInterceptor()).addPathPatterns("/**")
//        registry.addInterceptor(SAccessControlAllowOriginInterceptor()).addPathPatterns("/**")
        registry.addInterceptor(SPageInterceptor()).addPathPatterns("/**")
        super.addInterceptors(registry)
    } // Function addInterceptors()


    /**
     * 配置 json 转换使用 FastJson
     *
     * @param   converterList       转换器列表
     */
    override fun configureMessageConverters(converterList: MutableList<HttpMessageConverter<*>>) {
        // 先把JackSon的消息转换器删除.
        converterList.removeIf { item -> item is MappingJackson2HttpMessageConverter }

        val converter = SJsonHttpMessageConverter()
        // 设置 Json 序列化，属性首字母小写
        converter.propertyNamingStrategy = PropertyNamingStrategy.CamelCase
        converterList.add(converter)
    }
} // Class WebMvcConf()