Search in sources :

Example 1 with Match

use of com.huawei.route.common.gray.label.entity.Match in project Sermant by huaweicloud.

the class RouterUtil method isInvalidRule.

private static boolean isInvalidRule(Rule rule, String interfaceName) {
    if (rule == null) {
        return true;
    }
    Match match = rule.getMatch();
    if (match == null) {
        return true;
    }
    String source = match.getSource();
    if (StringUtils.isExist(source) && !source.equals(DubboCache.INSTANCE.getAppName())) {
        return true;
    }
    if (!interfaceName.equals(match.getPath())) {
        return true;
    }
    if (CollectionUtils.isEmpty(match.getArgs())) {
        return true;
    }
    return CollectionUtils.isEmpty(rule.getRoute());
}
Also used : ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) Match(com.huawei.route.common.gray.label.entity.Match)

Example 2 with Match

use of com.huawei.route.common.gray.label.entity.Match in project Sermant by huaweicloud.

the class RouterUtil method getRoutes.

private static List<Route> getRoutes(Object[] arguments, Rule rule) {
    Match match = rule.getMatch();
    boolean isFullMatch = match.isFullMatch();
    Map<String, List<MatchRule>> args = match.getArgs();
    for (Entry<String, List<MatchRule>> entry : args.entrySet()) {
        String key = entry.getKey();
        if (!key.startsWith(GrayConstant.DUBBO_SOURCE_TYPE_PREFIX)) {
            continue;
        }
        List<MatchRule> matchRuleList = entry.getValue();
        for (MatchRule matchRule : matchRuleList) {
            ValueMatch valueMatch = matchRule.getValueMatch();
            List<String> values = valueMatch.getValues();
            MatchStrategy matchStrategy = valueMatch.getMatchStrategy();
            String arg = TypeStrategyChooser.INSTANCE.getValue(matchRule.getType(), key, arguments).orElse(null);
            if (!isFullMatch && matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果不是全匹配,且匹配了一个,那么直接return
                return rule.getRoute();
            }
            if (isFullMatch && !matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果是全匹配,且有一个不匹配,则继续下一个规则
                return Collections.emptyList();
            }
        }
    }
    if (isFullMatch) {
        // 如果是全匹配,走到这里,说明没有不匹配的,直接return
        return rule.getRoute();
    }
    // 如果不是全匹配,走到这里,说明没有一个规则能够匹配上,则继续下一个规则
    return Collections.emptyList();
}
Also used : ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) ArrayList(java.util.ArrayList) List(java.util.List) MatchRule(com.huawei.route.common.gray.label.entity.MatchRule) ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) Match(com.huawei.route.common.gray.label.entity.Match) MatchStrategy(com.huawei.route.common.gray.label.entity.MatchStrategy)

Example 3 with Match

use of com.huawei.route.common.gray.label.entity.Match in project Sermant by huaweicloud.

the class RouterUtil method getRoutes.

/**
 * 获取路由
 *
 * @param list 规则列表
 * @param request 请求
 * @return 路由
 */
public static List<Route> getRoutes(List<Rule> list, Request request) {
    // 支持headers,parameters和cookie三种参数匹配方式且允许同时配置,全匹配应满足所有三种匹配方式规则
    for (Rule rule : list) {
        Match match = rule.getMatch();
        boolean isFullMatch = match.isFullMatch();
        // headers 参数匹配
        Map<String, List<MatchRule>> headerRules = match.getHeaders();
        boolean isMatchByHeader = true;
        if (headerRules != null) {
            isMatchByHeader = isMatchHeaderRule(headerRules, isFullMatch, request);
        }
        // parameters 参数匹配
        Map<String, List<MatchRule>> paramRules = match.getParameters();
        boolean isMatchByParam = true;
        if (paramRules != null) {
            isMatchByParam = isMatchParamRule(paramRules, isFullMatch, request);
        }
        // cookie 参数匹配
        Map<String, List<MatchRule>> cookieRules = match.getCookie();
        boolean isMatchByCookie = true;
        if (cookieRules != null) {
            isMatchByCookie = isMatchCookieRule(cookieRules, isFullMatch, request);
        }
        if (headerRules == null && paramRules == null && cookieRules == null) {
            continue;
        }
        // 全匹配需要三种配置都匹配上
        if (isFullMatch && isMatchByHeader && isMatchByParam && isMatchByCookie) {
            return rule.getRoute();
        }
        // 非全匹配只要其中一种规则匹配上即可
        if (!isFullMatch && (isMatchByHeader || isMatchByParam || isMatchByCookie)) {
            return rule.getRoute();
        }
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MatchRule(com.huawei.route.common.gray.label.entity.MatchRule) Rule(com.huawei.route.common.gray.label.entity.Rule) ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) Match(com.huawei.route.common.gray.label.entity.Match)

Example 4 with Match

use of com.huawei.route.common.gray.label.entity.Match in project Sermant by huaweicloud.

the class RouterUtil method isValidRule.

private static boolean isValidRule(Rule rule, String applicationName, String path) {
    if (rule == null) {
        return false;
    }
    Match match = rule.getMatch();
    if (match == null) {
        return false;
    }
    String source = match.getSource();
    if (!StringUtils.isBlank(source) && !source.equals(applicationName)) {
        return false;
    }
    String[] arr = match.getPath().split(":/");
    if (arr.length != EXPECT_LENGTH) {
        return false;
    }
    String protocol = arr[0];
    if (!HTTP.equalsIgnoreCase(protocol) && !HTTPS.equalsIgnoreCase(protocol)) {
        return false;
    }
    if (!path.equals(arr[1])) {
        return false;
    }
    if (CollectionUtils.isEmpty(match.getHeaders()) && CollectionUtils.isEmpty(match.getParameters()) && CollectionUtils.isEmpty(match.getCookie())) {
        return false;
    }
    return !CollectionUtils.isEmpty(rule.getRoute());
}
Also used : ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) Match(com.huawei.route.common.gray.label.entity.Match)

Aggregations

Match (com.huawei.route.common.gray.label.entity.Match)4 ValueMatch (com.huawei.route.common.gray.label.entity.ValueMatch)4 MatchRule (com.huawei.route.common.gray.label.entity.MatchRule)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MatchStrategy (com.huawei.route.common.gray.label.entity.MatchStrategy)1 Rule (com.huawei.route.common.gray.label.entity.Rule)1