From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/WebUtil.java | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 314 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/WebUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/WebUtil.java new file mode 100644 index 0000000..6860848 --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/WebUtil.java @@ -0,0 +1,314 @@ +/* + * Copyright (c) 2018-2028, DreamLu All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com) + */ +package org.springblade.core.tool.utils; + +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.jackson.JsonUtil; +import org.springframework.http.MediaType; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.method.HandlerMethod; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.util.Enumeration; +import java.util.Objects; +import java.util.function.Predicate; + + +/** + * Miscellaneous utilities for web applications. + * + * @author L.cm + */ +@Slf4j +public class WebUtil extends org.springframework.web.util.WebUtils { + + public static final String USER_AGENT_HEADER = "user-agent"; + + /** + * 鍒ゆ柇鏄惁ajax璇锋眰 + * spring ajax 杩斿洖鍚湁 ResponseBody 鎴栬�� RestController娉ㄨВ + * + * @param handlerMethod HandlerMethod + * @return 鏄惁ajax璇锋眰 + */ + public static boolean isBody(HandlerMethod handlerMethod) { + ResponseBody responseBody = ClassUtil.getAnnotation(handlerMethod, ResponseBody.class); + return responseBody != null; + } + + /** + * 璇诲彇cookie + * + * @param name cookie name + * @return cookie value + */ + @Nullable + public static String getCookieVal(String name) { + HttpServletRequest request = WebUtil.getRequest(); + Assert.notNull(request, "request from RequestContextHolder is null"); + return getCookieVal(request, name); + } + + /** + * 璇诲彇cookie + * + * @param request HttpServletRequest + * @param name cookie name + * @return cookie value + */ + @Nullable + public static String getCookieVal(HttpServletRequest request, String name) { + Cookie cookie = getCookie(request, name); + return cookie != null ? cookie.getValue() : null; + } + + /** + * 娓呴櫎 鏌愪釜鎸囧畾鐨刢ookie + * + * @param response HttpServletResponse + * @param key cookie key + */ + public static void removeCookie(HttpServletResponse response, String key) { + setCookie(response, key, null, 0); + } + + /** + * 璁剧疆cookie + * + * @param response HttpServletResponse + * @param name cookie name + * @param value cookie value + * @param maxAgeInSeconds maxage + */ + public static void setCookie(HttpServletResponse response, String name, @Nullable String value, int maxAgeInSeconds) { + Cookie cookie = new Cookie(name, value); + cookie.setPath(StringPool.SLASH); + cookie.setMaxAge(maxAgeInSeconds); + cookie.setHttpOnly(true); + response.addCookie(cookie); + } + + /** + * 鑾峰彇 HttpServletRequest + * + * @return {HttpServletRequest} + */ + public static HttpServletRequest getRequest() { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + return (requestAttributes == null) ? null : ((ServletRequestAttributes) requestAttributes).getRequest(); + } + + /** + * 杩斿洖json + * + * @param response HttpServletResponse + * @param result 缁撴灉瀵硅薄 + */ + public static void renderJson(HttpServletResponse response, Object result) { + renderJson(response, result, MediaType.APPLICATION_JSON_VALUE); + } + + /** + * 杩斿洖json + * + * @param response HttpServletResponse + * @param result 缁撴灉瀵硅薄 + * @param contentType contentType + */ + public static void renderJson(HttpServletResponse response, Object result, String contentType) { + response.setCharacterEncoding("UTF-8"); + response.setContentType(contentType); + try (PrintWriter out = response.getWriter()) { + out.append(JsonUtil.toJson(result)); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + + /** + * 鑾峰彇ip + * + * @return {String} + */ + public static String getIP() { + return getIP(WebUtil.getRequest()); + } + + private static final String[] IP_HEADER_NAMES = new String[]{ + "x-forwarded-for", + "Proxy-Client-IP", + "WL-Proxy-Client-IP", + "HTTP_CLIENT_IP", + "HTTP_X_FORWARDED_FOR" + }; + + private static final Predicate<String> IP_PREDICATE = (ip) -> StringUtil.isBlank(ip) || StringPool.UNKNOWN.equalsIgnoreCase(ip); + + /** + * 鑾峰彇ip + * + * @param request HttpServletRequest + * @return {String} + */ + @Nullable + public static String getIP(@Nullable HttpServletRequest request) { + if (request == null) { + return StringPool.EMPTY; + } + String ip = null; + for (String ipHeader : IP_HEADER_NAMES) { + ip = request.getHeader(ipHeader); + if (!IP_PREDICATE.test(ip)) { + break; + } + } + if (IP_PREDICATE.test(ip)) { + ip = request.getRemoteAddr(); + } + return StringUtil.isBlank(ip) ? null : StringUtil.splitTrim(ip, StringPool.COMMA)[0]; + } + + /** + * 鑾峰彇璇锋眰澶寸殑鍊� + * + * @param name 璇锋眰澶村悕绉� + * @return 璇锋眰澶� + */ + public static String getHeader(String name) { + HttpServletRequest request = getRequest(); + return Objects.requireNonNull(request).getHeader(name); + } + + /** + * 鑾峰彇璇锋眰澶寸殑鍊� + * + * @param name 璇锋眰澶村悕绉� + * @return 璇锋眰澶� + */ + public static Enumeration<String> getHeaders(String name) { + HttpServletRequest request = getRequest(); + return Objects.requireNonNull(request).getHeaders(name); + } + + /** + * 鑾峰彇鎵�鏈夌殑璇锋眰澶� + * + * @return 璇锋眰澶撮泦鍚� + */ + public static Enumeration<String> getHeaderNames() { + HttpServletRequest request = getRequest(); + return Objects.requireNonNull(request).getHeaderNames(); + } + + /** + * 鑾峰彇璇锋眰鍙傛暟 + * + * @param name 璇锋眰鍙傛暟鍚� + * @return 璇锋眰鍙傛暟 + */ + public static String getParameter(String name) { + HttpServletRequest request = getRequest(); + return Objects.requireNonNull(request).getParameter(name); + } + + /** + * 鑾峰彇 request 璇锋眰浣� + * + * @param servletInputStream servletInputStream + * @return body + */ + public static String getRequestBody(ServletInputStream servletInputStream) { + StringBuilder sb = new StringBuilder(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(servletInputStream, StandardCharsets.UTF_8)); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (servletInputStream != null) { + try { + servletInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return sb.toString(); + } + + /** + * 鑾峰彇 request 璇锋眰鍐呭 + * + * @param request request + * @return {String} + */ + public static String getRequestContent(HttpServletRequest request) { + try { + String queryString = request.getQueryString(); + if (StringUtil.isNotBlank(queryString)) { + return new String(queryString.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8).replaceAll("&", "&").replaceAll("%22", "\""); + } + String charEncoding = request.getCharacterEncoding(); + if (charEncoding == null) { + charEncoding = StringPool.UTF_8; + } + byte[] buffer = getRequestBody(request.getInputStream()).getBytes(); + String str = new String(buffer, charEncoding).trim(); + if (StringUtil.isBlank(str)) { + StringBuilder sb = new StringBuilder(); + Enumeration<String> parameterNames = request.getParameterNames(); + while (parameterNames.hasMoreElements()) { + String key = parameterNames.nextElement(); + String value = request.getParameter(key); + StringUtil.appendBuilder(sb, key, "=", value, "&"); + } + str = StringUtil.removeSuffix(sb.toString(), "&"); + } + return str.replaceAll("&", "&"); + } catch (Exception ex) { + ex.printStackTrace(); + return StringPool.EMPTY; + } + } + + +} + -- Gitblit v1.9.3