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

com.ingenico.connect.gateway.sdk.java.logging.BodyObfuscator Maven / Gradle / Ivy

package com.ingenico.connect.gateway.sdk.java.logging;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A class that can be used to obfuscate properties in JSON bodies. Thread-safe if all its obfuscation rules are.
 */
public final class BodyObfuscator {

	private static final BodyObfuscator DEFAULT_OBFUSCATOR = custom().build();

	private final Map obfuscationRules;
	private final Pattern propertyPattern;

	private BodyObfuscator(Map obfuscationRules) {
		// case sensitive
		this.obfuscationRules = Collections.unmodifiableMap(new LinkedHashMap(obfuscationRules));
		this.propertyPattern = buildPropertyPattern(obfuscationRules.keySet());
	}

	private static Pattern buildPropertyPattern(Set propertyNames) {
		if (propertyNames.isEmpty()) {
			// no matches possible
			return Pattern.compile("$^");
		}

		Iterator iterator = propertyNames.iterator();

		/*
		 * Regex to create: (["'])(X|Y|Z)\1\s*:\s*(?:(["'])(.*?)(? obfuscationRules;

		private Builder() {
			obfuscationRules = new LinkedHashMap();
		}

		/**
		 * Adds an obfuscation rule that will replace all characters with {@code *}.
		 */
		public Builder obfuscateAll(String propertyName) {
			obfuscationRules.put(propertyName, ValueObfuscator.ALL);
			return this;
		}

		/**
		 * Adds an obfuscation rule that will replace values with a fixed length string containing
		 * only {@code *}.
		 */
		public Builder obfuscateWithFixedLength(int fixedLength, String propertyName) {
			obfuscationRules.put(propertyName, ValueObfuscator.fixedLength(fixedLength));
			return this;
		}

		/**
		 * Adds an obfuscation rule that will keep a fixed number of characters at the start, then
		 * replaces all other characters with {@code *}.
		 */
		public Builder obfuscateAllButFirst(int count, String propertyName) {
			obfuscationRules.put(propertyName, ValueObfuscator.keepStartCount(count));
			return this;
		}

		/**
		 * Adds an obfuscation rule that will keep a fixed number of characters at the end, then
		 * replaces all other characters with {@code *}.
		 */
		public Builder obfuscateAllButLast(int count, String propertyName) {
			obfuscationRules.put(propertyName, ValueObfuscator.keepEndCount(count));
			return this;
		}

		/**
		 * Adds a custom, non-{@code null} obfuscation rule.
		 */
		public Builder obfuscateCustom(String propertyName, ObfuscationRule obfuscationRule) {
			if (obfuscationRule == null) {
				throw new IllegalArgumentException("obfuscationRule is required");
			}
			obfuscationRules.put(propertyName, obfuscationRule);
			return this;
		}

		public BodyObfuscator build() {
			return new BodyObfuscator(obfuscationRules);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy