in hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/util/CollectUtil.java [306:401]
public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map<String, Configmap> configmap) {
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
JsonElement element = entry.getValue();
String key = entry.getKey();
// Replace the attributes of the KEY-VALUE case such as http headers params
if (key != null && key.startsWith(SMILING_PLACEHOLDER) && key.endsWith(SMILING_PLACEHOLDER)) {
key = key.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(key);
if (param != null && param.getType() == CommonConstants.PARAM_TYPE_MAP) {
String jsonValue = (String) param.getValue();
TypeReference<Map<String, String>> typeReference = new TypeReference<>() {
};
Map<String, String> map = JsonUtil.fromJson(jsonValue, typeReference);
if (map != null) {
map.forEach((name, value) -> {
if (!StringUtils.isEmpty(name)) {
jsonObject.addProperty(name, value);
}
});
}
}
iterator.remove();
continue;
}
// Replace normal VALUE value
if (!element.isJsonPrimitive()) {
jsonObject.add(entry.getKey(), replaceSmilingPlaceholder(entry.getValue(), configmap));
continue;
}
// Check if there are special characters Replace
String value = element.getAsString();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!smilingMatcher.find()) {
continue;
}
// Replace special characters
value = replaceSpecialCharacterIfNeeded(value, SMILING_PLACEHOLDER_REX, smilingMatcher, configmap);
jsonObject.addProperty(entry.getKey(), value);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
int index = 0;
while (index < jsonArray.size()) {
JsonElement element = jsonArray.get(index);
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (smilingMatcher.find()) {
smilingMatcher.reset();
String[] arrayValues = null;
while (smilingMatcher.find()) {
String group = smilingMatcher.group();
String replaceField = group.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else if (param.getType() == CommonConstants.PARAM_TYPE_ARRAY) {
arrayValues = String.valueOf(param.getValue()).split(",");
} else {
value = value.replace(group, (String) param.getValue());
}
} else {
value = null;
break;
}
}
if (arrayValues != null) {
jsonArray.remove(index);
index--;
for (String arrayValue : arrayValues) {
jsonArray.add(arrayValue);
index++;
}
} else {
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
}
} else {
jsonArray.set(index, replaceSmilingPlaceholder(element, configmap));
}
index++;
}
}
return jsonElement;
}