All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icthh.xm.commons.logging.util.MaskingService Maven / Gradle / Ivy

There is a newer version: 4.0.17
Show newest version
package com.icthh.xm.commons.logging.util;

import com.icthh.xm.commons.logging.config.LoggingConfig.MaskingLogConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import static java.util.Collections.emptyList;

public class MaskingService {
    private final Pattern multilinePattern;
    private final boolean enabled;

    public MaskingService(MaskingLogConfiguration maskingLogConfiguration, List defaultMaskPatterns) {
        List maskPatterns = getMaskPatterns(maskingLogConfiguration);
        maskPatterns.addAll(defaultMaskPatterns);
        this.enabled = isEnabled(maskingLogConfiguration);
        this.multilinePattern = Pattern.compile(String.join("|", maskPatterns), Pattern.MULTILINE);
    }

    private static List getMaskPatterns(MaskingLogConfiguration maskingLogConfiguration) {
        List patterns = Optional.ofNullable(maskingLogConfiguration)
            .map(MaskingLogConfiguration::getMaskPatterns)
            .orElse(emptyList());
        return new ArrayList<>(patterns);
    }

    private static Boolean isEnabled(MaskingLogConfiguration maskingLogConfiguration) {
        return Optional.ofNullable(maskingLogConfiguration)
            .map(MaskingLogConfiguration::getEnabled)
            .orElse(false);
    }

    public String maskMessage(String message) {
        if (!enabled) {
            return message;
        }

        StringBuilder sb = new StringBuilder(message);
        Matcher matcher = multilinePattern.matcher(sb);
        while (matcher.find()) {
            IntStream.rangeClosed(1, matcher.groupCount()).forEach(group -> {
                if (matcher.group(group) != null) {
                    IntStream.range(matcher.start(group), matcher.end(group)).forEach(i -> sb.setCharAt(i, '*'));
                }
            });
        }
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy