From 5500e5d84069d19f4e703af25cdd5cf603c14d81 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 25 Dec 2023 20:11:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(log/httptrace-pro):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E8=AF=B7=E6=B1=82=E5=A4=B4=E5=8F=8A=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E5=A4=B4=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/core/util/ServletUtils.java | 18 +++++++++++++++++- .../starter/log/common/model/LogRequest.java | 12 +++++++----- .../starter/log/common/model/LogResponse.java | 2 +- .../common/model/RecordableHttpRequest.java | 3 +-- .../common/model/RecordableHttpResponse.java | 3 +-- .../handler/RecordableServletHttpRequest.java | 4 ++-- .../handler/RecordableServletHttpResponse.java | 9 +++------ 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ServletUtils.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ServletUtils.java index a70af10a..9187d72a 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ServletUtils.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ServletUtils.java @@ -16,6 +16,7 @@ package top.charles7c.continew.starter.core.util; +import cn.hutool.core.map.MapUtil; import cn.hutool.http.useragent.UserAgent; import cn.hutool.http.useragent.UserAgentUtil; import jakarta.servlet.http.HttpServletRequest; @@ -26,7 +27,7 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import top.charles7c.continew.starter.core.constant.StringConstants; -import java.util.Objects; +import java.util.*; /** * Servlet 工具类 @@ -103,6 +104,21 @@ public class ServletUtils { return userAgent.getOs().getName(); } + /** + * 获取响应所有的头(header)信息 + * + * @param response 响应对象{@link HttpServletResponse} + * @return header值 + */ + public static Map getHeaderMap(HttpServletResponse response) { + final Collection headerNames = response.getHeaderNames(); + final Map headerMap = MapUtil.newHashMap(headerNames.size(), true); + for (String name : headerNames) { + headerMap.put(name, response.getHeader(name)); + } + return headerMap; + } + private static ServletRequestAttributes getServletRequestAttributes() { return (ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()); } diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogRequest.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogRequest.java index c9f63e3c..1eb956ce 100644 --- a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogRequest.java +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogRequest.java @@ -16,6 +16,7 @@ package top.charles7c.continew.starter.log.common.model; +import cn.hutool.core.util.StrUtil; import lombok.Data; import org.springframework.http.HttpHeaders; import top.charles7c.continew.starter.core.util.ExceptionUtils; @@ -24,7 +25,6 @@ import top.charles7c.continew.starter.core.util.ServletUtils; import top.charles7c.continew.starter.log.common.enums.Include; import java.net.URI; -import java.util.List; import java.util.Map; import java.util.Set; @@ -55,7 +55,7 @@ public class LogRequest { /** * 请求头 */ - private Map> headers; + private Map headers; /** * 请求体(JSON 字符串) @@ -93,8 +93,10 @@ public class LogRequest { this.param = request.getParam(); } this.address = (includes.contains(Include.IP_ADDRESS)) ? IpUtils.getAddress(this.ip) : null; - String userAgentString = ExceptionUtils.exToNull(() -> this.headers.get(HttpHeaders.USER_AGENT).get(0)); - this.browser = (includes.contains(Include.BROWSER)) ? ServletUtils.getBrowser(userAgentString) : null; - this.os = (includes.contains(Include.OS)) ? ServletUtils.getOs(userAgentString) : null; + String userAgentString = ExceptionUtils.exToNull(() -> this.headers.get(HttpHeaders.USER_AGENT)); + if (StrUtil.isNotBlank(userAgentString)) { + this.browser = (includes.contains(Include.BROWSER)) ? ServletUtils.getBrowser(userAgentString) : null; + this.os = (includes.contains(Include.OS)) ? ServletUtils.getOs(userAgentString) : null; + } } } \ No newline at end of file diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogResponse.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogResponse.java index ed3e21ba..ac38a840 100644 --- a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogResponse.java +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/LogResponse.java @@ -38,7 +38,7 @@ public class LogResponse { /** * 响应头 */ - private Map> headers; + private Map headers; /** * 响应体(JSON 字符串) diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpRequest.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpRequest.java index f832c286..1ae1432b 100644 --- a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpRequest.java +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpRequest.java @@ -17,7 +17,6 @@ package top.charles7c.continew.starter.log.common.model; import java.net.URI; -import java.util.List; import java.util.Map; /** @@ -57,7 +56,7 @@ public interface RecordableHttpRequest { * * @return 请求头 */ - Map> getHeaders(); + Map getHeaders(); /** * 获取请求体 diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpResponse.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpResponse.java index 74092779..0506624d 100644 --- a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpResponse.java +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/model/RecordableHttpResponse.java @@ -16,7 +16,6 @@ package top.charles7c.continew.starter.log.common.model; -import java.util.List; import java.util.Map; /** @@ -41,7 +40,7 @@ public interface RecordableHttpResponse { * * @return 响应头 */ - Map> getHeaders(); + Map getHeaders(); /** * 获取响应体 diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpRequest.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpRequest.java index 1183caf5..8afd6134 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpRequest.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpRequest.java @@ -72,8 +72,8 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest } @Override - public Map> getHeaders() { - return JakartaServletUtil.getHeadersMap(request); + public Map getHeaders() { + return JakartaServletUtil.getHeaderMap(request); } @Override diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpResponse.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpResponse.java index 45dc6a99..3e960f73 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpResponse.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/RecordableServletHttpResponse.java @@ -22,6 +22,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.util.ContentCachingResponseWrapper; import org.springframework.web.util.WebUtils; import top.charles7c.continew.starter.core.constant.StringConstants; +import top.charles7c.continew.starter.core.util.ServletUtils; import top.charles7c.continew.starter.log.common.model.RecordableHttpResponse; import java.util.*; @@ -49,12 +50,8 @@ public final class RecordableServletHttpResponse implements RecordableHttpRespon } @Override - public Map> getHeaders() { - Map> headers = new LinkedHashMap<>(); - for (String name : response.getHeaderNames()) { - headers.put(name, new ArrayList<>(response.getHeaders(name))); - } - return headers; + public Map getHeaders() { + return ServletUtils.getHeaderMap(response); } @Override