Example usage for io.vertx.core.json JsonArray add

List of usage examples for io.vertx.core.json JsonArray add

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray add.

Prototype

public JsonArray add(Object value) 

Source Link

Document

Add an Object to the JSON array.

Usage

From source file:io.fabric8.devops.apps.elasticsearch.helper.service.ElasticSearchOptionsConverter.java

License:Apache License

public static void toJson(ElasticSearchOptions obj, JsonObject json) {
    if (obj.getConfigMap() != null) {
        json.put("configMap", obj.getConfigMap());
    }//  w w  w.  j av  a2  s  . com
    json.put("configMapScanPeriod", obj.getConfigMapScanPeriod());
    if (obj.getHost() != null) {
        json.put("host", obj.getHost());
    }
    if (obj.getIndexes() != null) {
        JsonArray array = new JsonArray();
        obj.getIndexes().forEach(item -> array.add(item));
        json.put("indexes", array);
    }
    if (obj.getKubernetesNamespace() != null) {
        json.put("kubernetesNamespace", obj.getKubernetesNamespace());
    }
    json.put("port", obj.getPort());
    json.put("ssl", obj.isSsl());
}

From source file:io.flowly.auth.manager.BaseManager.java

License:Open Source License

/**
 * Add distinct groups to the memberships array.
 *
 * @param groupVertices vertices that represent groups in the auth graph.
 * @param memberships JSON array to which the group is to be added.
 * @param uniqueId unique id that identifies a user or group. Can be null.
 * @param idRepresentsUser indicates if the unique id argument represents a user or a group.
 * @param includePermissions indicates if the permissions granted to the group are to be retrieved.
 *///from  w w w .ja  v  a2  s  .  c  o m
private void getDistinctMemberships(List<Vertex> groupVertices, JsonArray memberships, String uniqueId,
        boolean idRepresentsUser, boolean includePermissions) {
    Set<Object> groupIds = new HashSet<>();

    for (Vertex groupVertex : groupVertices) {
        if (!groupIds.contains(groupVertex.id())) {
            JsonObject group = makeGroupObject(groupVertex, uniqueId, idRepresentsUser);
            memberships.add(group);

            if (includePermissions) {
                getDirectPermissions(groupVertex, group);
            }

            groupIds.add(groupVertex.id());
        }
    }
}

From source file:io.flowly.auth.manager.GroupManager.java

License:Open Source License

/**
 * Create a group node in the auth graph.
 *
 * @param group JSON object representing a group's attributes, permissions, users and memberships.
 *              Ex: {//ww w  . j a va 2s . c  o  m
 *                  "groupId": "Group 1",
 *                  "usersToAdd": [
 *                      13,
 *                      14
 *                  ],
 *                  "groupsToAdd": [
 *                      12343,
 *                      34567
 *                  ],
 *                  "permissionsToAdd": [
 *                      {
 *                          "resourceVertexId": 555611
 *                          "rwx": 7
 *                      }
 *                  ]
 *              }
 * @return an empty list or a list of validation errors based on whether the group was created or not.
 */
@Override
public JsonArray create(JsonObject group) {
    Group newGroup = new Group(group);
    JsonArray errors = newGroup.validate();

    if (errors.size() == 0) {
        try {
            Vertex groupVertex = graph.addVertex(T.label, Schema.V_GROUP, Schema.V_P_GROUP_ID,
                    newGroup.getGroupId());
            setPropertyValue(groupVertex, Schema.V_P_DESCRIPTION, newGroup.getDescription());

            grantMemberships(groupVertex, true, group.getJsonArray(Group.USERS_TO_ADD), null);
            grantMemberships(groupVertex, false, group.getJsonArray(User.GROUPS_TO_ADD), null);
            grantPermissions(groupVertex, group.getJsonArray(Permission.PERMISSIONS_TO_ADD), null);

            commit();
        } catch (Exception ex) {
            rollback();
            String error = "Unable to create group: " + newGroup.getGroupId();
            logger.error(error, ex);
            errors.add(error);
        }
    }

    return errors;
}

From source file:io.flowly.auth.manager.GroupManager.java

License:Open Source License

/**
 * Update a group node in the auth graph.
 *
 * @param group JSON object representing a group's attributes, permissions, users and memberships.
 *              Ex: {//from  w  w w .j  av  a 2s  .  c  o  m
 *                  "id": 12345,
 *                  "description": "Represents flowly users.",
 *                  "usersToRemove": [
 *                      14
 *                  ],
 *                  "groupsToAdd": [
 *                      34567
 *                  ],
 *                  "groupsToRemove": [
 *                      12343
 *                  ],
 *                  "permissionsToRemove": [
 *                      555611
 *                  ]
 *              }
 * @return an empty list or a list of validation errors based on whether the group was updated or not.
 */
@Override
public JsonArray update(JsonObject group) {
    Group updatedGroup = new Group(group);
    JsonArray errors = updatedGroup.validate(true);

    if (errors.size() == 0) {
        try {
            Vertex groupVertex = getVertex(updatedGroup.getId());
            setGroupId(groupVertex, updatedGroup, true);
            setPropertyValue(groupVertex, Schema.V_P_DESCRIPTION, updatedGroup.getDescription());

            // Update group memberships.
            redoMemberships(groupVertex, false, updatedGroup.getJsonArray(User.GROUPS_TO_ADD),
                    updatedGroup.getJsonArray(User.GROUPS_TO_REMOVE));

            // Update group users.
            redoMemberships(groupVertex, true, updatedGroup.getJsonArray(Group.USERS_TO_ADD),
                    updatedGroup.getJsonArray(Group.USERS_TO_REMOVE));

            // Update permissions.
            redoPermissions(groupVertex, group);

            commit();
        } catch (IllegalArgumentException ex) {
            rollback();
            errors.add(ex.getMessage());
            logger.error(ex);
        } catch (Exception ex) {
            rollback();
            String error = "Unable to update group: " + updatedGroup.getGroupId();
            logger.error(error, ex);
            errors.add(error);
        }
    }

    return errors;
}

From source file:io.flowly.auth.manager.GroupManager.java

License:Open Source License

private void getMembers(Vertex groupVertex, JsonObject group, boolean includeDirectMembers,
        boolean includeEffectiveUsers) {
    if (includeDirectMembers) {
        JsonArray directMembers = new JsonArray();
        group.put(Group.DIRECT_MEMBERS, directMembers);

        graph.traversal().V(groupVertex).outE(Schema.E_MEMBER).inV().sideEffect(m -> {
            Vertex vertex = m.get();// ww w . ja va 2 s  .co m
            if (vertex.label().equals(Schema.V_USER)) {
                directMembers.add(makeUserObject(vertex));
            } else {
                directMembers.add(makeGroupObject(vertex, null, false));
            }

        }).toList();
    }

    // Recursively retrieve all unique users.
    if (includeEffectiveUsers) {
        JsonArray allUsers = new JsonArray();
        group.put(Group.ALL_USERS, allUsers);

        getUsers(groupVertex, allUsers, new HashSet<>());
    }
}

From source file:io.flowly.auth.manager.GroupManager.java

License:Open Source License

private void getUsers(Vertex groupVertex, JsonArray users, Set<Long> userIds) {
    graph.traversal().V(groupVertex).repeat(__.outE(Schema.E_MEMBER).inV()).emit().sideEffect(m -> {
        Vertex vertex = m.get();//  ww w . jav a  2s .  com

        if (vertex.label().equals(Schema.V_USER) && !userIds.contains(vertex.id())) {
            users.add(makeUserObject(vertex));
            userIds.add((Long) vertex.id());
        } else {
            getUsers(vertex, users, userIds);
        }

    }).toList();
}

From source file:io.flowly.auth.manager.ResourceManager.java

License:Open Source License

/**
 * Create a resource node in the auth graph.
 *
 * @param resource JSON object representing the resource attributes.
 *                 Ex: {/*from   ww w. ja v a  2s  .  com*/
 *                     "resourceId": "Studio",
 *                     "description": "Allows users to design and develop flowly apps."
 *                 }
 * @return an empty list or a list of validation errors based on whether the resource was created or not.
 */
@Override
public JsonArray create(JsonObject resource) {
    Resource newResource = new Resource(resource);
    JsonArray errors = newResource.validate();

    if (errors.size() == 0) {
        try {
            Vertex resourceVertex = graph.addVertex(T.label, Schema.V_RESOURCE, Schema.V_P_RESOURCE_ID,
                    newResource.getResourceId());
            setPropertyValue(resourceVertex, Schema.V_P_DESCRIPTION, newResource.getDescription());

            commit();
        } catch (Exception ex) {
            rollback();
            String error = "Unable to create resource: " + newResource.getResourceId();
            logger.error(error, ex);
            errors.add(error);
        }
    }

    return errors;
}

From source file:io.flowly.auth.manager.ResourceManager.java

License:Open Source License

/**
 * Update the attributes of a resource in the auth graph.
 *
 * @param resource JSON object representing the resource attributes.
 *                 Ex: {/* w  w  w  .j  a  va  2 s  .c o  m*/
 *                     "id": 12345,
 *                     "resourceId": "Studio",
 *                     "description": "Allows users to design and develop flowly apps."
 *                 }
 *
 * @return an empty list or a list of validation errors based on whether the resource was updated or not.
 */
@Override
public JsonArray update(JsonObject resource) {
    Resource newResource = new Resource(resource);
    JsonArray errors = newResource.validate(true);

    if (errors.size() == 0) {
        try {
            Vertex resourceVertex = getVertex(newResource.getId());
            setPropertyValue(resourceVertex, Schema.V_P_RESOURCE_ID, newResource.getResourceId());
            setPropertyValue(resourceVertex, Schema.V_P_DESCRIPTION, newResource.getDescription());

            commit();
        } catch (Exception ex) {
            rollback();
            String error = "Unable to update resource: " + newResource.getResourceId();
            logger.error(error, ex);
            errors.add(error);
        }
    }

    return errors;
}

From source file:io.flowly.auth.manager.UserManager.java

License:Open Source License

/**
 * Create a user node in the auth graph.
 * If this is an internally managed user, the provided password will be hashed before storing in the graph.
 * If group memberships are specified, add user to the groups.
 * If specified, grant permissions on resources (direct edge between user and resource).
 *
 * @param user JSON object representing the user attributes, memberships and permissions.
 *             Ex: {/*w  ww.  j a  v a  2 s.  c om*/
 *                 "userId": "aragorn",
 *                 "firstName": "First",
 *                 "lastName": "Last",
 *                 "isInternal": false,
 *                 "groupsToAdd": [
 *                     12343,
 *                     34567,
 *                     99901
 *                 ],
 *                 "permissionsToAdd": [
 *                     {
 *                         "resourceVertexId": 555611
 *                         "rwx": 7
 *                     }
 *                 ]
 *             }
 * @return an empty list or a list of validation errors based on whether the user was created or not.
 */
@Override
public JsonArray create(JsonObject user) {
    User newUser = new User(user);
    JsonArray errors = newUser.validate();

    if (errors.size() == 0) {
        try {
            Vertex userVertex = graph.addVertex(Schema.V_USER);
            setUserAttributes(userVertex, newUser, false);
            grantMemberships(userVertex, false, newUser.getGroupsToAdd(), null);
            grantPermissions(userVertex, newUser.getPermissionsToAdd(), null);

            commit();
        } catch (Exception ex) {
            rollback();
            String error = "Unable to create user: " + newUser.getUserId();
            logger.error(error, ex);
            errors.add(error);
        }
    }

    return errors;
}

From source file:io.flowly.auth.manager.UserManager.java

License:Open Source License

/**
 * Update the specified user attributes in the auth graph.
 * If specified, update user memberships.
 * If specified, update user permissions on given resources.
 *
 * @param user JSON object representing the user attributes, memberships and permissions.
 *             Ex: {// w w w .  jav a  2s. co  m
 *                 "id": 999123,
 *                 "middleName": "C",
 *                 "password": "!234aCbbJk_#3"
 *                 "groupsToAdd": [
 *                     34567
 *                 ],
 *                 "groupsToRemove": [
 *                     12345
 *                 ],
 *                 "permissionsToUpdate": [
 *                     {
 *                         "resourceVertexId": 555611
 *                         "rwx": 6
 *                     }
 *                 ]
 *             }
 * @return an empty list or a list of validation errors based on whether the user was updated or not.
 */
@Override
public JsonArray update(JsonObject user) {
    User updatedUser = new User(user);
    JsonArray errors = updatedUser.validate(true);

    try {
        Vertex userVertex = getVertex(updatedUser.getId());

        if (userVertex != null) {
            setUserAttributes(userVertex, updatedUser, true);
            redoPermissions(userVertex, user);
            redoMemberships(userVertex, false, updatedUser.getGroupsToAdd(), updatedUser.getGroupsToRemove());
        } else {
            errors.add("User does not exist: " + updatedUser.getId());
        }

        commit();
    } catch (IllegalArgumentException ex) {
        rollback();
        errors.add(ex.getMessage());
        logger.error(ex);
    } catch (Exception ex) {
        rollback();
        String error = "Unable to update user: " + updatedUser.getId();
        logger.error(error, ex);
        errors.add(error);
    }

    return errors;
}