package com.sikey.wa04.common;

import com.sikey.wa04.common.dto.header.GenericManufacturerHeader;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.Map;

public class BaseController {

    /**
     * 获取当前请求
     *
     * @return 当前请求
     */
    public HttpServletRequest getCurrentRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attributes.getRequest();
    }

    /**
     * 获取请求头
     *
     * @return 请求头
     */
    public GenericManufacturerHeader genericManufacturerHeader() {
        HttpServletRequest request = getCurrentRequest();
        Map<String, Object> headers = new HashMap<>();
        request.getHeaderNames().asIterator().forEachRemaining(name -> headers.put(name, request.getHeader(name)));
        return new GenericManufacturerHeader(headers);
    }

}
