Search in sources :

Example 6 with ApiInfo

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

the class ResponseLogHandler method process.

@Override
public String process(ServerWebExchange exchange, BizSystemModule module, String respBodyAsString) {
    if (!exchange.getResponse().getStatusCode().is2xxSuccessful()) {
        return respBodyAsString;
    }
    ActionLog actionLog = exchange.getAttribute(ActionLogCollector.CURRENT_LOG_CONTEXT_NAME);
    if (actionLog == null)
        return respBodyAsString;
    HttpHeaders headers = exchange.getResponse().getHeaders();
    if (headers.containsKey(CustomRequestHeaders.HEADER_EXCEPTION_CODE)) {
        actionLog.setResponseCode(Integer.parseInt(headers.getFirst(CustomRequestHeaders.HEADER_EXCEPTION_CODE)));
    }
    if (actionLog.getResponseCode() != 200) {
        actionLog.setResponseCode(200);
    }
    if (bodyIgnore)
        return respBodyAsString;
    ApiInfo apiInfo = RuequestHelper.getCurrentApi(exchange);
    if (apiInfo != null && !apiInfo.isResponseLog()) {
        return respBodyAsString;
    }
    // 
    actionLog.setResponseData(respBodyAsString);
    return respBodyAsString;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ApiInfo(com.mendmix.common.model.ApiInfo) ActionLog(com.mendmix.logging.integrate.ActionLog)

Example 7 with ApiInfo

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

the class CurrentSystemHolder method initModuleApiInfos.

private static void initModuleApiInfos(BizSystemModule module) {
    try {
        String url;
        AppMetadata appMetadata;
        if (GlobalRuntimeContext.APPID.equals(module.getRouteName())) {
            appMetadata = AppMetadataHolder.getMetadata();
        } else {
            url = module.getMetadataUri();
            appMetadata = HttpRequestEntity.get(url).backendInternalCall().execute().toObject(AppMetadata.class);
        }
        for (ApiInfo api : appMetadata.getApis()) {
            module.addApiInfo(api);
        }
        moduleApiInfos.put(module.getServiceId(), module.getApiInfos());
        log.info(">>initModuleApiInfos success -> serviceId:{},nums:{}", module.getServiceId(), module.getApiInfos().size());
    } catch (Exception e) {
        boolean ignore = e instanceof ClassCastException;
        if (!ignore && e instanceof JeesuiteBaseException) {
            JeesuiteBaseException ex = (JeesuiteBaseException) e;
            ignore = ex.getCode() == 404 || ex.getCode() == 401 || ex.getCode() == 403;
        }
        if (ignore) {
            module.setApiInfos(new HashMap<>(0));
            moduleApiInfos.put(module.getServiceId(), module.getApiInfos());
        } else if (fetchApiMetaRound <= 1) {
            log.error(">>initModuleApiInfos error -> serviceId:[" + module.getServiceId() + "]", e);
        }
    }
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) HashMap(java.util.HashMap) ApiInfo(com.mendmix.common.model.ApiInfo) AppMetadata(com.mendmix.springweb.model.AppMetadata) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 8 with ApiInfo

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

the class BizSystemModule method getApiInfo.

public ApiInfo getApiInfo(String method, String uri) {
    String uniqueKey = buildUniqueKey(method, uri);
    ApiInfo apiInfo = getApiInfos().get(uniqueKey);
    if (apiInfo != null) {
        return apiInfo;
    }
    for (Pattern pattern : wildcardUris.keySet()) {
        if (pattern.matcher(uniqueKey).matches()) {
            return wildcardUris.get(pattern);
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) ApiInfo(com.mendmix.common.model.ApiInfo)

Example 9 with ApiInfo

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

the class GatewayReactiveCustomAuthnHandler method afterAuthentication.

@Override
public void afterAuthentication(ServerWebExchange exchange, UserSession userSession) {
    if (!GatewayConfigs.actionLogEnabled)
        return;
    ServerHttpRequest request = exchange.getRequest();
    BizSystemModule module = CurrentSystemHolder.getModule(RuequestHelper.resolveRouteName(request.getPath().value()));
    ApiInfo apiInfo = module.getApiInfo(request.getMethodValue(), request.getPath().value());
    boolean logging = apiInfo != null ? apiInfo.isActionLog() : true;
    if (logging) {
        logging = !GatewayConfigs.actionLogGetMethodIngore || !request.getMethod().equals(HttpMethod.GET);
    }
    if (logging) {
        String clientIp = RuequestHelper.getIpAddr(request);
        ActionLog actionLog = ActionLogCollector.onRequestStart(request.getMethodValue(), request.getPath().value(), clientIp).apiMeta(apiInfo);
        exchange.getAttributes().put(ActionLogCollector.CURRENT_LOG_CONTEXT_NAME, actionLog);
    }
}
Also used : ApiInfo(com.mendmix.common.model.ApiInfo) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) ActionLog(com.mendmix.logging.integrate.ActionLog)

Example 10 with ApiInfo

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

the class GatewaySecurityDecisionProvider method getAllApiPermissions.

@Override
public List<ApiPermission> getAllApiPermissions() {
    Collection<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.mendmix.common.model.ApiInfo) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) ApiPermission(com.mendmix.security.model.ApiPermission) ArrayList(java.util.ArrayList)

Aggregations

ApiInfo (com.mendmix.common.model.ApiInfo)10 BizSystemModule (com.mendmix.gateway.model.BizSystemModule)4 ActionLog (com.mendmix.logging.integrate.ActionLog)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)3 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)2 ArrayList (java.util.ArrayList)2 CustomRequestHeaders (com.mendmix.common.CustomRequestHeaders)1 ApiMetadata (com.mendmix.common.annotation.ApiMetadata)1 Page (com.mendmix.common.model.Page)1 ResourceUtils (com.mendmix.common.util.ResourceUtils)1 GatewayConfigs (com.mendmix.gateway.GatewayConfigs)1 PostFilterHandler (com.mendmix.gateway.filter.PostFilterHandler)1 ApiPermission (com.mendmix.security.model.ApiPermission)1 AppMetadata (com.mendmix.springweb.model.AppMetadata)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 StandardCharsets (java.nio.charset.StandardCharsets)1