package com.example.JSiter.filter;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * Created by hdong on 19/09/2017.
 */
@WebFilter
public class InitialFilter implements Filter {

    private static final Logger logger = Logger.getLogger("InitialFilter");

    @Override
    public void destroy() {
        System.out.println("destroy");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        logger.warning("initial filter");
        chain.doFilter(request, response);
        System.out.println("do init filter");
    }

    @Override
    public void init(FilterConfig fConfig) throws ServletException {
        ServletContext sc = fConfig.getServletContext();
        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
        if (cxt != null) {

        }
        System.out.println("init finished");
    }
}