|
@@ -1,11 +1,9 @@
|
|
|
package com.xunmei.common.core.utils;
|
|
package com.xunmei.common.core.utils;
|
|
|
|
|
|
|
|
-import cn.hutool.extra.spring.SpringUtil;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.xunmei.common.core.constant.Constants;
|
|
import com.xunmei.common.core.constant.Constants;
|
|
|
import com.xunmei.common.core.domain.R;
|
|
import com.xunmei.common.core.domain.R;
|
|
|
import com.xunmei.common.core.text.Convert;
|
|
import com.xunmei.common.core.text.Convert;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -25,54 +23,63 @@ import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLDecoder;
|
|
import java.net.URLDecoder;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
-import java.util.*;
|
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.Enumeration;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 客户端工具类
|
|
* 客户端工具类
|
|
|
*
|
|
*
|
|
|
* @author xunmei
|
|
* @author xunmei
|
|
|
*/
|
|
*/
|
|
|
-@Slf4j
|
|
|
|
|
-public class ServletUtils {
|
|
|
|
|
|
|
+public class ServletUtils
|
|
|
|
|
+{
|
|
|
/**
|
|
/**
|
|
|
* 获取String参数
|
|
* 获取String参数
|
|
|
*/
|
|
*/
|
|
|
- public static String getParameter(String name) {
|
|
|
|
|
|
|
+ public static String getParameter(String name)
|
|
|
|
|
+ {
|
|
|
return getRequest().getParameter(name);
|
|
return getRequest().getParameter(name);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取String参数
|
|
* 获取String参数
|
|
|
*/
|
|
*/
|
|
|
- public static String getParameter(String name, String defaultValue) {
|
|
|
|
|
|
|
+ public static String getParameter(String name, String defaultValue)
|
|
|
|
|
+ {
|
|
|
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
|
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取Integer参数
|
|
* 获取Integer参数
|
|
|
*/
|
|
*/
|
|
|
- public static Integer getParameterToInt(String name) {
|
|
|
|
|
|
|
+ public static Integer getParameterToInt(String name)
|
|
|
|
|
+ {
|
|
|
return Convert.toInt(getRequest().getParameter(name));
|
|
return Convert.toInt(getRequest().getParameter(name));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取Integer参数
|
|
* 获取Integer参数
|
|
|
*/
|
|
*/
|
|
|
- public static Integer getParameterToInt(String name, Integer defaultValue) {
|
|
|
|
|
|
|
+ public static Integer getParameterToInt(String name, Integer defaultValue)
|
|
|
|
|
+ {
|
|
|
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
|
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取Boolean参数
|
|
* 获取Boolean参数
|
|
|
*/
|
|
*/
|
|
|
- public static Boolean getParameterToBool(String name) {
|
|
|
|
|
|
|
+ public static Boolean getParameterToBool(String name)
|
|
|
|
|
+ {
|
|
|
return Convert.toBool(getRequest().getParameter(name));
|
|
return Convert.toBool(getRequest().getParameter(name));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取Boolean参数
|
|
* 获取Boolean参数
|
|
|
*/
|
|
*/
|
|
|
- public static Boolean getParameterToBool(String name, Boolean defaultValue) {
|
|
|
|
|
|
|
+ public static Boolean getParameterToBool(String name, Boolean defaultValue)
|
|
|
|
|
+ {
|
|
|
return Convert.toBool(getRequest().getParameter(name), defaultValue);
|
|
return Convert.toBool(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -82,7 +89,8 @@ public class ServletUtils {
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
* @return Map
|
|
* @return Map
|
|
|
*/
|
|
*/
|
|
|
- public static Map<String, String[]> getParams(ServletRequest request) {
|
|
|
|
|
|
|
+ public static Map<String, String[]> getParams(ServletRequest request)
|
|
|
|
|
+ {
|
|
|
final Map<String, String[]> map = request.getParameterMap();
|
|
final Map<String, String[]> map = request.getParameterMap();
|
|
|
return Collections.unmodifiableMap(map);
|
|
return Collections.unmodifiableMap(map);
|
|
|
}
|
|
}
|
|
@@ -93,9 +101,11 @@ public class ServletUtils {
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
* @return Map
|
|
* @return Map
|
|
|
*/
|
|
*/
|
|
|
- public static Map<String, String> getParamMap(ServletRequest request) {
|
|
|
|
|
|
|
+ public static Map<String, String> getParamMap(ServletRequest request)
|
|
|
|
|
+ {
|
|
|
Map<String, String> params = new HashMap<>();
|
|
Map<String, String> params = new HashMap<>();
|
|
|
- for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) {
|
|
|
|
|
|
|
+ for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
|
|
|
|
|
+ {
|
|
|
params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
|
|
params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
|
|
|
}
|
|
}
|
|
|
return params;
|
|
return params;
|
|
@@ -104,15 +114,14 @@ public class ServletUtils {
|
|
|
/**
|
|
/**
|
|
|
* 获取request
|
|
* 获取request
|
|
|
*/
|
|
*/
|
|
|
- public static HttpServletRequest getRequest() {
|
|
|
|
|
- try {
|
|
|
|
|
- if (getRequestAttributes() != null) {
|
|
|
|
|
- return getRequestAttributes().getRequest();
|
|
|
|
|
- }
|
|
|
|
|
- HttpServletRequest bean = SpringUtil.getBean(HttpServletRequest.class);
|
|
|
|
|
- return bean;
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("获取request异常", e);
|
|
|
|
|
|
|
+ public static HttpServletRequest getRequest()
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ return getRequestAttributes().getRequest();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -120,14 +129,14 @@ public class ServletUtils {
|
|
|
/**
|
|
/**
|
|
|
* 获取response
|
|
* 获取response
|
|
|
*/
|
|
*/
|
|
|
- public static HttpServletResponse getResponse() {
|
|
|
|
|
- try {
|
|
|
|
|
- if (getRequestAttributes() != null) {
|
|
|
|
|
- return getRequestAttributes().getResponse();
|
|
|
|
|
- }
|
|
|
|
|
- return SpringUtil.getBean(HttpServletResponse.class);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("获取response异常", e);
|
|
|
|
|
|
|
+ public static HttpServletResponse getResponse()
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ return getRequestAttributes().getResponse();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -135,32 +144,42 @@ public class ServletUtils {
|
|
|
/**
|
|
/**
|
|
|
* 获取session
|
|
* 获取session
|
|
|
*/
|
|
*/
|
|
|
- public static HttpSession getSession() {
|
|
|
|
|
|
|
+ public static HttpSession getSession()
|
|
|
|
|
+ {
|
|
|
return getRequest().getSession();
|
|
return getRequest().getSession();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static ServletRequestAttributes getRequestAttributes() {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ public static ServletRequestAttributes getRequestAttributes()
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
|
|
return (ServletRequestAttributes) attributes;
|
|
return (ServletRequestAttributes) attributes;
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String getHeader(HttpServletRequest request, String name) {
|
|
|
|
|
|
|
+ public static String getHeader(HttpServletRequest request, String name)
|
|
|
|
|
+ {
|
|
|
String value = request.getHeader(name);
|
|
String value = request.getHeader(name);
|
|
|
- if (StringUtils.isEmpty(value)) {
|
|
|
|
|
|
|
+ if (StringUtils.isEmpty(value))
|
|
|
|
|
+ {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
return urlDecode(value);
|
|
return urlDecode(value);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static Map<String, String> getHeaders(HttpServletRequest request) {
|
|
|
|
|
|
|
+ public static Map<String, String> getHeaders(HttpServletRequest request)
|
|
|
|
|
+ {
|
|
|
Map<String, String> map = new LinkedCaseInsensitiveMap<>();
|
|
Map<String, String> map = new LinkedCaseInsensitiveMap<>();
|
|
|
Enumeration<String> enumeration = request.getHeaderNames();
|
|
Enumeration<String> enumeration = request.getHeaderNames();
|
|
|
- if (enumeration != null) {
|
|
|
|
|
- while (enumeration.hasMoreElements()) {
|
|
|
|
|
|
|
+ if (enumeration != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (enumeration.hasMoreElements())
|
|
|
|
|
+ {
|
|
|
String key = enumeration.nextElement();
|
|
String key = enumeration.nextElement();
|
|
|
String value = request.getHeader(key);
|
|
String value = request.getHeader(key);
|
|
|
map.put(key, value);
|
|
map.put(key, value);
|
|
@@ -173,15 +192,19 @@ public class ServletUtils {
|
|
|
* 将字符串渲染到客户端
|
|
* 将字符串渲染到客户端
|
|
|
*
|
|
*
|
|
|
* @param response 渲染对象
|
|
* @param response 渲染对象
|
|
|
- * @param string 待渲染的字符串
|
|
|
|
|
|
|
+ * @param string 待渲染的字符串
|
|
|
*/
|
|
*/
|
|
|
- public static void renderString(HttpServletResponse response, String string) {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ public static void renderString(HttpServletResponse response, String string)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
response.setStatus(200);
|
|
response.setStatus(200);
|
|
|
response.setContentType("application/json");
|
|
response.setContentType("application/json");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
response.setCharacterEncoding("utf-8");
|
|
|
response.getWriter().print(string);
|
|
response.getWriter().print(string);
|
|
|
- } catch (IOException e) {
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e)
|
|
|
|
|
+ {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -191,19 +214,23 @@ public class ServletUtils {
|
|
|
*
|
|
*
|
|
|
* @param request
|
|
* @param request
|
|
|
*/
|
|
*/
|
|
|
- public static boolean isAjaxRequest(HttpServletRequest request) {
|
|
|
|
|
|
|
+ public static boolean isAjaxRequest(HttpServletRequest request)
|
|
|
|
|
+ {
|
|
|
String accept = request.getHeader("accept");
|
|
String accept = request.getHeader("accept");
|
|
|
- if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
|
|
+ if (accept != null && accept.contains("application/json"))
|
|
|
|
|
+ {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
|
|
- if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
|
|
|
|
|
|
|
+ if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
|
|
|
|
|
+ {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String uri = request.getRequestURI();
|
|
String uri = request.getRequestURI();
|
|
|
- if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
|
|
|
|
|
|
|
+ if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
|
|
|
|
|
+ {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -217,10 +244,14 @@ public class ServletUtils {
|
|
|
* @param str 内容
|
|
* @param str 内容
|
|
|
* @return 编码后的内容
|
|
* @return 编码后的内容
|
|
|
*/
|
|
*/
|
|
|
- public static String urlEncode(String str) {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ public static String urlEncode(String str)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
return URLEncoder.encode(str, Constants.UTF8);
|
|
return URLEncoder.encode(str, Constants.UTF8);
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (UnsupportedEncodingException e)
|
|
|
|
|
+ {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -231,10 +262,14 @@ public class ServletUtils {
|
|
|
* @param str 内容
|
|
* @param str 内容
|
|
|
* @return 解码后的内容
|
|
* @return 解码后的内容
|
|
|
*/
|
|
*/
|
|
|
- public static String urlDecode(String str) {
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ public static String urlDecode(String str)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
return URLDecoder.decode(str, Constants.UTF8);
|
|
return URLDecoder.decode(str, Constants.UTF8);
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (UnsupportedEncodingException e)
|
|
|
|
|
+ {
|
|
|
return StringUtils.EMPTY;
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -243,10 +278,11 @@ public class ServletUtils {
|
|
|
* 设置webflux模型响应
|
|
* 设置webflux模型响应
|
|
|
*
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
* @param response ServerHttpResponse
|
|
|
- * @param value 响应内容
|
|
|
|
|
|
|
+ * @param value 响应内容
|
|
|
* @return Mono<Void>
|
|
* @return Mono<Void>
|
|
|
*/
|
|
*/
|
|
|
- public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value) {
|
|
|
|
|
|
|
+ public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value)
|
|
|
|
|
+ {
|
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -254,11 +290,12 @@ public class ServletUtils {
|
|
|
* 设置webflux模型响应
|
|
* 设置webflux模型响应
|
|
|
*
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
* @param response ServerHttpResponse
|
|
|
- * @param code 响应状态码
|
|
|
|
|
- * @param value 响应内容
|
|
|
|
|
|
|
+ * @param code 响应状态码
|
|
|
|
|
+ * @param value 响应内容
|
|
|
* @return Mono<Void>
|
|
* @return Mono<Void>
|
|
|
*/
|
|
*/
|
|
|
- public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
|
|
|
|
|
|
|
+ public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code)
|
|
|
|
|
+ {
|
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -266,26 +303,28 @@ public class ServletUtils {
|
|
|
* 设置webflux模型响应
|
|
* 设置webflux模型响应
|
|
|
*
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
* @param response ServerHttpResponse
|
|
|
- * @param status http状态码
|
|
|
|
|
- * @param code 响应状态码
|
|
|
|
|
- * @param value 响应内容
|
|
|
|
|
|
|
+ * @param status http状态码
|
|
|
|
|
+ * @param code 响应状态码
|
|
|
|
|
+ * @param value 响应内容
|
|
|
* @return Mono<Void>
|
|
* @return Mono<Void>
|
|
|
*/
|
|
*/
|
|
|
- public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
|
|
|
|
|
|
|
+ public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code)
|
|
|
|
|
+ {
|
|
|
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
|
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置webflux模型响应
|
|
* 设置webflux模型响应
|
|
|
*
|
|
*
|
|
|
- * @param response ServerHttpResponse
|
|
|
|
|
|
|
+ * @param response ServerHttpResponse
|
|
|
* @param contentType content-type
|
|
* @param contentType content-type
|
|
|
- * @param status http状态码
|
|
|
|
|
- * @param code 响应状态码
|
|
|
|
|
- * @param value 响应内容
|
|
|
|
|
|
|
+ * @param status http状态码
|
|
|
|
|
+ * @param code 响应状态码
|
|
|
|
|
+ * @param value 响应内容
|
|
|
* @return Mono<Void>
|
|
* @return Mono<Void>
|
|
|
*/
|
|
*/
|
|
|
- public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
|
|
|
|
|
|
+ public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code)
|
|
|
|
|
+ {
|
|
|
response.setStatusCode(status);
|
|
response.setStatusCode(status);
|
|
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
|
|
R<?> result = R.fail(code, value.toString());
|
|
R<?> result = R.fail(code, value.toString());
|