use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class RuleBasedRouter method matchValueByValueType.
private boolean matchValueByValueType(boolean isMatchSource, boolean isRegex, String ruleMetaKey, MatchString ruleMetaValue, String destMetaValue, Map<String, String> multiEnvRouterParamMap) {
boolean allMetaMatched = true;
switch(ruleMetaValue.getValueType()) {
case PARAMETER:
// 通过参数传入
if (isMatchSource) {
// 当匹配的是source,记录请求的 K V
multiEnvRouterParamMap.put(ruleMetaKey, destMetaValue);
} else {
// 当匹配的是dest, 判断value
if (!multiEnvRouterParamMap.containsKey(ruleMetaKey)) {
allMetaMatched = false;
} else {
String ruleValue = multiEnvRouterParamMap.get(ruleMetaKey);
// contains key
allMetaMatched = matchValue(isRegex, destMetaValue, ruleValue);
}
}
break;
case VARIABLE:
if (globalVariablesConfig.containsKey(ruleMetaKey)) {
// 1.先从配置获取
String ruleValue = (String) globalVariablesConfig.get(ruleMetaKey);
allMetaMatched = matchValue(isRegex, destMetaValue, ruleValue);
} else {
// 2.从环境变量中获取 key从规则中获取
String key = ruleMetaValue.getValue().getValue();
if (!System.getenv().containsKey(key)) {
allMetaMatched = false;
} else {
String value = System.getenv(key);
allMetaMatched = matchValue(isRegex, destMetaValue, value);
}
if (!System.getenv().containsKey(key) || !System.getenv(key).equals(destMetaValue)) {
allMetaMatched = false;
}
}
break;
default:
allMetaMatched = matchValue(isRegex, destMetaValue, ruleMetaValue.getValue().getValue());
}
return allMetaMatched;
}
use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class RuleBasedRouter method matchMetadata.
// 匹配metadata
private boolean matchMetadata(Map<String, MatchString> ruleMeta, Map<String, String> destMeta, boolean isMatchSource, Map<String, String> multiEnvRouterParamMap) {
// 如果规则metadata为空, 返回成功
if (MapUtils.isEmpty(ruleMeta)) {
return true;
}
if (ruleMeta.containsKey(RuleUtils.MATCH_ALL)) {
return true;
}
// 如果规则metadata不为空, 待匹配规则为空, 直接返回失败
if (MapUtils.isEmpty(destMeta)) {
return false;
}
// metadata是否全部匹配
boolean allMetaMatched = true;
// dest中找到的metadata个数, 用于辅助判断是否能匹配成功
int matchNum = 0;
for (Map.Entry<String, MatchString> entry : ruleMeta.entrySet()) {
String ruleMetaKey = entry.getKey();
MatchString ruleMetaValue = entry.getValue();
if (RuleUtils.isMatchAllValue(ruleMetaValue)) {
matchNum++;
continue;
}
if (destMeta.containsKey(ruleMetaKey)) {
matchNum++;
if (!ruleMetaValue.hasValue() && ruleMetaValue.getValueType() != MatchString.ValueType.PARAMETER) {
continue;
}
String destMetaValue = destMeta.get(ruleMetaKey);
allMetaMatched = isAllMetaMatched(isMatchSource, true, ruleMetaKey, ruleMetaValue, destMetaValue, multiEnvRouterParamMap);
}
if (!allMetaMatched) {
break;
}
}
// 如果一个metadata未找到, 匹配失败
if (matchNum == 0) {
allMetaMatched = false;
}
return allMetaMatched;
}
Aggregations