ServletUtil.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package jnpf.util;
  2. import cn.hutool.http.useragent.UserAgent;
  3. import cn.hutool.http.useragent.UserAgentUtil;
  4. import jnpf.constant.GlobalConst;
  5. import org.springframework.util.AntPathMatcher;
  6. import org.springframework.web.context.request.RequestAttributes;
  7. import org.springframework.web.context.request.RequestContextHolder;
  8. import org.springframework.web.context.request.ServletRequestAttributes;
  9. import jakarta.servlet.http.HttpServletRequest;
  10. import jakarta.servlet.http.HttpServletResponse;
  11. import jakarta.servlet.http.HttpSession;
  12. import java.io.IOException;
  13. import java.util.Collections;
  14. import java.util.Enumeration;
  15. import java.util.LinkedHashMap;
  16. import java.util.Map;
  17. /**
  18. * 客户端工具类
  19. *
  20. * @author jnpf
  21. */
  22. public class ServletUtil {
  23. private static AntPathMatcher matcher = new AntPathMatcher();
  24. /**
  25. * 获取request
  26. */
  27. public static HttpServletRequest getRequest() {
  28. try {
  29. return getRequestAttributes().getRequest();
  30. } catch (Exception e) {
  31. return null;
  32. }
  33. }
  34. public static String getHeader(String name) {
  35. if (getRequest() != null) {
  36. return getRequest().getHeader(name);
  37. }
  38. return null;
  39. }
  40. /**
  41. * 判断是否是手机端登陆
  42. */
  43. public static boolean getIsMobileDevice() {
  44. return isMobileDevice(ServletUtil.getUserAgent());
  45. }
  46. /**
  47. * 判断是否是手机端登陆
  48. */
  49. public static boolean getIsMobileDevice(String userAgent) {
  50. return isMobileDevice(userAgent);
  51. }
  52. /**
  53. * 获取User-Agent
  54. */
  55. public static String getUserAgent() {
  56. return ServletUtil.getHeader("User-Agent");
  57. }
  58. /**
  59. * 判断是否是移动设备
  60. *
  61. * @param requestHeader
  62. * @return
  63. */
  64. public static boolean isMobileDevice(String requestHeader) {
  65. UserAgent userAgent = UserAgentUtil.parse(requestHeader);
  66. if (userAgent == null) {
  67. return false;
  68. }
  69. return userAgent.isMobile();
  70. }
  71. /**
  72. * 获取ServletPath
  73. */
  74. public static String getServletPath() {
  75. return ServletUtil.getRequest().getServletPath();
  76. }
  77. /**
  78. * 获取response
  79. */
  80. public static HttpServletResponse getResponse() {
  81. try {
  82. return getRequestAttributes().getResponse();
  83. } catch (Exception e) {
  84. return null;
  85. }
  86. }
  87. /**
  88. * 获取session
  89. */
  90. public static HttpSession getSession() {
  91. return getRequest().getSession();
  92. }
  93. public static ServletRequestAttributes getRequestAttributes() {
  94. try {
  95. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  96. return (ServletRequestAttributes) attributes;
  97. } catch (Exception e) {
  98. return null;
  99. }
  100. }
  101. public static Map<String, String> getHeaders(HttpServletRequest request) {
  102. Map<String, String> map = new LinkedHashMap<>();
  103. Enumeration<String> enumeration = request.getHeaderNames();
  104. if (enumeration != null) {
  105. while (enumeration.hasMoreElements()) {
  106. String key = enumeration.nextElement();
  107. String value = request.getHeader(key);
  108. map.put(key, value);
  109. }
  110. }
  111. return map;
  112. }
  113. public static Map<String, Object> getParams(HttpServletRequest request) {
  114. Map<String, Object> parameters = new LinkedHashMap<>();
  115. Enumeration<String> parameterNames = request.getParameterNames();
  116. if (parameterNames != null) {
  117. while (parameterNames.hasMoreElements()) {
  118. String key = parameterNames.nextElement();
  119. if (key == null) {
  120. continue;
  121. }
  122. parameters.put(key,request.getParameter(key));
  123. }
  124. }
  125. return parameters;
  126. }
  127. /**
  128. * 将字符串渲染到客户端
  129. *
  130. * @param response 渲染对象
  131. * @param string 待渲染的字符串
  132. * @return null
  133. */
  134. public static String renderString(HttpServletResponse response, String string) {
  135. try {
  136. response.setStatus(200);
  137. response.setContentType("application/json");
  138. response.setCharacterEncoding("utf-8");
  139. response.getWriter().print(string);
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. return null;
  144. }
  145. /**
  146. * 是否是Ajax异步请求
  147. *
  148. * @param request
  149. */
  150. public static boolean isAjaxRequest(HttpServletRequest request) {
  151. String accept = request.getHeader("accept");
  152. if (accept != null && accept.indexOf("application/json") != -1) {
  153. return true;
  154. }
  155. String xRequestedWith = request.getHeader("X-Requested-With");
  156. if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
  157. return true;
  158. }
  159. String uri = request.getRequestURI();
  160. if (StringUtil.inStringIgnoreCase(uri, ".json", ".xml")) {
  161. return true;
  162. }
  163. String ajax = request.getParameter("__ajax");
  164. if (StringUtil.inStringIgnoreCase(ajax, "json", "xml")) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * 获取URL中的path变量
  171. *
  172. * @param pattern
  173. * @param path
  174. * @return
  175. */
  176. public static Map<String, String> getPathVariables(String pattern, String path) {
  177. Map<String, String> vars = null;
  178. try {
  179. if (!StringUtil.isEmpty(path)) {
  180. vars = matcher.extractUriTemplateVariables(pattern, path);
  181. }
  182. } catch (Exception e) {
  183. }
  184. if (vars == null) {
  185. vars = Collections.EMPTY_MAP;
  186. }
  187. return vars;
  188. }
  189. /**
  190. * 获取当前访问地址中的path变量
  191. *
  192. * @param pattern
  193. * @return
  194. */
  195. public static Map<String, String> getPathVariables(String pattern) {
  196. return getPathVariables(pattern, getServletPath());
  197. }
  198. public static String getRequestHost() {
  199. HttpServletRequest request = getRequest();
  200. if (request != null) {
  201. String host = request.getHeader(GlobalConst.HEADER_HOST);
  202. if (StringUtil.isEmpty(host)) {
  203. host = request.getHeader("X-Forwarded-Host");
  204. if (StringUtil.isNotEmpty(host)) {
  205. int index = host.lastIndexOf(",");
  206. if (index != -1) {
  207. return host.substring(index);
  208. } else {
  209. return host;
  210. }
  211. } else {
  212. host = request.getHeader("Host");
  213. }
  214. }
  215. if (StringUtil.isNotEmpty(host)) {
  216. return host;
  217. }
  218. }
  219. return StringUtil.EMPTY;
  220. }
  221. }