Skip to content

Commit 8803ecb

Browse files
committed
Remove Jackson dependency from MutableThreadContextMapFilter
1 parent 0fa8ba5 commit 8803ecb

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/filter/MutableThreadContextMapFilter.java

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.filter;
1818

19-
import com.fasterxml.jackson.databind.DeserializationFeature;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
2121
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2222
import java.io.File;
2323
import java.io.FileInputStream;
@@ -38,7 +38,6 @@
3838
import org.apache.logging.log4j.core.config.ConfigurationException;
3939
import org.apache.logging.log4j.core.config.ConfigurationScheduler;
4040
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
41-
import org.apache.logging.log4j.core.filter.mutable.KeyValuePairConfig;
4241
import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
4342
import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory;
4443
import org.apache.logging.log4j.core.util.AuthorizationProvider;
@@ -52,6 +51,7 @@
5251
import org.apache.logging.log4j.plugins.PluginAliases;
5352
import org.apache.logging.log4j.plugins.PluginAttribute;
5453
import org.apache.logging.log4j.plugins.PluginFactory;
54+
import org.apache.logging.log4j.util.JsonReader;
5555
import org.apache.logging.log4j.util.PerformanceSensitive;
5656
import org.apache.logging.log4j.util.PropertyEnvironment;
5757

@@ -64,8 +64,6 @@
6464
@PerformanceSensitive("allocation")
6565
public class MutableThreadContextMapFilter extends AbstractFilter {
6666

67-
private static final ObjectMapper MAPPER =
68-
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
6967
private static final KeyValuePair[] EMPTY_ARRAY = {};
7068

7169
private volatile Filter filter;
@@ -456,44 +454,64 @@ private static ConfigResult getConfig(
456454
final ConfigResult configResult = new ConfigResult();
457455
if (result.getStatus() == Status.SUCCESS) {
458456
LOGGER.debug("Processing Debug key/value pairs from: {}", source.toString());
459-
try {
460-
final KeyValuePairConfig keyValuePairConfig = MAPPER.readValue(inputStream, KeyValuePairConfig.class);
461-
if (keyValuePairConfig != null) {
462-
final Map<String, String[]> configs = keyValuePairConfig.getConfigs();
463-
if (configs != null && configs.size() > 0) {
464-
final List<KeyValuePair> pairs = new ArrayList<>();
465-
for (Map.Entry<String, String[]> entry : configs.entrySet()) {
466-
final String key = entry.getKey();
467-
for (final String value : entry.getValue()) {
468-
if (value != null) {
469-
pairs.add(new KeyValuePair(key, value));
457+
parseJsonConfiguration(inputStream, configResult);
458+
} else {
459+
configResult.status = result.getStatus();
460+
}
461+
return configResult;
462+
}
463+
464+
/**
465+
* Parses a JSON configuration file.
466+
* <pre>
467+
* {
468+
* "config": {
469+
* "loginId": ["rgoers", "adam"],
470+
* "accountNumber": ["30510263"]
471+
* }
472+
* }
473+
* </pre>
474+
*/
475+
private static void parseJsonConfiguration(final InputStream inputStream, final ConfigResult configResult) {
476+
try {
477+
final Object wrapper = JsonReader.read(new String(inputStream.readAllBytes(), UTF_8));
478+
if (wrapper instanceof Map wrapperMap) {
479+
final Object config = wrapperMap.get("configs");
480+
if (config instanceof Map<?, ?> configMap && configMap.size() > 0) {
481+
final List<KeyValuePair> pairs = new ArrayList<>();
482+
for (Map.Entry<?, ?> entry : configMap.entrySet()) {
483+
final String key = String.valueOf(entry.getKey());
484+
final Object jsonArray = entry.getValue();
485+
if (jsonArray instanceof List<?> valueList) {
486+
for (final Object value : valueList) {
487+
if (value instanceof String stringValue) {
488+
pairs.add(new KeyValuePair(key, stringValue));
470489
} else {
471-
LOGGER.warn("Ignoring null value for {}", key);
490+
LOGGER.warn("Ignoring null value for {}: {}", key, value);
472491
}
473492
}
474-
}
475-
if (pairs.size() > 0) {
476-
configResult.pairs = pairs.toArray(EMPTY_ARRAY);
477-
configResult.status = Status.SUCCESS;
478493
} else {
479-
configResult.status = Status.EMPTY;
494+
LOGGER.warn("Ignoring the value for {}, which is not an array: {}", key, jsonArray);
480495
}
496+
}
497+
if (pairs.size() > 0) {
498+
configResult.pairs = pairs.toArray(EMPTY_ARRAY);
499+
configResult.status = Status.SUCCESS;
481500
} else {
482-
LOGGER.debug("No configuration data in {}", source.toString());
483501
configResult.status = Status.EMPTY;
484502
}
485503
} else {
486-
LOGGER.warn("No configs element in MutableThreadContextMapFilter configuration");
487-
configResult.status = Status.ERROR;
504+
LOGGER.debug("No configuration data in {}", wrapper);
505+
configResult.status = Status.EMPTY;
488506
}
489-
} catch (Exception ex) {
490-
LOGGER.warn("Invalid key/value pair configuration, input ignored: {}", ex.getMessage());
507+
} else {
508+
LOGGER.warn("No configs element in MutableThreadContextMapFilter configuration");
491509
configResult.status = Status.ERROR;
492510
}
493-
} else {
494-
configResult.status = result.getStatus();
511+
} catch (Exception ex) {
512+
LOGGER.warn("Invalid key/value pair configuration, input ignored: {}", ex.getMessage());
513+
configResult.status = Status.ERROR;
495514
}
496-
return configResult;
497515
}
498516

499517
private static class NoOpFilter extends AbstractFilter {

0 commit comments

Comments
 (0)