package com.madeu.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RequestCachingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            String contentType = request.getContentType();
            // JSON 요청만 래핑 (body에서 menuClass 파싱 필요)
            if (contentType != null && contentType.toLowerCase().contains("application/json")) {
                CachedBodyRequestWrapper wrappingRequest = new CachedBodyRequestWrapper(
                        (HttpServletRequest) request);
                chain.doFilter(wrappingRequest, response);
            } else {
                chain.doFilter(request, response);
            }
        } else {
            chain.doFilter(request, response);
        }
    }
}
