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

io.cloudslang.lang.commons.services.impl.SlangSourceServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.0.94
Show newest version
/*******************************************************************************
 * (c) Copyright 2016 Hewlett-Packard Development Company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *******************************************************************************/
package io.cloudslang.lang.commons.services.impl;

import io.cloudslang.lang.commons.services.api.SlangSourceService;
import io.cloudslang.lang.compiler.SlangTextualKeys;
import io.cloudslang.lang.entities.bindings.values.Value;
import io.cloudslang.lang.entities.bindings.values.ValueFactory;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

/**
 * @author Bonczidai Levente
 * @since 8/23/2016
 */
@Service
public class SlangSourceServiceImpl implements SlangSourceService {
    @Override
    public Map convertInputFromMap(Map rawMap, String artifact) {
        Map result = new HashMap<>();
        for (Map.Entry property : rawMap.entrySet()) {
            if (property.getValue() instanceof Map) {
                @SuppressWarnings("unchecked") Map valueMap = (Map) property.getValue();
                validateKeys(valueMap, artifact);
                result.put(
                        property.getKey(),
                        ValueFactory.create(valueMap.get(SlangTextualKeys.VALUE_KEY), isSensitiveValue(valueMap))
                );
            } else {
                result.put(property.getKey(), ValueFactory.create(property.getValue(), false));
            }
        }
        return result;
    }

    private void validateKeys(Map valueMap, String artifact) {
        List knownModifierKeys = Arrays.asList(SlangTextualKeys.SENSITIVE_KEY, SlangTextualKeys.VALUE_KEY);
        for (String modifierKey : valueMap.keySet()) {
            if (!knownModifierKeys.contains(modifierKey)) {
                throw new RuntimeException(
                        "Artifact {" + artifact + "} has unrecognized tag {" + modifierKey + "}" +
                                ". Please take a look at the supported features per versions link");
            }
        }
    }

    private Boolean isSensitiveValue(Map valueMap) {
        Boolean isSensitive = false;
        if (valueMap.get(SlangTextualKeys.SENSITIVE_KEY) instanceof Boolean) {
            isSensitive = (Boolean) valueMap.get(SlangTextualKeys.SENSITIVE_KEY);
        }
        return isSensitive;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy