package com.uas.report.filter;

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 com.uas.report.exception.ExceptionHandlerAdvice;
import com.uas.report.util.IpHelper;

/**
 * 对文件删除进行过滤
 * 
 * @author sunyj
 * @since 2016年12月13日 下午4:46:03
 */
public class FileDeleteFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		// 局域网内才允许执行
		String ip = IpHelper.getIp(httpRequest);
		if (!IpHelper.isLAN(ip)) {
			ExceptionHandlerAdvice.handleError(httpResponse, HttpServletResponse.SC_FORBIDDEN, "没有权限", ip);
		} else {
			chain.doFilter(httpRequest, response);
		}
	}

	@Override
	public void destroy() {

	}

}
