| | |
| | | import com.zy.acs.manager.manager.enums.IntegrationDirectionType; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.service.IntegrationRecordService; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.http.server.ServerHttpRequest; |
| | | import org.springframework.http.server.ServerHttpResponse; |
| | | import org.springframework.http.server.ServletServerHttpRequest; |
| | | import org.springframework.web.util.ContentCachingRequestWrapper; |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | | import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
| | | import org.springframework.web.util.ContentCachingRequestWrapper; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | @Slf4j |
| | | @ControllerAdvice |
| | |
| | | Exception failure) { |
| | | Date now = new Date(); |
| | | RequestSnapshot payload = buildSnapshot(request); |
| | | String reqContent = !Cools.isEmpty(payload.getParameters()) |
| | | ? JSON.toJSONString(payload.getParameters()) |
| | | : payload.getJson(); |
| | | |
| | | IntegrationRecord record = new IntegrationRecord( |
| | | String.valueOf(snowflakeIdWorker.nextId()).substring(3), // 编号 |
| | | context.getNamespaceType().name(), // 名称空间 |
| | | payload.getUri(), // 接口地址 |
| | | request.getHeader(HEADER_APP_KEY), // 平台密钥 |
| | | context.getNamespaceType().caller, // 调用方标识 |
| | | IntegrationDirectionType.INBOUND.value, // 方向[非空] |
| | | String.valueOf(context.getStartAt()), // 时间戳 |
| | | IpTools.gainRealIp(request), // 客户端IP |
| | | reqContent, // 请求内容 |
| | | JSON.toJSONString(responseBody), // 响应内容 |
| | | null, // 异常内容 |
| | | 0, // 结果 |
| | | (int) (System.currentTimeMillis() - context.getStartAt()), // 耗时 |
| | | StatusType.ENABLE.val, // 状态 |
| | | now, // 添加时间[非空] |
| | | now, // 修改时间[非空] |
| | | context.getHandler() // 备注 |
| | | ); |
| | | |
| | | IntegrationRecord record = new IntegrationRecord(); |
| | | record.setUuid(nextUuid()); |
| | | record.setNamespace(context.getNamespaceType().name()); |
| | | record.setUrl(payload.getUri()); |
| | | record.setAppkey(request.getHeader(HEADER_APP_KEY)); |
| | | record.setCaller(resolveCaller(request)); |
| | | record.setDirection(IntegrationDirectionType.INBOUND.value); |
| | | record.setTimestamp(String.valueOf(context.getStartAt())); |
| | | record.setClientIp(IpTools.gainRealIp(request)); |
| | | record.setRequest(safeToJson(payload)); |
| | | record.setResponse(safeToJson(responseBody)); |
| | | applyResult(record, responseBody, failure); |
| | | record.setCostMs(cost(context.getStartAt())); |
| | | record.setStatus(StatusType.ENABLE.val); |
| | | record.setCreateTime(now); |
| | | record.setUpdateTime(now); |
| | | record.setMemo(context.getHandler()); |
| | | return record; |
| | | } |
| | | |
| | |
| | | } |
| | | boolean success = code == 200; |
| | | record.setResult(success ? 1 : 0); |
| | | record.setErr(success ? null : safeToString(response.get("msg"))); |
| | | record.setErr(success ? null : String.valueOf(response.get("msg"))); |
| | | } |
| | | |
| | | private Integer parseInteger(Object codeObj) { |
| | |
| | | } |
| | | } |
| | | |
| | | private String safeToJson(Object value) { |
| | | if (value == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return JSON.toJSONString(value); |
| | | } catch (Exception e) { |
| | | log.warn("Failed to serialize value for integration log: {}", value.getClass().getName(), e); |
| | | return String.valueOf(value); |
| | | } |
| | | } |
| | | |
| | | private String resolveCaller(HttpServletRequest request) { |
| | | String caller = request.getHeader(HEADER_CALLER); |
| | | if (Cools.isEmpty(caller)) { |
| | | caller = request.getHeader(HEADER_APP_KEY); |
| | | } |
| | | return caller; |
| | | } |
| | | |
| | | private int cost(long startAt) { |
| | | long duration = System.currentTimeMillis() - startAt; |
| | | if (duration < 0) { |
| | | return 0; |
| | | } |
| | | return (int) duration; |
| | | } |
| | | |
| | | private String nextUuid() { |
| | | if (snowflakeIdWorker != null) { |
| | | return String.valueOf(snowflakeIdWorker.nextId()).substring(3); |
| | | } |
| | | return UUID.randomUUID().toString().replace("-", ""); |
| | | } |
| | | |
| | | private String safeToString(Object value) { |
| | | return value == null ? null : String.valueOf(value); |
| | | } |
| | | private RequestSnapshot buildSnapshot(HttpServletRequest request) { |
| | | Map<String, Object> params = flattenParameters(request.getParameterMap()); |
| | | String body = normalizeBody(readBody(request), request.getContentType()); |
| | | return new RequestSnapshot( |
| | | request.getMethod(), |
| | | request.getRequestURI(), |
| | | request.getQueryString(), |
| | | request.getContentType(), |
| | | params.isEmpty() ? null : params, |
| | | body |
| | | normalizeBody(readBody(request), request.getContentType()) |
| | | ); |
| | | } |
| | | |
| | |
| | | Object parsed = JSON.parse(body); |
| | | return JSON.toJSONString(parsed, false); |
| | | } catch (Exception ignore) { |
| | | // fall through to compacting whitespace |
| | | } |
| | | } |
| | | return body.replaceAll("[\\n\\r\\t]", "").trim(); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | @Data |
| | | private static class RequestSnapshot { |
| | | private final String method; |
| | | private final String uri; |
| | | private final String query; |
| | | private final String contentType; |
| | | private final Map<String, Object> parameters; |
| | | private final String body; |
| | | private final String json; |
| | | |
| | | RequestSnapshot(String method, String uri, String query, String contentType, |
| | | Map<String, Object> parameters, String body) { |
| | | Map<String, Object> parameters, String json) { |
| | | this.method = method; |
| | | this.uri = uri; |
| | | this.query = query; |
| | | this.contentType = contentType; |
| | | this.parameters = parameters; |
| | | this.body = body; |
| | | this.json = json; |
| | | } |
| | | |
| | | public String getUri() { |
| | | return uri; |
| | | } |
| | | } |
| | | } |