package com.hotent.base.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
 * 修改请求路由
 **/
public class UrlFilter implements Filter {


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

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        String path=httpRequest.getRequestURI();
        // 路径包含/v2/,且放掉swagger的相关路径
        if(path.contains("/v2/") && !path.contains("/v2/api-docs")){
            request.setAttribute("originURI",path);
            path=path.replace("/v2/","/v1/");
            httpRequest.getRequestDispatcher(path).forward(request,response);
        }
       else {
            chain.doFilter(request,response);
        }
        return;
       
    }
}