Search in sources :

Example 1 with AttributeKey

use of org.keycloak.client.admin.cli.common.AttributeKey in project keycloak by keycloak.

the class ReflectionUtil method setAttributes.

public static void setAttributes(JsonNode client, List<AttributeOperation> attrs) {
    for (AttributeOperation item : attrs) {
        AttributeKey attr = item.getKey();
        JsonNode nested = client;
        List<AttributeKey.Component> cs = attr.getComponents();
        for (int i = 0; i < cs.size(); i++) {
            AttributeKey.Component c = cs.get(i);
            // if this is the last component of the name
            if (i == cs.size() - 1) {
                String val = item.getValue();
                ObjectNode obj = (ObjectNode) nested;
                if (SET == item.getType()) {
                    JsonNode valNode = valueToJsonNode(val);
                    if (c.isArray() || attr.isAppend()) {
                        JsonNode list = obj.get(c.getName());
                        // child expected to be an array
                        if (!(list instanceof ArrayNode)) {
                            // replace with new array
                            list = MAPPER.createArrayNode();
                            obj.set(c.getName(), list);
                        }
                        setArrayItem((ArrayNode) list, c.getIndex(), valNode);
                    } else {
                        ((ObjectNode) nested).set(c.getName(), valNode);
                    }
                } else {
                    // type == DELETE
                    if (c.isArray()) {
                        JsonNode list = obj.get(c.getName());
                        // child expected to be an array
                        if (list instanceof ArrayNode) {
                            removeArrayItem((ArrayNode) list, c.getIndex());
                        }
                    } else {
                        obj.remove(c.getName());
                    }
                }
            } else {
                // get child and
                // if exist set nested to child
                // else create new empty object or array - depending on c.isArray()
                JsonNode node = nested.get(c.getName());
                if (node == null) {
                    if (c.isArray()) {
                        node = MAPPER.createArrayNode();
                    } else {
                        node = MAPPER.createObjectNode();
                    }
                    ((ObjectNode) nested).set(c.getName(), node);
                }
                nested = node;
            }
        }
    }
}
Also used : AttributeKey(org.keycloak.client.admin.cli.common.AttributeKey) AttributeOperation(org.keycloak.client.admin.cli.common.AttributeOperation) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AttributeKey (org.keycloak.client.admin.cli.common.AttributeKey)1 AttributeOperation (org.keycloak.client.admin.cli.common.AttributeOperation)1