Search in sources :

Example 1 with ApiInfo

use of com.jeesuite.common.model.ApiInfo in project jeesuite-libs by vakinge.

the class GatewaySecurityDecisionProvider method getAllApiPermissions.

@Override
public List<ApiPermission> getAllApiPermissions() {
    List<BizSystemModule> modules = CurrentSystemHolder.getModules();
    List<ApiPermission> result = new ArrayList<>();
    Collection<ApiInfo> apis;
    ApiPermission apiPermission;
    for (BizSystemModule module : modules) {
        if (module.getApiInfos() == null)
            continue;
        apis = module.getApiInfos().values();
        for (ApiInfo apiInfo : apis) {
            apiPermission = new ApiPermission();
            apiPermission.setGrantType(apiInfo.getPermissionType().name());
            apiPermission.setHttpMethod(apiInfo.getMethod());
            apiPermission.setUri(apiInfo.getUrl());
            result.add(apiPermission);
        }
    }
    return result;
}
Also used : ApiInfo(com.jeesuite.common.model.ApiInfo) BizSystemModule(com.jeesuite.gateway.model.BizSystemModule) ApiPermission(com.jeesuite.security.model.ApiPermission) ArrayList(java.util.ArrayList)

Example 2 with ApiInfo

use of com.jeesuite.common.model.ApiInfo in project jeesuite-libs by vakinge.

the class GlobalAdditionHandler method afterAuthorization.

@Override
public void afterAuthorization(UserSession userSession) {
    if (!actionLogEnabled)
        return;
    HttpServletRequest request = CurrentRuntimeContext.getRequest();
    BizSystemModule module = CurrentSystemHolder.getModule(currentRouteName(request.getRequestURI()));
    ApiInfo apiInfo = module.getApiInfo(request.getRequestURI());
    boolean logging = apiInfo != null ? apiInfo.isActionLog() : true;
    if (logging) {
        logging = !ignoreReadMethodLog || !request.getMethod().equals(RequestMethod.GET.name());
    }
    if (logging) {
        ActionLogCollector.onRequestStart(request).apiMeta(apiInfo);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ApiInfo(com.jeesuite.common.model.ApiInfo) BizSystemModule(com.jeesuite.gateway.model.BizSystemModule)

Example 3 with ApiInfo

use of com.jeesuite.common.model.ApiInfo in project jeesuite-libs by vakinge.

the class ResponseLogHandler method process.

@Override
public Object process(RequestContext ctx, HttpServletRequest request, BizSystemModule module) {
    int statusCode = ctx.getResponseStatusCode();
    if (statusCode != 200)
        return null;
    ActionLog actionLog = ActionLogCollector.currentActionLog();
    if (actionLog == null)
        return null;
    List<Pair<String, String>> headers = ctx.getOriginResponseHeaders();
    for (Pair<String, String> pair : headers) {
        if (CustomRequestHeaders.HEADER_EXCEPTION_CODE.equals(pair.first())) {
            actionLog.setResponseCode(Integer.parseInt(pair.second()));
            break;
        }
    }
    if (ignoreBody)
        return null;
    ApiInfo apiInfo = module.getApiInfo(request.getRequestURI());
    if (apiInfo != null && !apiInfo.isResponseLog()) {
        return null;
    }
    ResponseCompose responseCompose = new ResponseCompose(ctx);
    ctx.set(ResponseCompose.class.getName(), responseCompose);
    actionLog.setResponseData(responseCompose.getBodyString());
    return null;
}
Also used : ApiInfo(com.jeesuite.common.model.ApiInfo) ActionLog(com.jeesuite.logging.integrate.ActionLog) Pair(com.netflix.util.Pair)

Example 4 with ApiInfo

use of com.jeesuite.common.model.ApiInfo in project jeesuite-libs by vakinge.

the class RequestLogHandler method process.

@Override
public Object process(RequestContext ctx, HttpServletRequest request, BizSystemModule module) {
    ActionLog actionLog = ActionLogCollector.currentActionLog();
    if (actionLog == null)
        return null;
    actionLog.setModuleId(module.getServiceId());
    ApiInfo apiInfo = module.getApiInfo(request.getRequestURI());
    if (apiInfo != null && !apiInfo.isRequestLog()) {
        return null;
    }
    actionLog.setQueryParameters(request.getQueryString());
    if (!ignoreBody && HttpMethod.POST.name().equals(request.getMethod()) && !WebUtils.isMultipartContent(request)) {
        try {
            String data = CharStreams.toString(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            actionLog.setRequestData(data);
        } catch (Exception e) {
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) ApiInfo(com.jeesuite.common.model.ApiInfo) ActionLog(com.jeesuite.logging.integrate.ActionLog)

Example 5 with ApiInfo

use of com.jeesuite.common.model.ApiInfo 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)

Aggregations

ApiInfo (com.jeesuite.common.model.ApiInfo)6 BizSystemModule (com.jeesuite.gateway.model.BizSystemModule)2 ActionLog (com.jeesuite.logging.integrate.ActionLog)2 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)1 ApiMetadata (com.jeesuite.common.annotation.ApiMetadata)1 ApiPermission (com.jeesuite.security.model.ApiPermission)1 AppMetadata (com.jeesuite.springweb.model.AppMetadata)1 Pair (com.netflix.util.Pair)1 ApiOperation (io.swagger.annotations.ApiOperation)1 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)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