Example usage for io.vertx.core.eventbus Message body

List of usage examples for io.vertx.core.eventbus Message body

Introduction

In this page you can find the example usage for io.vertx.core.eventbus Message body.

Prototype

@CacheReturn
T body();

Source Link

Document

The body of the message.

Usage

From source file:org.entcore.registry.controllers.AppRegistryController.java

License:Open Source License

@Put("/application")
public void recordApplication(final HttpServerRequest request) {
    if (("localhost:" + config.getInteger("port", 8012)).equalsIgnoreCase(request.headers().get("Host"))) {
        bodyToJson(request, new Handler<JsonObject>() {
            @Override// w  w  w .  j  av a2 s  .c  om
            public void handle(JsonObject jo) {
                eb.send(config.getString("address", "wse.app.registry"), jo,
                        handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
                            @Override
                            public void handle(Message<JsonObject> reply) {
                                renderJson(request, reply.body());
                            }
                        }));
            }
        });
    } else {
        forbidden(request, "invalid.host");
    }
}

From source file:org.entcore.registry.controllers.AppRegistryController.java

License:Open Source License

@BusAddress("wse.app.registry.applications")
public void applications(final Message<JsonObject> message) {
    String application = message.body().getString("application");
    if (application != null && !application.trim().isEmpty()) {
        String action = message.body().getString("action", "");
        Handler<Either<String, JsonArray>> responseHandler = new Handler<Either<String, JsonArray>>() {
            @Override//from   w  w  w. java 2 s  .co m
            public void handle(Either<String, JsonArray> res) {
                if (res.isRight()) {
                    message.reply(res.right().getValue());
                } else {
                    message.reply(new fr.wseduc.webutils.collections.JsonArray());
                }
            }
        };
        switch (action) {
        case "allowedUsers":
            appRegistryService.applicationAllowedUsers(application, message.body().getJsonArray("users"),
                    message.body().getJsonArray("groups"), responseHandler);
            break;
        case "allowedProfileGroups":
            appRegistryService.applicationAllowedProfileGroups(application, responseHandler);
            break;
        default:
            message.reply(new fr.wseduc.webutils.collections.JsonArray());
            break;
        }
    } else {
        message.reply(new fr.wseduc.webutils.collections.JsonArray());
    }
}

From source file:org.entcore.registry.controllers.AppRegistryController.java

License:Open Source License

@BusAddress("wse.app.registry.bus")
public void registryEventBusHandler(final Message<JsonObject> message) {
    final String structureId = message.body().getString("structureId");
    switch (message.body().getString("action", "")) {
    case "setDefaultClassRoles":
        appRegistryService.setDefaultClassRoles(message.body().getString("classId"),
                new Handler<Either<String, JsonObject>>() {
                    @Override// w  w w.jav a 2 s  .  c  o m
                    public void handle(Either<String, JsonObject> r) {
                        if (r.isRight()) {
                            message.reply(r.right().getValue());
                        } else {
                            message.reply(
                                    new JsonObject().put("status", "error").put("message", "invalid.classId"));
                        }
                    }
                });
        break;
    case "create-external-application":
        appRegistryService.createApplication(structureId, message.body().getJsonObject("application"), null,
                busResponseHandler(message));
        break;
    case "create-role":
        final JsonObject role = message.body().getJsonObject("role");
        final JsonArray actions = message.body().getJsonArray("actions");
        appRegistryService.createRole(structureId, role, actions, busResponseHandler(message));
        break;
    case "link-role-group":
        final String groupId = message.body().getString("groupId");
        final JsonArray roleIds = message.body().getJsonArray("roleIds");
        appRegistryService.linkRolesToGroup(groupId, roleIds, new Handler<Either<String, JsonObject>>() {
            @Override
            public void handle(Either<String, JsonObject> event) {
                if (event.isRight()) {
                    updatedProfileGroupActions(groupId);
                    message.reply(new JsonObject().put("status", "ok").put("result", event.right().getValue()));
                } else {
                    JsonObject error = new JsonObject().put("status", "error").put("message",
                            event.left().getValue());
                    message.reply(error);
                }
            }
        });
        break;
    case "list-groups-with-roles":
        boolean classGroups = message.body().getBoolean("classGroups", false);
        appRegistryService.listGroupsWithRoles(structureId, classGroups, busArrayHandler(message));
        break;
    case "list-roles":
        appRegistryService.listRoles(structureId, busArrayHandler(message));
        break;
    case "list-cas-connectors":
        appRegistryService.listCasConnectors(busArrayHandler(message));
        break;
    default:
        message.reply(new JsonObject().put("status", "error").put("message", "invalid.action"));
    }
}

From source file:org.entcore.registry.controllers.ExternalApplicationController.java

License:Open Source License

@BusAddress("external-application")
public void externalApplications(Message<JsonObject> message) {
    final String structureId = message.body().getString("structureId");
    switch (message.body().getString("action", "")) {
    case "list":
        externalAppService.listExternalApps(structureId, busArrayHandler(message));
        break;/*from  w w w . java 2  s  . c om*/
    default:
        message.reply(new JsonObject().put("status", "error").put("message", "invalid.action"));
    }
}

From source file:org.entcore.registry.controllers.WidgetController.java

License:Open Source License

@Post("/widget")
public void recordWidget(final HttpServerRequest request) {
    if (("localhost:" + config.getInteger("port", 8012)).equalsIgnoreCase(request.headers().get("Host"))) {
        bodyToJson(request, new Handler<JsonObject>() {
            @Override//from  w w w  .  j ava 2  s.c o m
            public void handle(JsonObject jo) {
                eb.send("wse.app.registry.widgets", jo,
                        handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
                            @Override
                            public void handle(Message<JsonObject> reply) {
                                renderJson(request, reply.body());
                            }
                        }));
            }
        });
    } else {
        forbidden(request, "invalid.host");
    }
}

From source file:org.entcore.registry.controllers.WidgetController.java

License:Open Source License

@BusAddress("wse.app.registry.widgets")
public void collectWidgets(final Message<JsonObject> message) {
    final JsonArray widgets = message.body().getJsonArray("widgets",
            new fr.wseduc.webutils.collections.JsonArray());
    if (widgets.size() == 0 && message.body().containsKey("widget")) {
        widgets.add(message.body().getJsonObject("widget"));
    } else if (widgets.size() == 0) {
        message.reply(new JsonObject().put("status", "error").put("message", "invalid.parameters"));
        return;/*from www  . j  ava 2s. c  o m*/
    }

    final AtomicInteger countdown = new AtomicInteger(widgets.size());
    final JsonObject reply = new JsonObject().put("status", "ok").put("errors",
            new fr.wseduc.webutils.collections.JsonArray());

    final Handler<JsonObject> replyHandler = new Handler<JsonObject>() {
        public void handle(JsonObject res) {
            if ("error".equals(res.getString("status"))) {
                reply.put("status", "error");
                reply.getJsonArray("errors").add(reply.getString("message"));
            }
            if (countdown.decrementAndGet() == 0) {
                message.reply(reply);
            }
        }
    };

    for (Object widgetObj : widgets) {
        registerWidget((JsonObject) widgetObj, replyHandler);
    }
}

From source file:org.entcore.registry.filters.AbstractFilter.java

License:Open Source License

private void check(final HttpServerRequest request, String query, JsonObject params,
        final Handler<Boolean> handler) {
    request.pause();//w  w w  .j  av a  2s . c o  m
    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            request.resume();
            JsonArray r = event.body().getJsonArray("result");
            handler.handle("ok".equals(event.body().getString("status")) && r != null && r.size() == 1
                    && r.getJsonObject(0).getBoolean("exists", false));
        }
    });
}

From source file:org.entcore.registry.filters.RoleGroupFilter.java

License:Open Source License

@Override
public void authorize(HttpServerRequest request, Binding binding, UserInfos user,
        final Handler<Boolean> handler) {
    Map<String, UserInfos.Function> functions = user.getFunctions();
    final String groupId = request.params().get("groupId");
    final String roleId = request.params().get("roleId");

    if (groupId == null || groupId.trim().isEmpty() || roleId == null || roleId.trim().isEmpty()) {
        handler.handle(false);/* www .  j  a v  a 2 s. co m*/
        return;
    }

    if (functions == null || functions.isEmpty()) {
        handler.handle(false);
        return;
    }
    if (functions.containsKey(DefaultFunctions.SUPER_ADMIN)) {
        handler.handle(true);
        return;
    }
    final UserInfos.Function adminLocal = functions.get(DefaultFunctions.ADMIN_LOCAL);
    if (adminLocal == null || adminLocal.getScope() == null) {
        handler.handle(false);
        return;
    }

    final JsonObject params = new JsonObject().put("groupId", groupId).put("roleId", roleId)
            .put("scopedStructures", new fr.wseduc.webutils.collections.JsonArray(adminLocal.getScope()));

    final String regularQuery = "MATCH (s:Structure)<-[:BELONGS*0..1]-()<-[:DEPENDS]-(:Group {id: {groupId}}), (r:Role) "
            + "WHERE s.id IN {scopedStructures} AND r.id = {roleId} "
            + "AND NOT((:Application {locked: true})-[:PROVIDE]->(:Action)<-[:AUTHORIZE]-(r)) "
            + "OPTIONAL MATCH (ext:Application:External)-[:PROVIDE]->(:Action)<-[:AUTHORIZE]-(r) "
            + "RETURN count(distinct r) = 1 as exists, count(distinct ext) as externalApps";
    final String externalQuery = "MATCH (r:Role {id : {roleId}}), "
            + "(ext:Application:External)-[:PROVIDE]->(:Action)<-[:AUTHORIZE]-(r), "
            + "(s:Structure)-[:HAS_ATTACHMENT*0..]->(p:Structure) "
            + "WHERE s.id IN {scopedStructures} AND p.id = ext.structureId AND (ext.inherits = true OR p = s) "
            + "RETURN (count(distinct r) = 1 AND count(distinct ext) = {nbExt}) as exists";

    neo4j.execute(regularQuery, params, new Handler<Message<JsonObject>>() {
        public void handle(Message<JsonObject> event) {
            JsonArray r = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && r != null && r.size() == 1) {
                boolean exists = r.getJsonObject(0).getBoolean("exists", false);
                int nbExt = r.getJsonObject(0).getInteger("nbExt", 0);
                if (!exists) {
                    handler.handle(false);
                } else if (nbExt == 0) {
                    handler.handle(true);
                } else {
                    neo4j.execute(externalQuery, params.put("nbExt", nbExt),
                            new Handler<Message<JsonObject>>() {
                                public void handle(Message<JsonObject> event) {
                                    JsonArray r = event.body().getJsonArray("result");
                                    if ("ok".equals(event.body().getString("status")) && r != null
                                            && r.size() == 1) {
                                        handler.handle(r.getJsonObject(0).getBoolean("exists", false));
                                    } else {
                                        handler.handle(false);
                                    }
                                }
                            });
                }
            } else {
                handler.handle(false);
            }

        }
    });

}

From source file:org.entcore.registry.filters.WidgetLinkFilter.java

License:Open Source License

@Override
public void authorize(HttpServerRequest request, Binding binding, UserInfos user,
        final Handler<Boolean> handler) {

    Map<String, UserInfos.Function> functions = user.getFunctions();

    if (functions == null || functions.isEmpty()) {
        handler.handle(false);//from w  w w . j a  v  a 2s .c om
        return;
    }
    if (functions.containsKey(DefaultFunctions.SUPER_ADMIN)) {
        handler.handle(true);
        return;
    }
    final UserInfos.Function adminLocal = functions.get(DefaultFunctions.ADMIN_LOCAL);
    if (adminLocal == null || adminLocal.getScope() == null) {
        handler.handle(false);
        return;
    }

    final String groupId = request.params().get("groupId");
    if (groupId == null || groupId.trim().isEmpty()) {
        handler.handle(false);
        return;
    }

    String query = "MATCH (s:Structure)<-[:BELONGS*0..1]-()<-[:DEPENDS]-(g:Group {id : {groupId}}) "
            + "WHERE s.id IN {adminScope} " + "RETURN count(g) = 1 as exists";
    JsonObject params = new JsonObject().put("groupId", groupId).put("adminScope",
            new fr.wseduc.webutils.collections.JsonArray(adminLocal.getScope()));

    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        public void handle(Message<JsonObject> event) {
            JsonArray r = event.body().getJsonArray("result");
            handler.handle("ok".equals(event.body().getString("status")) && r != null && r.size() == 1
                    && r.getJsonObject(0).getBoolean("exists", false));
        }
    });
}

From source file:org.entcore.registry.services.impl.DefaultAppRegistryService.java

License:Open Source License

@Override
public void createApplication(String structureId, JsonObject application, JsonArray actions,
        final Handler<Either<String, JsonObject>> handler) {
    if (defaultValidationParamsNull(handler, application, application.getString("name")))
        return;/*from   ww w  .  j  a  v  a  2s. c o m*/
    final String applicationName = application.getString("name");
    final String id = UUID.randomUUID().toString();
    final String address = application.getString("address");
    application.put("scope", new fr.wseduc.webutils.collections.JsonArray(
            "[\"" + application.getString("scope", "").replaceAll("\\s", "\",\"") + "\"]"));
    application.put("id", id);
    final String createApplicationQuery = "MATCH (n:Application) " + "WHERE n.name = {applicationName} "
            + "WITH count(*) AS exists " + "WHERE exists=0 " + "CREATE (m:Application {props}) "
            + "RETURN m.id as id";
    final JsonObject params = new JsonObject().put("applicationName", applicationName);
    if (structureId != null && !structureId.trim().isEmpty()) {
        application.put("structureId", structureId);
    }
    final StatementsBuilder b = new StatementsBuilder().add(createApplicationQuery,
            params.copy().put("props", application));
    if (actions != null && actions.size() > 0) {
        for (Object o : actions) {
            JsonObject json = (JsonObject) o;
            String type;
            List<String> removeLabels = new ArrayList<>();
            removeLabels.add("ResourceAction");
            removeLabels.add("AuthenticatedAction");
            removeLabels.add("WorkflowAction");
            switch (json.getString("type", "WORKFLOW")) {
            case "RESOURCE":
                type = "Resource";
                break;
            case "AUTHENTICATED":
                type = "Authenticated";
                break;
            default:
                type = "Workflow";
                break;
            }
            removeLabels.remove(type + "Action");
            String createAction = "MERGE (a:Action {name:{name}}) " + "REMOVE a:"
                    + Joiner.on(":").join(removeLabels) + " "
                    + "SET a.displayName = {displayName}, a.type = {type}, a:" + type + "Action " + "WITH a "
                    + "MATCH (n:Application) " + "WHERE n.name = {applicationName} "
                    + "CREATE UNIQUE n-[r:PROVIDE]->a " + "RETURN a.name as name";
            b.add(createAction, json.put("applicationName", applicationName).put("type",
                    "SECURED_ACTION_" + json.getString("type", "WORKFLOW")));
        }
        final String removeNotWorkflowInRole = "MATCH (:Role)-[r:AUTHORIZE]->(a:Action) "
                + "WHERE a:ResourceAction OR a:AuthenticatedAction " + "DELETE r";
        b.add(removeNotWorkflowInRole);
    } else if (address != null && !address.trim().isEmpty()) {
        String query2 = "MATCH (n:Application) " + "WHERE n.id = {id} "
                + "CREATE UNIQUE n-[r:PROVIDE]->(a:Action:WorkflowAction {type: {type}, "
                + "name:{name}, displayName:{displayName}}) " + "RETURN a.name as name";
        b.add(query2, new JsonObject().put("id", id).put("type", "SECURED_ACTION_WORKFLOW")
                .put("name", applicationName + "|address").put("displayName", applicationName + ".address"));
    }
    neo.executeTransaction(b.build(), null, true, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            JsonArray results = m.body().getJsonArray("results");
            if ("ok".equals(m.body().getString("status")) && results != null) {
                JsonArray r = results.getJsonArray(0);
                JsonObject j;
                if (r.size() > 0) {
                    j = r.getJsonObject(0);
                } else {
                    j = new JsonObject();
                }
                handler.handle(new Either.Right<String, JsonObject>(j));
            } else {
                handler.handle(new Either.Left<String, JsonObject>(m.body().getString("message")));
            }
        }
    });
}