Search in sources :

Example 1 with ApiMetadata

use of com.jeesuite.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.

the class RequestLoggingInterceptor method around.

@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
    ActionLog actionLog = ActionLogCollector.currentActionLog();
    if (actionLog == null) {
        return pjp.proceed();
    }
    HttpServletRequest request = CurrentRuntimeContext.getRequest();
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    boolean requestLog = true;
    boolean responseLog = !ignoreResponseBody;
    if (method.isAnnotationPresent(ApiMetadata.class)) {
        ApiMetadata metadata = method.getAnnotation(ApiMetadata.class);
        requestLog = metadata.requestLog();
        responseLog = metadata.responseLog();
        if (!requestLog && !responseLog) {
            return pjp.proceed();
        }
    }
    Map<String, Object> parameters = null;
    Object body = null;
    if (requestLog) {
        Object[] args = pjp.getArgs();
        if (args.length > 0) {
            List<ParameterLogConfig> parameterConfigs = getParameterConfigs(pjp.getTarget().getClass().getName(), method);
            parameters = new HashMap<>(3);
            ParameterLogConfig config;
            for (int i = 0; i < args.length; i++) {
                if ((config = parameterConfigs.get(i)).ignore)
                    continue;
                if (config.isBody) {
                    body = args[i];
                } else if (config.paramName != null && args[i] != null) {
                    parameters.put(config.paramName, args[i].toString());
                }
            }
        }
        if (log.isDebugEnabled()) {
            String requestLogMessage = RequestLogBuilder.requestLogMessage(request.getRequestURI(), request.getMethod(), parameters, body);
            log.debug(requestLogMessage);
        }
    }
    actionLog.setQueryParameters(ParameterUtils.mapToQueryParams(parameters));
    actionLog.setRequestData(body);
    Object result = null;
    try {
        result = pjp.proceed();
        // 
        if (responseLog) {
            actionLog.setResponseData(result);
        }
        return result;
    } catch (Exception e) {
        if (e instanceof JeesuiteBaseException) {
            actionLog.setExceptions(e.getMessage());
        } else {
            actionLog.setExceptions(ExceptionUtils.getMessage(e));
        }
        throw e;
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) ApiMetadata(com.jeesuite.common.annotation.ApiMetadata) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) HttpServletRequest(javax.servlet.http.HttpServletRequest) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Around(org.aspectj.lang.annotation.Around)

Example 2 with ApiMetadata

use of com.jeesuite.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.

the class AppMetadataHolder method scanApiInfos.

private static synchronized void scanApiInfos(AppMetadata metadata, List<String> classNameList) {
    if (!metadata.getApis().isEmpty())
        return;
    Method[] methods;
    String baseUri;
    ApiInfo apiInfo;
    ApiMetadata classMetadata;
    ApiMetadata methodMetadata;
    for (String className : classNameList) {
        if (!className.contains(GlobalRuntimeContext.MODULE_NAME))
            continue;
        try {
            Class<?> clazz = Class.forName(className);
            if (!clazz.isAnnotationPresent(Controller.class) && !clazz.isAnnotationPresent(RestController.class)) {
                continue;
            }
            RequestMapping requestMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);
            if (requestMapping != null) {
                baseUri = requestMapping.value()[0];
                if (!baseUri.startsWith("/"))
                    baseUri = "/" + baseUri;
                if (baseUri.endsWith("/"))
                    baseUri = baseUri.substring(0, baseUri.length() - 1);
            } else {
                baseUri = "";
            }
            // 
            classMetadata = clazz.getAnnotation(ApiMetadata.class);
            methods = clazz.getDeclaredMethods();
            Map<String, Method> interfaceMethods = getInterfaceMethods(clazz);
            methodLoop: for (Method method : methods) {
                methodMetadata = method.isAnnotationPresent(ApiMetadata.class) ? method.getAnnotation(ApiMetadata.class) : classMetadata;
                String apiUri = null;
                String apiHttpMethod = null;
                requestMapping = getAnnotation(method, interfaceMethods.get(method.getName()), RequestMapping.class);
                if (requestMapping != null) {
                    apiUri = requestMapping.value()[0];
                    if (requestMapping.method() != null && requestMapping.method().length > 0) {
                        apiHttpMethod = requestMapping.method()[0].name();
                    }
                } else {
                    PostMapping postMapping = getAnnotation(method, interfaceMethods.get(method.getName()), PostMapping.class);
                    if (postMapping != null) {
                        apiUri = postMapping.value()[0];
                        apiHttpMethod = RequestMethod.POST.name();
                    }
                    GetMapping getMapping = getAnnotation(method, interfaceMethods.get(method.getName()), GetMapping.class);
                    if (getMapping != null) {
                        apiUri = getMapping.value()[0];
                        apiHttpMethod = RequestMethod.GET.name();
                    }
                }
                if (StringUtils.isBlank(apiUri)) {
                    continue methodLoop;
                }
                apiInfo = new ApiInfo();
                if (apiUri == null) {
                    apiUri = baseUri;
                } else {
                    if (!apiUri.startsWith("/")) {
                        apiUri = "/" + apiUri;
                    }
                    apiUri = baseUri + apiUri;
                }
                apiInfo.setUrl(apiUri);
                apiInfo.setMethod(apiHttpMethod);
                if (method.isAnnotationPresent(ApiOperation.class)) {
                    apiInfo.setName(method.getAnnotation(ApiOperation.class).value());
                } else {
                    apiInfo.setName(apiInfo.getUrl());
                }
                if (methodMetadata != null) {
                    apiInfo.setActionLog(methodMetadata.actionLog());
                    apiInfo.setRequestLog(methodMetadata.requestLog());
                    apiInfo.setResponseLog(methodMetadata.responseLog());
                }
                if (methodMetadata != null && StringUtils.isNotBlank(methodMetadata.actionName())) {
                    apiInfo.setName(methodMetadata.actionName());
                } else if (method.isAnnotationPresent(ApiOperation.class)) {
                    apiInfo.setName(method.getAnnotation(ApiOperation.class).value());
                }
                if (methodMetadata == null) {
                    apiInfo.setPermissionType(PermissionLevel.LoginRequired);
                } else {
                    apiInfo.setPermissionType(methodMetadata.permissionLevel());
                }
                metadata.addApi(apiInfo);
            }
        } catch (Exception e) {
            System.err.println("error className:" + className);
        }
    }
}
Also used : GetMapping(org.springframework.web.bind.annotation.GetMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) ApiMetadata(com.jeesuite.common.annotation.ApiMetadata) ApiInfo(com.jeesuite.common.model.ApiInfo) ApiOperation(io.swagger.annotations.ApiOperation) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ApiMetadata

use of com.jeesuite.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.

the class GlobalDefaultInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (!integratedGatewayDeploy) {
        CurrentRuntimeContext.init(request, response);
        // 
        if (invokeTokenCheckEnabled) {
            String uri = request.getRequestURI();
            if (!invoketokenCheckIgnoreUriMather.match(uri)) {
                String authCode = request.getHeader(CustomRequestHeaders.HEADER_INVOKE_TOKEN);
                TokenGenerator.validate(authCode, true);
            }
        }
    }
    if (handler instanceof HandlerMethod) {
        HandlerMethod method = (HandlerMethod) handler;
        ApiMetadata config = method.getMethod().getAnnotation(ApiMetadata.class);
        if (config != null) {
            if (!isLocalEnv && config.IntranetAccessOnly() && !WebUtils.isInternalRequest(request)) {
                response.setStatus(403);
                throw new ForbiddenAccessException();
            }
            // @ResponseBody and ResponseEntity的接口在postHandle addHeader不生效,因为会经过HttpMessageConverter
            if (config.responseKeep()) {
                response.addHeader(CustomRequestHeaders.HEADER_RESP_KEEP, Boolean.TRUE.toString());
            }
        }
        // 行为日志
        if (requestLogEnabled) {
            boolean logging = config == null ? true : config.actionLog();
            ;
            if (logging) {
                logging = !requestLogGetIngore || !request.getMethod().equals(RequestMethod.GET.name());
            }
            if (logging) {
                ActionLogCollector.onRequestStart(request).apiMeta(config);
            }
        }
    }
    return true;
}
Also used : ApiMetadata(com.jeesuite.common.annotation.ApiMetadata) ForbiddenAccessException(com.jeesuite.common.exception.ForbiddenAccessException) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

ApiMetadata (com.jeesuite.common.annotation.ApiMetadata)3 Method (java.lang.reflect.Method)2 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)1 ForbiddenAccessException (com.jeesuite.common.exception.ForbiddenAccessException)1 ApiInfo (com.jeesuite.common.model.ApiInfo)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1