package com.style24.admin.support.readable;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * 다시 읽을 수 있는 Request Filter
 * 		RequestBody는 request.getInputStream()을 통해 한 번만 읽을 수 있다.
 * 		Controller 또는 Interceptor에서 계속 액세스 하기 위해 구현한 Request Filter
 * 
 * @author gagamel
 * @since 2021. 4. 20
 */
@WebFilter("/readableRequestFilter")
@Slf4j
public class TsaRequestFilter implements Filter {

	/**
	 * Default constructor. 
	 */
	public TsaRequestFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		TsaHttpServletRequestWrapper wrappedRequest = null;

		try {
			wrappedRequest = new TsaHttpServletRequestWrapper((HttpServletRequest)request);
		} catch (Exception e) {
			log.error("Fail to wrap request", e);
		}

		chain.doFilter(wrappedRequest, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
