private void parseResponseByConfig()

in hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java [444:522]


    private void parseResponseByConfig(String resp, List<String> aliasFields, HttpProtocol http,
                                       CollectRep.MetricsData.Builder builder, Long responseTime) {
        if (!StringUtils.hasText(resp)) {
            log.warn("Http collect parse type is config, but response body is empty.");
            builder.setCode(CollectRep.Code.FAIL);
            builder.setMsg("Response body is empty");
            return;
        }

        Properties properties = new Properties();
        try (StringReader reader = new StringReader(resp)) {
            properties.load(reader);
        } catch (IOException e) {
            log.warn("Failed to parse config response: {}", e.getMessage(), e);
            builder.setCode(CollectRep.Code.FAIL);
            builder.setMsg("Failed to parse config response: " + e.getMessage());
            return;
        }
        String arrayBasePath = http.getParseScript();
        int keywordNum = CollectUtil.countMatchKeyword(resp, http.getKeyword());

        if (!StringUtils.hasText(arrayBasePath)) {
            CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
            for (String alias : aliasFields) {
                if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
                    valueRowBuilder.addColumn(responseTime.toString());
                } else if (CollectorConstants.KEYWORD.equalsIgnoreCase(alias)) {
                    valueRowBuilder.addColumn(Integer.toString(keywordNum));
                } else {
                    String value = properties.getProperty(alias);
                    valueRowBuilder.addColumn(value != null ? value : CommonConstants.NULL_VALUE);
                }
            }
            CollectRep.ValueRow valueRow = valueRowBuilder.build();
            if (hasMeaningfulDataInRow(valueRow, aliasFields)) {
                builder.addValueRow(valueRow);
            } else {
                log.warn("No meaningful data found in single config object response for aliasFields: {}", aliasFields);
            }
        } else {
            Pattern pattern = Pattern.compile("^" + Pattern.quote(arrayBasePath) + "\\[(\\d+)]\\.");
            Set<Integer> existingIndices = new HashSet<>();
            for (String key : properties.stringPropertyNames()) {
                Matcher matcher = pattern.matcher(key);
                if (matcher.find()) {
                    try {
                        int index = Integer.parseInt(matcher.group(1));
                        existingIndices.add(index);
                    } catch (NumberFormatException e) {
                        log.error("Could not parse index from key: {}", key);
                    }
                }
            }
            if (existingIndices.isEmpty()) {
                log.warn("Could not find any array elements for base path '{}' in config response.", arrayBasePath);
                return;
            }
            List<Integer> sortedIndices = new ArrayList<>(existingIndices);
            Collections.sort(sortedIndices);
            for (int i : sortedIndices) {
                CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
                for (String alias : aliasFields) {
                    if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
                        valueRowBuilder.addColumn(responseTime.toString());
                    } else if (CollectorConstants.KEYWORD.equalsIgnoreCase(alias)) {
                        valueRowBuilder.addColumn(Integer.toString(keywordNum));
                    } else {
                        String currentKey = arrayBasePath + "[" + i + "]." + alias;
                        String value = properties.getProperty(currentKey);
                        valueRowBuilder.addColumn(value != null ? value : CommonConstants.NULL_VALUE);
                    }
                }
                CollectRep.ValueRow valueRow = valueRowBuilder.build();
                if (hasMeaningfulDataInRow(valueRow, aliasFields)) {
                    builder.addValueRow(valueRowBuilder.build());
                }
            }
        }
    }