refactor(log/httptrace-pro): 重构请求头及响应头信息获取

This commit is contained in:
2023-12-25 20:11:33 +08:00
parent e9f01d05c9
commit 5500e5d840
7 changed files with 32 additions and 19 deletions

View File

@@ -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;
}
}
}

View File

@@ -38,7 +38,7 @@ public class LogResponse {
/**
* 响应头
*/
private Map<String, List<String>> headers;
private Map<String, String> headers;
/**
* 响应体JSON 字符串)

View File

@@ -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();
/**
* 获取请求体

View File

@@ -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();
/**
* 获取响应体

View File

@@ -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

View File

@@ -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