Initial project
This commit is contained in:
360
src/main/java/com/imyeyu/spring/TimiSpring.java
Normal file
360
src/main/java/com/imyeyu/spring/TimiSpring.java
Normal file
@@ -0,0 +1,360 @@
|
||||
package com.imyeyu.spring;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.Language;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* TimiSpring
|
||||
*
|
||||
* <p>如果使用本依赖相关组件,务必让 SpringBoot 扫描本依赖的包,在 SpringApplication 上加上注解
|
||||
* <pre>
|
||||
* @ComponentScan({"自己扫描的包", "com.imyeyu.spring"})
|
||||
* </pre>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2021-11-20 17:16
|
||||
*/
|
||||
public class TimiSpring {
|
||||
|
||||
/** 版本号 */
|
||||
public static final String VERSION = "0.0.1";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TimiSpring.class);
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
/**
|
||||
* 回调数据
|
||||
*
|
||||
* @param response 返回
|
||||
* @param resp 返回结果
|
||||
*/
|
||||
public static void render(HttpServletResponse response, TimiResponse<?> resp) {
|
||||
try {
|
||||
HttpSession session = getSession();
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
OutputStream out = response.getOutputStream();
|
||||
out.write(GSON.toJson(resp).getBytes(StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
log.error("TimiSpring.render Error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调错误
|
||||
*
|
||||
* @param response 返回
|
||||
* @param code 代码
|
||||
* @param msgKey 消息映射键
|
||||
*/
|
||||
public static void renderError(HttpServletResponse response, TimiCode code, String msgKey) {
|
||||
try {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
OutputStream out = response.getOutputStream();
|
||||
out.write(GSON.toJson(code.toResponse().msg(msgKey)).getBytes(StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
log.error("TimiSpring.renderError error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Servlet 请求属性
|
||||
*
|
||||
* @return Servlet 请求属性
|
||||
* @throws TimiException 请求异常
|
||||
*/
|
||||
public static ServletRequestAttributes getServletRequestAttributes() {
|
||||
if (RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes sra) {
|
||||
return sra;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR_NPE_VARIABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HttpServlet 请求
|
||||
*
|
||||
* @return HttpServlet 请求
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
return getServletRequestAttributes().getRequest();
|
||||
}
|
||||
|
||||
public static String getDomain() {
|
||||
return getRequest().getServerName();
|
||||
}
|
||||
|
||||
public static String getFullDomain() {
|
||||
HttpServletRequest req = getRequest();
|
||||
String port = req.getServerPort() == 80 || req.getServerPort() == 443 ? "" : ":" + req.getServerPort();
|
||||
return "%s://%s%s".formatted(req.getScheme(), getDomain(), port);
|
||||
}
|
||||
|
||||
public static String getURL() {
|
||||
return getRequest().getRequestURL().toString();
|
||||
}
|
||||
|
||||
public static String getURI() {
|
||||
return getRequest().getRequestURI();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HttpServlet 回调
|
||||
*
|
||||
* @return HttpServlet 回调
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
return getServletRequestAttributes().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Http 会话
|
||||
*
|
||||
* @return Http 会话
|
||||
*/
|
||||
public static HttpSession getSession() {
|
||||
return getRequest().getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求地理区域
|
||||
*
|
||||
* @return 地区
|
||||
*/
|
||||
public static Locale getLocale() {
|
||||
return getRequest().getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求头属性
|
||||
*
|
||||
* @param key 属性键
|
||||
* @return 属性值
|
||||
*/
|
||||
public static String getHeader(String key) {
|
||||
return getRequest().getHeader(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getSessionAttr(String key) {
|
||||
return getSession().getAttribute(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static boolean hasSessionAttr(String key) {
|
||||
return getSessionAttr(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话数据(字符串)
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static String getSessionAttrAsString(String key) {
|
||||
Object sessionAttr = getSessionAttr(key);
|
||||
if (sessionAttr == null) {
|
||||
return null;
|
||||
}
|
||||
return sessionAttr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param clazz 值类型
|
||||
* @param <T> 值类型
|
||||
* @return 值
|
||||
*/
|
||||
public static <T> T getSessionAttr(String key, Class<T> clazz) {
|
||||
return clazz.cast(getSessionAttr(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置会话数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param t 值
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> void setSessionAttr(String key, T t) {
|
||||
getSession().setAttribute(key, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除会话数据
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static void removeSessionAttr(String key) {
|
||||
getSession().removeAttribute(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getRequestAttr(String key) {
|
||||
return getRequest().getAttribute(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求数据
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static boolean hasRequestAttr(String key) {
|
||||
return getRequestAttr(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求数据(字符串)
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static String getRequestAttrAsString(String key) {
|
||||
Object reqAttr = getRequestAttr(key);
|
||||
if (reqAttr == null) {
|
||||
return null;
|
||||
}
|
||||
return reqAttr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param clazz 值类型
|
||||
* @param <T> 值类型
|
||||
* @return 值
|
||||
*/
|
||||
public static <T> T getRequestAttr(String key, Class<T> clazz) {
|
||||
return clazz.cast(getRequestAttr(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param t 值
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> void setRequestAttr(String key, T t) {
|
||||
getRequest().setAttribute(key, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除请求数据
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static void removeRequestAttr(String key) {
|
||||
getRequest().removeAttribute(key);
|
||||
}
|
||||
|
||||
public static void addCookie(Cookie cookie) {
|
||||
getResponse().addCookie(cookie);
|
||||
}
|
||||
|
||||
public static void addCookie(String key, String value) {
|
||||
addCookie(new Cookie(key, value));
|
||||
}
|
||||
|
||||
public static Cookie getCookie(String key) {
|
||||
Cookie[] cookies = getRequest().getCookies();
|
||||
if (cookies == null) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < cookies.length; i++) {
|
||||
if (cookies[i].getName().equals(key)) {
|
||||
return cookies[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求头的令牌,键为 Token
|
||||
*
|
||||
* @return 令牌
|
||||
*/
|
||||
public static String getToken() {
|
||||
return getHeader("Token");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return 客户端地区语言
|
||||
*/
|
||||
public static Language getLanguage() {
|
||||
String name = TimiSpring.getHeader("Language");
|
||||
if (TimiJava.isEmpty(name)) {
|
||||
name = TimiSpring.getLocale().toString();
|
||||
}
|
||||
if (TimiJava.isEmpty(name)) { // use for not support
|
||||
return Language.zh_CN;
|
||||
}
|
||||
return Ref.toType(Language.class, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求 IP
|
||||
*
|
||||
* @return 请求 IP
|
||||
* @throws TimiException 服务异常
|
||||
*/
|
||||
public static String getRequestIP() {
|
||||
String ip = getHeader("x-forwarded-for");
|
||||
// nginx 转发
|
||||
if (TimiJava.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = getHeader("X-Forwarded-For");
|
||||
}
|
||||
if (TimiJava.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = getHeader("X-Real-IP");
|
||||
}
|
||||
// 默认获取
|
||||
if (TimiJava.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = getRequest().getRemoteAddr();
|
||||
}
|
||||
// 本地 IP
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
||||
}
|
||||
|
||||
public static boolean isLocalIP() {
|
||||
return getRequestIP().startsWith("127");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user