| | |
| | | |
| | | 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.extern.slf4j.Slf4j; |
| | | import org.springframework.core.annotation.AnnotatedElementUtils; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | 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; |
| | |
| | | @Component |
| | | public class IntegrationOpenApiInterceptor implements HandlerInterceptor { |
| | | |
| | | private static final String ATTR_APP_AUTH = "appAuth"; |
| | | private static final String ATTR_AUTH_MEMO = "integrationAuthMemo"; |
| | | private static final String ATTR_CACHE = "cache"; |
| | | private static final String ATTR_START_AT = "openApiStartTime"; |
| | | private static final String ATTR_CONTEXT = IntegrationOpenApiInterceptor.class.getName() + ".CONTEXT"; |
| | | |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { |
| | |
| | | } |
| | | HandlerMethod handlerMethod = (HandlerMethod) handler; |
| | | IntegrationAuth integrationAuth = findIntegrationAuth(handlerMethod); |
| | | if (integrationAuth == null) { |
| | | log.debug("Skip open-api logging for {} because @IntegrationAuth is missing", handlerMethod.getMethod()); |
| | | if (integrationAuth == null || integrationAuth.value() == IntegrationAuth.Enable.SKIP_LOG) { |
| | | return true; |
| | | } |
| | | |
| | | request.setAttribute(ATTR_APP_AUTH, integrationAuth.value()); |
| | | if (!Cools.isEmpty(integrationAuth.memo())) { |
| | | request.setAttribute(ATTR_AUTH_MEMO, integrationAuth.memo()); |
| | | } |
| | | request.setAttribute(ATTR_START_AT, System.currentTimeMillis()); |
| | | request.setAttribute(ATTR_CACHE, buildRequestCache(request)); |
| | | 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) |
| | | ); |
| | | request.setAttribute(ATTR_CONTEXT, context); |
| | | return true; |
| | | } |
| | | |
| | | public static IntegrationRequestContext getContext(HttpServletRequest request) { |
| | | Object attribute = request.getAttribute(ATTR_CONTEXT); |
| | | if (attribute instanceof IntegrationRequestContext) { |
| | | return (IntegrationRequestContext) attribute; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private IntegrationAuth findIntegrationAuth(HandlerMethod handlerMethod) { |
| | |
| | | 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)); |
| | | String body = readBody(request); |
| | | if (!Cools.isEmpty(body)) { |
| | | cache.put("body", body); |
| | | } |
| | | return cache; |
| | | return Collections.unmodifiableMap(cache); |
| | | } |
| | | |
| | | private Map<String, Object> flattenParameters(Map<String, String[]> rawParams) { |
| | |
| | | |
| | | private Map<String, Object> extractHeaders(HttpServletRequest request) { |
| | | Map<String, Object> headers = new LinkedHashMap<>(); |
| | | List<String> names = Collections.list(request.getHeaderNames()); |
| | | 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 StandardCharsets.UTF_8; |
| | | } |
| | | } |
| | | |
| | | @Value |
| | | public static class IntegrationRequestContext { |
| | | NamespaceType namespaceType; |
| | | String namespace; |
| | | String handler; |
| | | String method; |
| | | String uri; |
| | | String query; |
| | | long startAt; |
| | | Map<String, Object> requestSnapshot; |
| | | } |
| | | } |