|
|
@@ -1,5 +1,6 @@
|
|
|
package com.xunmei.common.core.utils;
|
|
|
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.xunmei.common.core.constant.Constants;
|
|
|
import com.xunmei.common.core.domain.R;
|
|
|
@@ -23,63 +24,53 @@ import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Enumeration;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 客户端工具类
|
|
|
- *
|
|
|
+ *
|
|
|
* @author xunmei
|
|
|
*/
|
|
|
-public class ServletUtils
|
|
|
-{
|
|
|
+public class ServletUtils {
|
|
|
/**
|
|
|
* 获取String参数
|
|
|
*/
|
|
|
- public static String getParameter(String name)
|
|
|
- {
|
|
|
+ public static String getParameter(String name) {
|
|
|
return getRequest().getParameter(name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取String参数
|
|
|
*/
|
|
|
- public static String getParameter(String name, String defaultValue)
|
|
|
- {
|
|
|
+ public static String getParameter(String name, String defaultValue) {
|
|
|
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取Integer参数
|
|
|
*/
|
|
|
- public static Integer getParameterToInt(String name)
|
|
|
- {
|
|
|
+ public static Integer getParameterToInt(String name) {
|
|
|
return Convert.toInt(getRequest().getParameter(name));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取Integer参数
|
|
|
*/
|
|
|
- public static Integer getParameterToInt(String name, Integer defaultValue)
|
|
|
- {
|
|
|
+ public static Integer getParameterToInt(String name, Integer defaultValue) {
|
|
|
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取Boolean参数
|
|
|
*/
|
|
|
- public static Boolean getParameterToBool(String name)
|
|
|
- {
|
|
|
+ public static Boolean getParameterToBool(String name) {
|
|
|
return Convert.toBool(getRequest().getParameter(name));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取Boolean参数
|
|
|
*/
|
|
|
- public static Boolean getParameterToBool(String name, Boolean defaultValue)
|
|
|
- {
|
|
|
+ public static Boolean getParameterToBool(String name, Boolean defaultValue) {
|
|
|
return Convert.toBool(getRequest().getParameter(name), defaultValue);
|
|
|
}
|
|
|
|
|
|
@@ -89,8 +80,7 @@ public class ServletUtils
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
* @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();
|
|
|
return Collections.unmodifiableMap(map);
|
|
|
}
|
|
|
@@ -101,11 +91,9 @@ public class ServletUtils
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
* @return Map
|
|
|
*/
|
|
|
- public static Map<String, String> getParamMap(ServletRequest request)
|
|
|
- {
|
|
|
+ public static Map<String, String> getParamMap(ServletRequest request) {
|
|
|
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(), ","));
|
|
|
}
|
|
|
return params;
|
|
|
@@ -114,14 +102,10 @@ public class ServletUtils
|
|
|
/**
|
|
|
* 获取request
|
|
|
*/
|
|
|
- public static HttpServletRequest getRequest()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- return getRequestAttributes().getRequest();
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ public static HttpServletRequest getRequest() {
|
|
|
+ try {
|
|
|
+ return Optional.ofNullable(ServletUtils.getRequest()).orElseGet(() -> SpringUtil.getBean(HttpServletRequest.class));
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -129,14 +113,10 @@ public class ServletUtils
|
|
|
/**
|
|
|
* 获取response
|
|
|
*/
|
|
|
- public static HttpServletResponse getResponse()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public static HttpServletResponse getResponse() {
|
|
|
+ try {
|
|
|
return getRequestAttributes().getResponse();
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -144,42 +124,32 @@ public class ServletUtils
|
|
|
/**
|
|
|
* 获取session
|
|
|
*/
|
|
|
- public static HttpSession getSession()
|
|
|
- {
|
|
|
+ public static HttpSession getSession() {
|
|
|
return getRequest().getSession();
|
|
|
}
|
|
|
|
|
|
- public static ServletRequestAttributes getRequestAttributes()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public static ServletRequestAttributes getRequestAttributes() {
|
|
|
+ try {
|
|
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
|
|
return (ServletRequestAttributes) attributes;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String getHeader(HttpServletRequest request, String name)
|
|
|
- {
|
|
|
+ public static String getHeader(HttpServletRequest request, String name) {
|
|
|
String value = request.getHeader(name);
|
|
|
- if (StringUtils.isEmpty(value))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
|
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<>();
|
|
|
Enumeration<String> enumeration = request.getHeaderNames();
|
|
|
- if (enumeration != null)
|
|
|
- {
|
|
|
- while (enumeration.hasMoreElements())
|
|
|
- {
|
|
|
+ if (enumeration != null) {
|
|
|
+ while (enumeration.hasMoreElements()) {
|
|
|
String key = enumeration.nextElement();
|
|
|
String value = request.getHeader(key);
|
|
|
map.put(key, value);
|
|
|
@@ -190,47 +160,39 @@ public class ServletUtils
|
|
|
|
|
|
/**
|
|
|
* 将字符串渲染到客户端
|
|
|
- *
|
|
|
+ *
|
|
|
* @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.setContentType("application/json");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
response.getWriter().print(string);
|
|
|
- }
|
|
|
- catch (IOException e)
|
|
|
- {
|
|
|
+ } catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 是否是Ajax异步请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param request
|
|
|
*/
|
|
|
- public static boolean isAjaxRequest(HttpServletRequest request)
|
|
|
- {
|
|
|
+ public static boolean isAjaxRequest(HttpServletRequest request) {
|
|
|
String accept = request.getHeader("accept");
|
|
|
- if (accept != null && accept.contains("application/json"))
|
|
|
- {
|
|
|
+ if (accept != null && accept.contains("application/json")) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
|
|
- if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
|
|
|
- {
|
|
|
+ if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
String uri = request.getRequestURI();
|
|
|
- if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
|
|
|
- {
|
|
|
+ if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -240,36 +202,28 @@ public class ServletUtils
|
|
|
|
|
|
/**
|
|
|
* 内容编码
|
|
|
- *
|
|
|
+ *
|
|
|
* @param str 内容
|
|
|
* @return 编码后的内容
|
|
|
*/
|
|
|
- public static String urlEncode(String str)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public static String urlEncode(String str) {
|
|
|
+ try {
|
|
|
return URLEncoder.encode(str, Constants.UTF8);
|
|
|
- }
|
|
|
- catch (UnsupportedEncodingException e)
|
|
|
- {
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 内容解码
|
|
|
- *
|
|
|
+ *
|
|
|
* @param str 内容
|
|
|
* @return 解码后的内容
|
|
|
*/
|
|
|
- public static String urlDecode(String str)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public static String urlDecode(String str) {
|
|
|
+ try {
|
|
|
return URLDecoder.decode(str, Constants.UTF8);
|
|
|
- }
|
|
|
- catch (UnsupportedEncodingException e)
|
|
|
- {
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
|
}
|
|
|
@@ -278,11 +232,10 @@ public class ServletUtils
|
|
|
* 设置webflux模型响应
|
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
|
- * @param value 响应内容
|
|
|
+ * @param value 响应内容
|
|
|
* @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);
|
|
|
}
|
|
|
|
|
|
@@ -290,12 +243,11 @@ public class ServletUtils
|
|
|
* 设置webflux模型响应
|
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
|
- * @param code 响应状态码
|
|
|
- * @param value 响应内容
|
|
|
+ * @param code 响应状态码
|
|
|
+ * @param value 响应内容
|
|
|
* @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);
|
|
|
}
|
|
|
|
|
|
@@ -303,28 +255,26 @@ public class ServletUtils
|
|
|
* 设置webflux模型响应
|
|
|
*
|
|
|
* @param response ServerHttpResponse
|
|
|
- * @param status http状态码
|
|
|
- * @param code 响应状态码
|
|
|
- * @param value 响应内容
|
|
|
+ * @param status http状态码
|
|
|
+ * @param code 响应状态码
|
|
|
+ * @param value 响应内容
|
|
|
* @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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置webflux模型响应
|
|
|
*
|
|
|
- * @param response ServerHttpResponse
|
|
|
+ * @param response ServerHttpResponse
|
|
|
* @param contentType content-type
|
|
|
- * @param status http状态码
|
|
|
- * @param code 响应状态码
|
|
|
- * @param value 响应内容
|
|
|
+ * @param status http状态码
|
|
|
+ * @param code 响应状态码
|
|
|
+ * @param value 响应内容
|
|
|
* @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.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
|
|
R<?> result = R.fail(code, value.toString());
|