package com.brframework.commonweb.utils;

import com.brframework.commonweb.exception.HandleException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ServletUtils
 * @author xu
 * @date 2019/8/19 10:40
 */
public class ServletUtils {

    /**
     * 获取ServletRequestAttributes
     * @return ServletRequestAttributes
     */
    public static ServletRequestAttributes getServletRequestAttributes(){
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 获取当前 request
     * @return
     */
    public static HttpServletRequest request(){
        if(getServletRequestAttributes() == null || getServletRequestAttributes() == null){
            throw new HandleException("当前非WEB环境");
        }

        return getServletRequestAttributes().getRequest();
    }

    /**
     * 获取当前 response
     * @return
     */
    public static HttpServletResponse response(){
        return getServletRequestAttributes().getResponse();
    }

}
