| | |
| | | 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 |
| | |
| | | Date now = new Date(); |
| | | RequestSnapshot payload = buildSnapshot(request); |
| | | |
| | | System.out.println(JSON.toJSONString(payload.body)); |
| | | |
| | | IntegrationRecord record = new IntegrationRecord(); |
| | | record.setUuid(nextUuid()); |
| | | record.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); |
| | | record.setNamespace(context.getNamespaceType().name()); |
| | | record.setUrl(payload.getUri()); |
| | | record.setAppkey(request.getHeader(HEADER_APP_KEY)); |
| | |
| | | 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 = readBody(request); |
| | | String body = normalizeBody(readBody(request), request.getContentType()); |
| | | return new RequestSnapshot( |
| | | request.getMethod(), |
| | | request.getRequestURI(), |
| | |
| | | ); |
| | | } |
| | | |
| | | 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 Map<String, Object> flattenParameters(Map<String, String[]> rawParams) { |
| | | Map<String, Object> flattened = new LinkedHashMap<>(); |
| | | if (rawParams == null) { |