package xtools.boot.web.base.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.jspecify.annotations.NonNull;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import xtools.base.config.BaseParams;
import xtools.boot.core.holder.CommonHolder;
import xtools.boot.web.filter.base.BaseFilter;

import java.io.IOException;

/**
 * <p>Title : CommonFilter</p>
 * <p>Description : CommonFilter</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/1/6 16:37
 */
@Slf4j
@Component
public class CommonFilter extends BaseFilter implements Ordered, BaseParams {

    @Override
    public int getOrder() {
        return CP_NUM100 + CP_NUM1;
    }

    @Override
    protected void doFilterInternal(
            @NonNull HttpServletRequest request,
            @NonNull HttpServletResponse response,
            @NonNull FilterChain filterChain
    ) {
        ScopedValue.where(CommonHolder.getScoped(), CommonHolder.create()).run(() -> {
            try {
                filterChain.doFilter(request, response);
            } catch (IOException | ServletException e) {
                printLog(e, "添加通用ScopedValue异常");
            }
        });
    }

}
