package com.emr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.util.HttpClientTool;
import com.emr.util.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/9/5 9:35
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/9/5 9:35
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Component
public class UrlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void destroy() {

    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {
        //获取权限系统请求头ip+端口
        String POWER_URLHEAD = PropertiesUtils.getProperty("POWER_URLHEAD");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        //获取相对路径
        String url = request.getServletPath();
        //从session中取token
        String token = (String) request.getSession().getAttribute("token");
        //静态资源不拦截验证
        if(url.endsWith(".css")||url.endsWith(".js")||url.endsWith(".jpg")
                ||url.endsWith(".gif")||url.endsWith(".png") || url.endsWith(".ico")||url.endsWith(".pdf")){
            chain.doFilter(request, response);
            response.setHeader("Access-Control-Allow-Origin", "*");
            return;
        }
        String[] split = url.split("/");
        if(null != split && split.length != 0) {
            String headUrl = "/" + split[1];
            if (!"/getSessionRemainingTime".equals(url)) {
                request.getSession().setAttribute(request.getSession().getId(), System.currentTimeMillis());
            }
            //过滤第一次进入该系统请求
            if (!"/login".equals(url) && !"/font".equals(headUrl) && StringUtils.isNoneBlank(token)) {
                try {
                    // 创建url
                    String requestUrl = POWER_URLHEAD + "/font/checkToken";
                    // 执行请求
                    Map<String, String> params = new HashMap<>();
                    params.put("token", token);
                    String resultString = HttpClientTool.doPost(requestUrl, params);
                    String code = JSONArray.toJSONString(JSON.parseObject(resultString).get("code"));
                    if ("200".equals(code)) {
                        response.sendRedirect(POWER_URLHEAD + "/login");
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    response.sendRedirect(POWER_URLHEAD + "/login");
                    return;
                }
            } else {
                if ("/font".equals(headUrl)) {
                    response.setHeader("Access-Control-Allow-Origin", "*");
                }
            }
        }
        // 如果有下一个过滤器则跳转到下一个过滤器否则目标页面
        chain.doFilter(request, response);
    }
}
