package cn.sagacloud.server.algorithm.filter

import javax.servlet.*
import javax.servlet.annotation.WebFilter
import javax.servlet.http.HttpServletRequest
import java.io.IOException
import javax.servlet.FilterConfig

@WebFilter(urlPatterns = ["/aaa"], filterName = "uploadFilter")
class UploadFilter : Filter {

    @Throws(ServletException::class)
    override fun init(filterConfig: FilterConfig?) {

        println("----------------------->过滤器被创建")
    }

    @Throws(IOException::class, ServletException::class)
    override fun doFilter(servletRequest: ServletRequest, servletResponse: ServletResponse, filterChain: FilterChain) {
        val req = servletRequest as HttpServletRequest
        val requestURI = req.requestURI
        println("--------------------->过滤器：请求地址$requestURI")
        if (requestURI.endsWith("upload")) {
            //servletRequest.getRequestDispatcher("/failed").forward(servletRequest, servletResponse);
            println("filter")
        }
        filterChain.doFilter(servletRequest, servletResponse)
    }

    override fun destroy() {
        println("----------------------->过滤器被销毁")
    }
}
