entry : configs.entrySet()) {
- final String key = entry.getKey();
- for (final String value : entry.getValue()) {
- if (value != null) {
- pairs.add(new KeyValuePair(key, value));
+ return configResult;
+ }
+
+ /**
+ * Parses a JSON configuration file.
+ *
+ * {
+ * "config": {
+ * "loginId": ["rgoers", "adam"],
+ * "accountNumber": ["30510263"]
+ * }
+ * }
+ *
+ */
+ private static void parseJsonConfiguration(final InputStream inputStream, final ConfigResult configResult) {
+ try {
+ final Object wrapper = JsonReader.read(new String(inputStream.readAllBytes(), UTF_8));
+ if (wrapper instanceof Map wrapperMap) {
+ final Object config = wrapperMap.get("configs");
+ if (config instanceof Map, ?> configMap && configMap.size() > 0) {
+ final List pairs = new ArrayList<>();
+ for (Map.Entry, ?> entry : configMap.entrySet()) {
+ final String key = String.valueOf(entry.getKey());
+ final Object jsonArray = entry.getValue();
+ if (jsonArray instanceof List> valueList) {
+ for (final Object value : valueList) {
+ if (value instanceof String stringValue) {
+ pairs.add(new KeyValuePair(key, stringValue));
} else {
- LOGGER.warn("Ignoring null value for {}", key);
+ LOGGER.warn("Ignoring null value for {}: {}", key, value);
}
}
- }
- if (pairs.size() > 0) {
- configResult.pairs = pairs.toArray(EMPTY_ARRAY);
- configResult.status = Status.SUCCESS;
} else {
- configResult.status = Status.EMPTY;
+ LOGGER.warn("Ignoring the value for {}, which is not an array: {}", key, jsonArray);
}
+ }
+ if (pairs.size() > 0) {
+ configResult.pairs = pairs.toArray(EMPTY_ARRAY);
+ configResult.status = Status.SUCCESS;
} else {
- LOGGER.debug("No configuration data in {}", source.toString());
configResult.status = Status.EMPTY;
}
} else {
- LOGGER.warn("No configs element in MutableThreadContextMapFilter configuration");
- configResult.status = Status.ERROR;
+ LOGGER.debug("No configuration data in {}", wrapper);
+ configResult.status = Status.EMPTY;
}
- } catch (Exception ex) {
- LOGGER.warn("Invalid key/value pair configuration, input ignored: {}", ex.getMessage());
+ } else {
+ LOGGER.warn("No configs element in MutableThreadContextMapFilter configuration");
configResult.status = Status.ERROR;
}
- } else {
- configResult.status = result.getStatus();
+ } catch (Exception ex) {
+ LOGGER.warn("Invalid key/value pair configuration, input ignored: {}", ex.getMessage());
+ configResult.status = Status.ERROR;
}
- return configResult;
}
private static class NoOpFilter extends AbstractFilter {
diff --git a/log4j-layout-template-json-test/pom.xml b/log4j-layout-template-json-test/pom.xml
index f0945107da8..94d8664cada 100644
--- a/log4j-layout-template-json-test/pom.xml
+++ b/log4j-layout-template-json-test/pom.xml
@@ -48,6 +48,11 @@
log4j-1.2-api
test