Search in sources :

Example 1 with OverrideParameterException

use of org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException in project che-server by eclipse-che.

the class OverridePropertiesApplier method applyPropertiesOverride.

public JsonNode applyPropertiesOverride(JsonNode devfileNode, Map<String, String> overrideProperties) throws OverrideParameterException {
    for (Map.Entry<String, String> entry : overrideProperties.entrySet()) {
        // skip some segment
        if (skipJsonSegments.contains(entry.getKey())) {
            continue;
        }
        String[] pathSegments = parseSegments(entry.getKey());
        if (pathSegments.length < 1) {
            continue;
        }
        validateFirstSegment(pathSegments);
        String lastSegment = pathSegments[pathSegments.length - 1];
        JsonNode currentNode = devfileNode;
        Iterator<String> pathSegmentsIterator = Stream.of(pathSegments).iterator();
        do {
            String currentSegment = pathSegmentsIterator.next();
            JsonNode nextNode = currentNode.path(currentSegment);
            if (nextNode.isMissingNode() && pathSegmentsIterator.hasNext()) {
                // no such intermediate node, let's create it as a empty object
                currentNode = ((ObjectNode) currentNode).putObject(currentSegment);
                continue;
            } else if (nextNode.isArray()) {
                // and then try to retrieve it from array
                if (!pathSegmentsIterator.hasNext()) {
                    throw new OverrideParameterException(format("Override property reference '%s' points to an array type object. Please add an item qualifier by name.", entry.getKey()));
                }
                // retrieve object by name from array
                String arrayObjectName = pathSegmentsIterator.next();
                currentNode = findNodeByName((ArrayNode) nextNode, arrayObjectName, currentSegment);
                continue;
            } else {
                // so not set latest segment as an current node
                if (pathSegmentsIterator.hasNext()) {
                    currentNode = nextNode;
                }
            }
        } while (pathSegmentsIterator.hasNext());
        // end of path segments reached, now we can set value
        ((ObjectNode) currentNode).put(lastSegment, entry.getValue());
    }
    return devfileNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map) OverrideParameterException(org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException)

Example 2 with OverrideParameterException

use of org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException in project devspaces-images by redhat-developer.

the class OverridePropertiesApplier method applyPropertiesOverride.

public JsonNode applyPropertiesOverride(JsonNode devfileNode, Map<String, String> overrideProperties) throws OverrideParameterException {
    for (Map.Entry<String, String> entry : overrideProperties.entrySet()) {
        // skip some segment
        if (skipJsonSegments.contains(entry.getKey())) {
            continue;
        }
        String[] pathSegments = parseSegments(entry.getKey());
        if (pathSegments.length < 1) {
            continue;
        }
        validateFirstSegment(pathSegments);
        String lastSegment = pathSegments[pathSegments.length - 1];
        JsonNode currentNode = devfileNode;
        Iterator<String> pathSegmentsIterator = Stream.of(pathSegments).iterator();
        do {
            String currentSegment = pathSegmentsIterator.next();
            JsonNode nextNode = currentNode.path(currentSegment);
            if (nextNode.isMissingNode() && pathSegmentsIterator.hasNext()) {
                // no such intermediate node, let's create it as a empty object
                currentNode = ((ObjectNode) currentNode).putObject(currentSegment);
                continue;
            } else if (nextNode.isArray()) {
                // and then try to retrieve it from array
                if (!pathSegmentsIterator.hasNext()) {
                    throw new OverrideParameterException(format("Override property reference '%s' points to an array type object. Please add an item qualifier by name.", entry.getKey()));
                }
                // retrieve object by name from array
                String arrayObjectName = pathSegmentsIterator.next();
                currentNode = findNodeByName((ArrayNode) nextNode, arrayObjectName, currentSegment);
                continue;
            } else {
                // so not set latest segment as an current node
                if (pathSegmentsIterator.hasNext()) {
                    currentNode = nextNode;
                }
            }
        } while (pathSegmentsIterator.hasNext());
        // end of path segments reached, now we can set value
        ((ObjectNode) currentNode).put(lastSegment, entry.getValue());
    }
    return devfileNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map) OverrideParameterException(org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Map (java.util.Map)2 OverrideParameterException (org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException)2