| | |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.annotation.IntegrationAuth; |
| | | import com.zy.acs.manager.core.domain.type.NamespaceType; |
| | | import lombok.Value; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.core.annotation.AnnotatedElementUtils; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.Enumeration; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | |
| | | NamespaceType namespaceType = integrationAuth.name() == null ? NamespaceType.NONE : integrationAuth.name(); |
| | | IntegrationRequestContext context = new IntegrationRequestContext( |
| | | namespaceType, |
| | | namespaceType.name, |
| | | handlerMethod.getBeanType().getSimpleName() + "#" + handlerMethod.getMethod().getName(), |
| | | request.getMethod(), |
| | | request.getRequestURI(), |
| | | request.getQueryString(), |
| | | System.currentTimeMillis(), |
| | | buildRequestCache(request) |
| | | buildPayload(request) |
| | | ); |
| | | request.setAttribute(ATTR_CONTEXT, context); |
| | | return true; |
| | |
| | | return AnnotatedElementUtils.findMergedAnnotation(handlerMethod.getBeanType(), IntegrationAuth.class); |
| | | } |
| | | |
| | | private Map<String, Object> buildRequestCache(HttpServletRequest request) { |
| | | Map<String, Object> cache = new LinkedHashMap<>(); |
| | | cache.put("method", request.getMethod()); |
| | | cache.put("uri", request.getRequestURI()); |
| | | cache.put("query", request.getQueryString()); |
| | | cache.put("contentType", request.getContentType()); |
| | | cache.put("parameters", flattenParameters(request.getParameterMap())); |
| | | cache.put("headers", extractHeaders(request)); |
| | | private RequestPayload buildPayload(HttpServletRequest request) { |
| | | Map<String, Object> params = flattenParameters(request.getParameterMap()); |
| | | String body = readBody(request); |
| | | if (!Cools.isEmpty(body)) { |
| | | cache.put("body", body); |
| | | } |
| | | return Collections.unmodifiableMap(cache); |
| | | return new RequestPayload( |
| | | request.getMethod(), |
| | | request.getRequestURI(), |
| | | request.getQueryString(), |
| | | request.getContentType(), |
| | | params.isEmpty() ? null : params, |
| | | Cools.isEmpty(body) ? null : body |
| | | ); |
| | | } |
| | | |
| | | private Map<String, Object> flattenParameters(Map<String, String[]> rawParams) { |
| | |
| | | } |
| | | }); |
| | | return flattened; |
| | | } |
| | | |
| | | private Map<String, Object> extractHeaders(HttpServletRequest request) { |
| | | Map<String, Object> headers = new LinkedHashMap<>(); |
| | | Enumeration<String> headerNames = request.getHeaderNames(); |
| | | if (headerNames == null) { |
| | | return headers; |
| | | } |
| | | List<String> names = Collections.list(headerNames); |
| | | for (String name : names) { |
| | | headers.put(name, request.getHeader(name)); |
| | | } |
| | | return headers; |
| | | } |
| | | |
| | | private String readBody(HttpServletRequest request) { |
| | |
| | | } |
| | | } |
| | | |
| | | @Value |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public static class IntegrationRequestContext { |
| | | NamespaceType namespaceType; |
| | | String namespace; |
| | | String handler; |
| | | String method; |
| | | String uri; |
| | | String query; |
| | | long startAt; |
| | | Map<String, Object> requestSnapshot; |
| | | private final NamespaceType namespaceType; |
| | | private final String handler; |
| | | private final long startAt; |
| | | private final RequestPayload payload; |
| | | } |
| | | |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public static class RequestPayload { |
| | | 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; |
| | | } |
| | | } |