package com.bb.config;

import java.io.IOException;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;


import com.bb.util.StringUtils;

@Component
public class ApiKeyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		
		
		String reqKey = req.getHeader("Authorization");
		
		if (StringUtils.isEmpty(reqKey)) {
			
			res.setStatus(HttpStatus.UNAUTHORIZED.value());
		} else {
			request.setAttribute("Authorization", reqKey);
			chain.doFilter(request, response);
		}
    }
}
