org.kuali.rice.core.impl.security.PropertySuppressionServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.kuali.rice.core.impl.security.PropertySuppressionServiceImpl.java

Source

/*-
 * #%L
 * %%
 * Copyright (C) 2005 - 2019 Kuali, Inc. - All Rights Reserved
 * %%
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 * 
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 * #L%
 */
package org.kuali.rice.core.impl.security;

import org.apache.commons.lang3.StringUtils;
import org.kuali.rice.core.api.config.property.ConfigurationService;
import org.kuali.rice.core.api.security.PropertySuppressionService;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PropertySuppressionServiceImpl implements PropertySuppressionService {

    private static final String PROP_SUPPRESS_PATTERNS = "prop.suppress.patterns";

    private ConfigurationService configurationService;

    @Override
    public String suppressValue(String property, String value) {
        if (property == null) {
            throw new IllegalArgumentException("property is null");
        }

        return shouldSuppress(property) ? suppressionString() : value;
    }

    @Override
    public Map<String, String> suppressValueMap(Map<String, String> properties) {
        if (properties == null) {
            throw new IllegalArgumentException("properties is null");
        }

        return properties.entrySet().stream().map(e -> entry(e.getKey(), suppressValue(e.getKey(), e.getValue())))
                .collect(nullSafeEntriesToMap());
    }

    @Override
    public List<String> suppressValues(String property, List<String> values) {
        if (property == null) {
            throw new IllegalArgumentException("property is null");
        }

        if (shouldSuppress(property)) {
            if (values == null) {
                return null;
            } else {
                return values.stream().map(value -> {
                    if (value != null) {
                        return suppressionString();
                    } else {
                        return null;
                    }
                }).collect(Collectors.toList());
            }
        }

        return values;
    }

    @Override
    public Map<String, List<String>> suppressValuesMap(Map<String, List<String>> properties) {
        if (properties == null) {
            throw new IllegalArgumentException("properties is null");
        }

        return properties.entrySet().stream().map(e -> entry(e.getKey(), suppressValues(e.getKey(), e.getValue())))
                .collect(nullSafeEntriesToMap());
    }

    @Override
    public boolean shouldSuppress(String property) {
        return configuredSuppressionPatterns().stream().anyMatch(property::matches);
    }

    @Override
    public String suppressionString() {
        return "*****";
    }

    @Override
    public Set<String> configuredSuppressionPatterns() {
        return Stream.of(getConfigurationService().getPropertyValueAsString(PROP_SUPPRESS_PATTERNS).split(","))
                .map(String::trim).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
    }

    private static <K, V> Map.Entry<K, V> entry(K key, V value) {
        return new AbstractMap.SimpleEntry<K, V>(key, value);
    }

    private static <K, U> Collector<Map.Entry<K, U>, ?, Map<K, U>> nullSafeEntriesToMap() {
        return nullSafeToMap(Map.Entry::getKey, Map.Entry::getValue);
    }

    private static <T, K, U> Collector<T, ?, Map<K, U>> nullSafeToMap(Function<? super T, ? extends K> keyMapper,
            Function<? super T, ? extends U> valueMapper) {
        return Collectors.collectingAndThen(Collectors.toList(), list -> {
            Map<K, U> result = new HashMap<>();
            for (T item : list) {
                K key = keyMapper.apply(item);
                if (result.putIfAbsent(key, valueMapper.apply(item)) != null) {
                    throw new IllegalStateException(String.format("Duplicate key %s", key));
                }
            }
            return result;
        });
    }

    public ConfigurationService getConfigurationService() {
        return configurationService;
    }

    public void setConfigurationService(ConfigurationService configurationService) {
        this.configurationService = configurationService;
    }
}