| | |
| | | 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.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | 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 javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletRequestWrapper; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | |
| | | @Slf4j |
| | |
| | | if (context == null) { |
| | | return body; |
| | | } |
| | | IntegrationRecord record = null; |
| | | try { |
| | | IntegrationRecord record = buildRecord(body, request, context); |
| | | integrationRecordService.syncRecord(record); |
| | | record = buildRecord(body, request, context, null); |
| | | } catch (Exception e) { |
| | | log.error("Failed to persist integration log for {}", context.getHandler(), e); |
| | | log.error("Failed to build integration log for {}", context.getHandler(), e); |
| | | record = buildRecord(null, request, context, e); |
| | | } finally { |
| | | if (record != null) { |
| | | try { |
| | | integrationRecordService.syncRecord(record); |
| | | } catch (Exception persistEx) { |
| | | log.error("Failed to persist integration log for {}", context.getHandler(), persistEx); |
| | | } |
| | | } |
| | | } |
| | | return body; |
| | | } |
| | | |
| | | private IntegrationRecord buildRecord(Object responseBody, |
| | | HttpServletRequest request, |
| | | IntegrationRequestContext context) { |
| | | IntegrationRequestContext context, |
| | | Exception failure) { |
| | | Date now = new Date(); |
| | | ResultView resultView = resolveResult(responseBody); |
| | | RequestSnapshot payload = buildSnapshot(request); |
| | | |
| | | |
| | | IntegrationRecord record = new IntegrationRecord(); |
| | | record.setUuid(nextUuid()); |
| | | record.setNamespace(context.getNamespace()); |
| | | record.setUrl(context.getUri()); |
| | | 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(context.getRequestSnapshot())); |
| | | record.setRequest(safeToJson(payload)); |
| | | record.setResponse(safeToJson(responseBody)); |
| | | record.setErr(resultView.getError()); |
| | | record.setResult(resultView.getResult()); |
| | | applyResult(record, responseBody, failure); |
| | | record.setCostMs(cost(context.getStartAt())); |
| | | record.setStatus(StatusType.ENABLE.val); |
| | | record.setCreateTime(now); |
| | |
| | | return record; |
| | | } |
| | | |
| | | private ResultView resolveResult(Object body) { |
| | | private void applyResult(IntegrationRecord record, Object body, Exception failure) { |
| | | if (failure != null) { |
| | | record.setResult(0); |
| | | record.setErr(failure.getMessage()); |
| | | return; |
| | | } |
| | | if (!(body instanceof R)) { |
| | | return ResultView.unknown(); |
| | | record.setResult(null); |
| | | record.setErr(null); |
| | | return; |
| | | } |
| | | R response = (R) body; |
| | | Integer code = parseInteger(response.get("code")); |
| | | if (code == null) { |
| | | return ResultView.unknown(); |
| | | record.setResult(null); |
| | | record.setErr(null); |
| | | return; |
| | | } |
| | | boolean success = code == 200; |
| | | String error = success ? null : safeToString(response.get("msg")); |
| | | return new ResultView(success ? 1 : 0, error); |
| | | record.setResult(success ? 1 : 0); |
| | | record.setErr(success ? null : safeToString(response.get("msg"))); |
| | | } |
| | | |
| | | private Integer parseInteger(Object codeObj) { |
| | |
| | | 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 |
| | | ); |
| | | } |
| | | |
| | | @Getter |
| | | @AllArgsConstructor |
| | | private static class ResultView { |
| | | private final Integer result; |
| | | private final String error; |
| | | private String normalizeBody(String body, String contentType) { |
| | | if (Cools.isEmpty(body)) { |
| | | return null; |
| | | } |
| | | boolean isJson = !Cools.isEmpty(contentType) && contentType.toLowerCase().contains("json"); |
| | | if (isJson) { |
| | | try { |
| | | 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(); |
| | | } |
| | | |
| | | private static ResultView unknown() { |
| | | return new ResultView(null, null); |
| | | private Map<String, Object> flattenParameters(Map<String, String[]> rawParams) { |
| | | Map<String, Object> flattened = new LinkedHashMap<>(); |
| | | if (rawParams == null) { |
| | | return flattened; |
| | | } |
| | | rawParams.forEach((key, values) -> { |
| | | if (values == null) { |
| | | flattened.put(key, null); |
| | | } else if (values.length == 1) { |
| | | flattened.put(key, values[0]); |
| | | } else { |
| | | flattened.put(key, Arrays.asList(values)); |
| | | } |
| | | }); |
| | | return flattened; |
| | | } |
| | | |
| | | private String readBody(HttpServletRequest request) { |
| | | HttpServletRequest target = unwrapCachingRequest(request); |
| | | if (target instanceof ContentCachingRequestWrapper) { |
| | | ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) target; |
| | | byte[] buffer = wrapper.getContentAsByteArray(); |
| | | if (buffer.length > 0) { |
| | | Charset charset = charset(wrapper.getCharacterEncoding()); |
| | | return new String(buffer, charset); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private HttpServletRequest unwrapCachingRequest(HttpServletRequest request) { |
| | | HttpServletRequest current = request; |
| | | while (current instanceof HttpServletRequestWrapper) { |
| | | if (current instanceof ContentCachingRequestWrapper) { |
| | | return current; |
| | | } |
| | | current = (HttpServletRequest) ((HttpServletRequestWrapper) current).getRequest(); |
| | | } |
| | | return request; |
| | | } |
| | | |
| | | private Charset charset(String encoding) { |
| | | if (Cools.isEmpty(encoding)) { |
| | | return StandardCharsets.UTF_8; |
| | | } |
| | | try { |
| | | return Charset.forName(encoding); |
| | | } catch (Exception e) { |
| | | return StandardCharsets.UTF_8; |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | |
| | | RequestSnapshot(String method, String uri, String query, String contentType, |
| | | Map<String, Object> parameters, String body) { |
| | | this.method = method; |
| | | this.uri = uri; |
| | | this.query = query; |
| | | this.contentType = contentType; |
| | | this.parameters = parameters; |
| | | this.body = body; |
| | | } |
| | | |
| | | public String getUri() { |
| | | return uri; |
| | | } |
| | | } |
| | | } |