<commit_before>package io.tracee.configuration;

import io.tracee.Utilities;

import java.util.*;

public final class PropertiesBasedTraceeFilterConfiguration implements TraceeFilterConfiguration {

	public static final String TRACEE_CONFIG_PREFIX = "tracee.";

	static final String DEFAULT_PROFILE_PREFIX = DEFAULT_PROFILE + ".";
	public static final String GENERATE_REQUEST_ID = "requestIdLength";
	public static final String GENERATE_SESSION_ID = "sessionIdLength";

	private final PropertyChain propertyChain;
	private final String profileName;

	public PropertiesBasedTraceeFilterConfiguration(PropertyChain propertyChain) {
		this(propertyChain, null);
	}

	public PropertiesBasedTraceeFilterConfiguration(PropertyChain propertyChain, String profileName) {
		this.propertyChain = propertyChain;
		this.profileName = profileName;
	}

	private String getProfiledOrDefaultProperty(String propertyName) {
		if (profileName != null) {
			final String profiledProperty = propertyChain.getProperty(TRACEE_CONFIG_PREFIX + profileName + '.' + propertyName);
			if (profiledProperty != null)
				return profiledProperty;
		}
		return propertyChain.getProperty(TRACEE_CONFIG_PREFIX + DEFAULT_PROFILE_PREFIX + propertyName);
	}

	@Override
	public boolean shouldProcessParam(String paramName, Channel channel) {
		final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name());
		final List<String> patterns = extractPatterns(messageTypePropertyValue);
		return anyPatternMatchesParamName(patterns, paramName);
	}

	@Override
	public boolean shouldProcessContext(Channel channel) {
		final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name());
		return !Utilities.isNullOrEmptyString(messageTypePropertyValue);
	}

	@Override
	public boolean shouldGenerateRequestId() {
		return generatedRequestIdLength() > 0;
	}

	@Override
	public int generatedRequestIdLength() {
		return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_REQUEST_ID));
	}

	@Override
	public boolean shouldGenerateSessionId() {
		return generatedSessionIdLength() > 0;
	}

	@Override
	public int generatedSessionIdLength() {
		return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_SESSION_ID));
	}

	@Override
	public Map<String, String> filterDeniedParams(Map<String, String> unfiltered, Channel channel) {
		final Map<String, String> filtered = new HashMap<String, String>(unfiltered.size());
		for (Map.Entry<String, String> entry : unfiltered.entrySet()) {
			if (shouldProcessParam(entry.getKey(), channel)) {
				filtered.put(entry.getKey(), entry.getValue());
			}
		}
		return filtered;
	}

	private int parseIntOrZero(String intString) {
		try {
			return Integer.parseInt(intString);
		} catch (NumberFormatException nfe) {
			return 0;
		}
	}

	private boolean anyPatternMatchesParamName(Iterable<String> patterns, String paramName) {
		for (String pattern : patterns) {
			if (patternMatchesParamName(pattern, paramName))
				return true;
		}
		return false;
	}

	private boolean patternMatchesParamName(String pattern, String paramName) {
		return paramName.matches(pattern);
	}

	private List<String> extractPatterns(String propertyValue) {
		if (propertyValue == null)
			return Collections.emptyList();

		final List<String> trimmedPatterns = new ArrayList<String>();<commit_msg>Speed up property filtering by bypass regex stuff<commit_after>package io.tracee.configuration;

import io.tracee.Utilities;

import java.util.*;

public final class PropertiesBasedTraceeFilterConfiguration implements TraceeFilterConfiguration {

	public static final String TRACEE_CONFIG_PREFIX = "tracee.";

	static final String DEFAULT_PROFILE_PREFIX = DEFAULT_PROFILE + ".";
	public static final String GENERATE_REQUEST_ID = "requestIdLength";
	public static final String GENERATE_SESSION_ID = "sessionIdLength";

	private final PropertyChain propertyChain;
	private final String profileName;

	public PropertiesBasedTraceeFilterConfiguration(PropertyChain propertyChain) {
		this(propertyChain, null);
	}

	public PropertiesBasedTraceeFilterConfiguration(PropertyChain propertyChain, String profileName) {
		this.propertyChain = propertyChain;
		this.profileName = profileName;
	}

	private String getProfiledOrDefaultProperty(String propertyName) {
		if (profileName != null) {
			final String profiledProperty = propertyChain.getProperty(TRACEE_CONFIG_PREFIX + profileName + '.' + propertyName);
			if (profiledProperty != null)
				return profiledProperty;
		}
		return propertyChain.getProperty(TRACEE_CONFIG_PREFIX + DEFAULT_PROFILE_PREFIX + propertyName);
	}

	@Override
	public boolean shouldProcessParam(String paramName, Channel channel) {
		final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name());
		final List<String> patterns = extractPatterns(messageTypePropertyValue);
		return anyPatternMatchesParamName(patterns, paramName);
	}

	@Override
	public boolean shouldProcessContext(Channel channel) {
		final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name());
		return !Utilities.isNullOrEmptyString(messageTypePropertyValue);
	}

	@Override
	public boolean shouldGenerateRequestId() {
		return generatedRequestIdLength() > 0;
	}

	@Override
	public int generatedRequestIdLength() {
		return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_REQUEST_ID));
	}

	@Override
	public boolean shouldGenerateSessionId() {
		return generatedSessionIdLength() > 0;
	}

	@Override
	public int generatedSessionIdLength() {
		return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_SESSION_ID));
	}

	@Override
	public Map<String, String> filterDeniedParams(Map<String, String> unfiltered, Channel channel) {
		final Map<String, String> filtered = new HashMap<String, String>(unfiltered.size());
		for (Map.Entry<String, String> entry : unfiltered.entrySet()) {
			if (shouldProcessParam(entry.getKey(), channel)) {
				filtered.put(entry.getKey(), entry.getValue());
			}
		}
		return filtered;
	}

	private int parseIntOrZero(String intString) {
		try {
			return Integer.parseInt(intString);
		} catch (NumberFormatException nfe) {
			return 0;
		}
	}

	private boolean anyPatternMatchesParamName(Iterable<String> patterns, String paramName) {
		for (String pattern : patterns) {
			if (patternMatchesParamName(pattern, paramName))
				return true;
		}
		return false;
	}

	private boolean patternMatchesParamName(String pattern, String paramName) {
		// .* matches the whole param and we could speed up this call with a simple equal
		return ".*".equals(pattern) || paramName.matches(pattern);
	}

	private List<String> extractPatterns(String propertyValue) {
		if (propertyValue == null)
			return Collections.emptyList();

		final List<String> trimmedPatterns = new ArrayList<String>();
{'fraction_non_alphanumeric': 0.08660371791088817, 'fraction_numerical': 0.0008852168781351431, 'mean_word_length': 6.135789473684211, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501977', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1841, 'n_words': 448}