package com.persagy.dmp.file.handler;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.file.config.AccountConfig;
import com.persagy.dmp.file.context.OldFileAppContext;
import com.persagy.dmp.file.context.OldFileContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OldFileAuthHandler implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        OldFileContext context = OldFileAppContext.getContext();
        if (StrUtil.isBlank(context.getSystemId()) || StrUtil.isBlank(context.getSecret())){
            throw new BusinessException("文件服务器密钥有误");
        }
        if (!AccountConfig.accountMap.containsKey(context.getSystemId())
                || !context.getSecret().equals(AccountConfig.accountMap.get(context.getSystemId()))){
            throw new BusinessException("文件服务器密钥有误");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
