Search in sources :

Example 1 with MatchStrategy

use of com.huawei.route.common.gray.label.entity.MatchStrategy 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 2 with MatchStrategy

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

the class RouterUtil method matchResultForParamAndCookie.

private static boolean matchResultForParamAndCookie(Map<String, List<MatchRule>> rules, boolean isFullMatch, Map<String, String> paramMap) {
    for (Entry<String, List<MatchRule>> entry : rules.entrySet()) {
        String key = entry.getKey();
        if (!paramMap.containsKey(key)) {
            if (isFullMatch) {
                return false;
            } else {
                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 = paramMap.get(key);
            if (!isFullMatch && matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果不是全匹配,且匹配了一个,那么直接return
                return true;
            }
            if (isFullMatch && !matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果是全匹配,且有一个不匹配,则继续下一个规则
                return false;
            }
        }
    }
    // 执行到此处,如果是全匹配,则匹配成功,如果不是全匹配,则没有匹配到
    return isFullMatch;
}
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) MatchStrategy(com.huawei.route.common.gray.label.entity.MatchStrategy)

Example 3 with MatchStrategy

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

the class RouterUtil method isMatchHeaderRule.

private static boolean isMatchHeaderRule(Map<String, List<MatchRule>> headerRules, boolean isFullMatch, Request request) {
    Map<String, Collection<String>> headers = request.headers();
    for (Entry<String, List<MatchRule>> entry : headerRules.entrySet()) {
        String key = entry.getKey();
        if (!headers.containsKey(key) || headers.get(key).size() == 0) {
            if (isFullMatch) {
                return false;
            } else {
                continue;
            }
        }
        List<MatchRule> headerMatchRuleList = entry.getValue();
        for (MatchRule matchRule : headerMatchRuleList) {
            ValueMatch valueMatch = matchRule.getValueMatch();
            List<String> values = valueMatch.getValues();
            MatchStrategy headerMatchStrategy = valueMatch.getMatchStrategy();
            String arg = new ArrayList<String>(headers.get(key)).get(0);
            if (!isFullMatch && headerMatchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果不是全匹配,且匹配了一个,那么直接return
                return true;
            }
            if (isFullMatch && !headerMatchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
                // 如果是全匹配,且有一个不匹配,则继续下一个规则
                return false;
            }
        }
    }
    // 执行到此处,如果是全匹配,则匹配成功,如果不是全匹配,则没有匹配到
    return isFullMatch;
}
Also used : ValueMatch(com.huawei.route.common.gray.label.entity.ValueMatch) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) MatchRule(com.huawei.route.common.gray.label.entity.MatchRule) MatchStrategy(com.huawei.route.common.gray.label.entity.MatchStrategy)

Aggregations

MatchRule (com.huawei.route.common.gray.label.entity.MatchRule)3 MatchStrategy (com.huawei.route.common.gray.label.entity.MatchStrategy)3 ValueMatch (com.huawei.route.common.gray.label.entity.ValueMatch)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Match (com.huawei.route.common.gray.label.entity.Match)1 Collection (java.util.Collection)1