top.cutexingluo.tools.utils.ee.web.holder.HttpContextUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xingtools-pkg-jdk8 Show documentation
Show all versions of xingtools-pkg-jdk8 Show documentation
xingtools 依赖core包,jdk 分类包,对不同的jdk版本提供兼容性
The newest version!
package top.cutexingluo.tools.utils.ee.web.holder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.cutexingluo.tools.bridge.servlet.HttpServletRequestData;
import top.cutexingluo.tools.bridge.servlet.HttpServletResponseData;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
/**
* 全局获取HttpServletRequest、HttpServletResponse
*
* @since 1.0.4
*/
public class HttpContextUtil {
protected HttpContextUtil() {
}
public static RequestAttributes getRequestAttributes() {
return RequestContextHolder.getRequestAttributes();
}
@Nullable
public static ServletRequestAttributes getServletRequestAttributes() {
return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
}
/**
* fix v1.0.5
*
* @return HttpServletRequest
*/
@Nullable
public static HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes requestAttributes = getServletRequestAttributes();
if (Objects.isNull(requestAttributes)) {
return null;
}
return requestAttributes.getRequest();
}
/**
* fix v1.0.5
*
* @return HttpServletResponse
*/
@Nullable
public static HttpServletResponse getHttpServletResponse() {
ServletRequestAttributes requestAttributes = getServletRequestAttributes();
if (Objects.isNull(requestAttributes)) {
return null;
} else {
return requestAttributes.getResponse();
}
}
/**
* 返回封装类,兼容不同版本
*
* @since 1.1.1
*/
@NotNull
public static HttpServletRequestData getHttpServletRequestData() {
return new HttpServletRequestData(getHttpServletRequest());
}
/**
* 返回封装类,兼容不同版本
*
* @since 1.1.1
*/
@NotNull
public static HttpServletResponseData getHttpServletResponseData() {
return new HttpServletResponseData(getHttpServletResponse());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy