mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 08:57:17 +08:00
refactor(log/httptrace-pro): 重构请求头及响应头信息获取
This commit is contained in:
@@ -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<String, String> getHeaderMap(HttpServletResponse response) {
|
||||
final Collection<String> headerNames = response.getHeaderNames();
|
||||
final Map<String, String> 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());
|
||||
}
|
||||
|
@@ -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<String, List<String>> headers;
|
||||
private Map<String, String> 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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -38,7 +38,7 @@ public class LogResponse {
|
||||
/**
|
||||
* 响应头
|
||||
*/
|
||||
private Map<String, List<String>> headers;
|
||||
private Map<String, String> headers;
|
||||
|
||||
/**
|
||||
* 响应体(JSON 字符串)
|
||||
|
@@ -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<String, List<String>> getHeaders();
|
||||
Map<String, String> getHeaders();
|
||||
|
||||
/**
|
||||
* 获取请求体
|
||||
|
@@ -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<String, List<String>> getHeaders();
|
||||
Map<String, String> getHeaders();
|
||||
|
||||
/**
|
||||
* 获取响应体
|
||||
|
@@ -72,8 +72,8 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return JakartaServletUtil.getHeadersMap(request);
|
||||
public Map<String, String> getHeaders() {
|
||||
return JakartaServletUtil.getHeaderMap(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -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<String, List<String>> getHeaders() {
|
||||
Map<String, List<String>> headers = new LinkedHashMap<>();
|
||||
for (String name : response.getHeaderNames()) {
|
||||
headers.put(name, new ArrayList<>(response.getHeaders(name)));
|
||||
}
|
||||
return headers;
|
||||
public Map<String, String> getHeaders() {
|
||||
return ServletUtils.getHeaderMap(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user