package utis.wall.common.util;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MacAddressFilter implements Filter {
	
	protected FilterConfig 	filterConfig 		= null;
	protected String	 	macaddressCheckPage	= "";
	
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig		 = filterConfig;
		this.macaddressCheckPage = filterConfig.getInitParameter("macaddress-check-page");
	}
	
	public void destroy() {

		this.filterConfig = null;
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {

		boolean validRequest = false;
		
		System.out.println("#######################");
		
		if(request instanceof HttpServletRequest) {
			HttpServletRequest hreq = (HttpServletRequest)request;
			HttpSession ses 		= hreq.getSession();
			
			validRequest = ses.getAttribute("MACADDRESS")!=null;
			
			if(validRequest){
				chain.doFilter(request, response);
			}else{
				HttpServletResponse hres = (HttpServletResponse)response;
				hres.sendRedirect( this.macaddressCheckPage );
			}
		}
	}
}
