package com.bb.config;

/*
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
@Component
public class AccessLogFilter implements Filter {
 
    private static final Logger LOG = LoggerFactory.getLogger(AccessLogFilter.class);
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
 
    @Override
    public void destroy() {
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        CustomRequestWrapper customRequestWrapper = new CustomRequestWrapper((HttpServletRequest) servletRequest);
        String accessLog = buildAccessLog(customRequestWrapper);
        LOG.info(accessLog);
 
        try {
            filterChain.doFilter(customRequestWrapper, servletResponse);
        } finally {
            customRequestWrapper.setBody(null);
        }
    }
 
    private String buildAccessLog(CustomRequestWrapper customRequestWrapper) {
 
        String requestURL = getRequestURL(customRequestWrapper);
        String remoteAddr = getRemoteAddr(customRequestWrapper);
        String method = getMethod(customRequestWrapper);
        String queryString = getQueryString(customRequestWrapper);
        String requestBody = getRequestBody(customRequestWrapper);
 
        StringBuilder sb = new StringBuilder();
        sb.append("##### ACCESS LOG {");
        if (requestURL != null) {
            sb
                    .append("\"").append("requestURL").append("\"")
                    .append(":")
                    .append("\"").append(requestURL).append("\"");
        }
        if (remoteAddr != null) {
            sb
                    .append(",")
                    .append("\"").append("remoteAddr").append("\"")
                    .append(":")
                    .append("\"").append(remoteAddr).append("\"");
        }
        if (method != null) {
            sb
                    .append(",")
                    .append("\"").append("method").append("\"")
                    .append(":")
                    .append("\"").append(method).append("\"");
        }
        if (queryString != null) {
            sb
                    .append(",")
                    .append("\"").append("queryString").append("\"")
                    .append(":")
                    .append("\"").append(queryString).append("\"");
        }
        if (requestBody != null && requestBody.length() > 0) {
            sb
                    .append(",")
                    .append("\"").append("body").append("\"")
                    .append(":")
                    .append("\"").append(requestBody).append("\"");
        }
        sb.append("}");
        return sb.toString();
    }
 
    private String getRequestBody(CustomRequestWrapper customRequestWrapper) {
        String content = null;
        String method = customRequestWrapper.getMethod().toLowerCase();
 
        // POST, PUT + application/json
        if (method.startsWith("p")) {
            if (customRequestWrapper.getContentType().toLowerCase().indexOf("json") > 0) {
                try {
                    content = new String(customRequestWrapper.getBody(), customRequestWrapper.getCharacterEncoding());
                } catch (UnsupportedEncodingException e) {
                    LOG.error(e.getMessage());
                }
            }
        }
        return content;
    }
 
    private String getQueryString(CustomRequestWrapper customRequestWrapper) {
        String queryString = null;
        if (customRequestWrapper.getQueryString() != null) {
            queryString = customRequestWrapper.getQueryString();
        }
        return queryString;
    }
 
    private String getMethod(CustomRequestWrapper customRequestWrapper) {
        return customRequestWrapper.getMethod();
    }
 
    private String getRemoteAddr(CustomRequestWrapper customRequestWrapper) {
        return customRequestWrapper.getRemoteAddr();
    }
 
    private String getRequestURL(CustomRequestWrapper customRequestWrapper) {
        return customRequestWrapper.getRequestURL().toString();
    }

}
 */
