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

com.okta.sdk.impl.config.YAMLPropertiesSource Maven / Gradle / Ivy

/*
 * Copyright 2014 Stormpath, Inc.
 * Modifications Copyright 2018 Okta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.okta.sdk.impl.config;

import com.okta.commons.lang.Assert;
import com.okta.commons.lang.Classes;
import com.okta.commons.lang.Strings;
import com.okta.sdk.impl.io.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class YAMLPropertiesSource implements PropertiesSource {

    private static final Logger log = LoggerFactory.getLogger(YAMLPropertiesSource.class);
    private final Resource resource;

    public YAMLPropertiesSource(Resource resource) {
        Assert.notNull(resource, "resource argument cannot be null.");
        this.resource = resource;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map getProperties() {
        try (InputStream in = resource.getInputStream()) {
            // check to see if file exists
            if (in != null) { // if we have a yaml file.
                if (Classes.isAvailable("org.yaml.snakeyaml.Yaml")) {
                    Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
                    Map config = yaml.load(in);
                    return getFlattenedMap(config);
                } else {
                    log.warn("YAML not found in classpath, add 'org.yaml.snakeyaml' to support YAML configuration");
                }
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to read resource [" + resource + "]: " + e.getMessage(), e);
        }
        return new LinkedHashMap<>();
    }

    /**
     * Return a flattened version of the given map, recursively following any nested Map
     * or Collection values. Entries from the resulting map retain the same order as the
     * source.
     *
     * Copied from https://github.com/spring-projects/spring-framework/blob/master/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java
     *
     * @param source the source map
     * @return a flattened map
     */
    private Map getFlattenedMap(Map source) {
        Map result = new LinkedHashMap<>();
        buildFlattenedMap(result, source, null);
        return result;
    }

    private void buildFlattenedMap(Map result, Map source, String path) {
        for (Map.Entry entry : source.entrySet()) {
            String key = entry.getKey();
            if (Strings.hasText(path)) {
                if (key.startsWith("[")) {
                    key = path + key;
                }
                else {
                    key = path + "." + key;
                }
            }
            Object value = entry.getValue();
            if (value instanceof String) {
                result.put(key, String.valueOf(value));
            }
            else if (value instanceof Map) {
                // Need a compound key
                @SuppressWarnings("unchecked")
                Map map = (Map) value;
                buildFlattenedMap(result, map, key);
            }
            else if (value instanceof Collection) {
                // Need a compound key
                @SuppressWarnings("unchecked")
                Collection collection = (Collection) value;
                result.put(key, Strings.collectionToCommaDelimitedString(collection));
            }
            else {
                result.put(key, value != null ? String.valueOf(value) : "");
            }
        }
    }
}