Skip to content

Commit

Permalink
perf: 文件下载不保存ResponseBody
Browse files Browse the repository at this point in the history
  • Loading branch information
1962247851 committed Jun 17, 2024
1 parent 84e6757 commit 4f0da23
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ public interface PathConstants {
* TODO 内部接口、免登、登录、免登或登录
*/
List<String> NO_LOGIN_UPDATE_PATHS = Arrays.asList(IM_MIMC_CALLBACK);

/**
* 不需要保存请求体的地址
*/
List<String> DO_NOT_SET_REQUEST_BODY_PATHS = Arrays.asList(UPMS_FILE_UPLOAD);
/**
* 不需要保存响应体的地址
*/
List<String> DO_NOT_SET_RESPONSE_BODY_PATHS = Arrays.asList(UPMS_FILE_DOWNLOAD);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import tech.ordinaryroad.commons.core.base.result.Result;
import tech.ordinaryroad.commons.core.constant.PathConstants;
import tech.ordinaryroad.commons.log.RequestWrapper;
import tech.ordinaryroad.commons.log.ResponseWrapper;
import tech.ordinaryroad.commons.log.entity.OperationLogDO;
Expand Down Expand Up @@ -84,8 +85,18 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
operationLogDO.setQueryParams(JSON.toJSONString(ServletUtils.getQueryParamsMap(request)));

if (request instanceof RequestWrapper) {
// 长度最大一百万
operationLogDO.setRequest(StrUtil.subWithLength(((RequestWrapper) request).getBody(), 0, MAX_BODY_LENGTH));
// 排除不需要记录请求体的接口
boolean skipSetRequestBody = false;
for (String path : PathConstants.DO_NOT_SET_REQUEST_BODY_PATHS) {
if (request.getRequestURI().startsWith(path)) {
skipSetRequestBody = true;
break;
}
}
if (!skipSetRequestBody) {
// 长度最大一百万
operationLogDO.setRequest(StrUtil.subWithLength(((RequestWrapper) request).getBody(), 0, MAX_BODY_LENGTH));
}
}

if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -121,14 +132,24 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp

Result<?> result = null;
if (response instanceof ResponseWrapper) {
String responseString = new String(((ResponseWrapper) response).toByteArray(), StandardCharsets.UTF_8);
try {
result = JSON.parseObject(responseString, Result.class);
} catch (Exception e) {
// ignore
// 排除不需要记录响应体的接口
boolean skipSetResponseBody = false;
for (String path : PathConstants.DO_NOT_SET_RESPONSE_BODY_PATHS) {
if (request.getRequestURI().startsWith(path)) {
skipSetResponseBody = true;
break;
}
}
if (!skipSetResponseBody) {
String responseString = new String(((ResponseWrapper) response).toByteArray(), StandardCharsets.UTF_8);
try {
result = JSON.parseObject(responseString, Result.class);
} catch (Exception e) {
// ignore
}
// 长度最大一百万
operationLogDO.setResponse(StrUtil.subWithLength(responseString, 0, MAX_BODY_LENGTH));
}
// 长度最大一百万
operationLogDO.setResponse(StrUtil.subWithLength(responseString, 0, MAX_BODY_LENGTH));
}
Optional.ofNullable(operationLogInterceptorService.getType(request, response, result)).ifPresent(operationLogDO::setType);
Optional.ofNullable(operationLogInterceptorService.getStatus(request, response, result)).ifPresent(operationLogDO::setStatus);
Expand Down

0 comments on commit 4f0da23

Please sign in to comment.