Example usage for io.vertx.core.json JsonObject getJsonArray

List of usage examples for io.vertx.core.json JsonObject getJsonArray

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getJsonArray.

Prototype

public JsonArray getJsonArray(String key) 

Source Link

Document

Get the JsonArray value with the specified key

Usage

From source file:org.entcore.feeder.utils.Report.java

License:Open Source License

public void sendEmails(final Vertx vertx, final JsonObject config, String source) {
    final JsonArray sendReport = config.getJsonArray("sendReport");
    if (sendReport == null) {
        //log.error("Cannot send reports because of empty config: " + sendReport);
        return;//w w  w  .  ja v  a2 s . c  o m
    }
    int count = sendReport.size();
    EmailFactory emailFactory = new EmailFactory(vertx, config);
    for (Object o : sendReport) {
        JsonObject currentSendReport = (JsonObject) o;
        if (currentSendReport.getJsonArray("to") == null //
                || currentSendReport.getJsonArray("to").size() == 0 //
                || currentSendReport.getJsonArray("sources") == null//
                || !currentSendReport.getJsonArray("sources").contains(source)) {
            // log.error("Cannot send report because of missing infos: " + currentSendReport);
            continue;
        }
        if (count == 1) {
            this.countDiff(Optional.empty(), source, countEvent -> {
                if (countEvent != null) {
                    this.emailReport(vertx, emailFactory, currentSendReport, countEvent);
                }
            });
        } else {
            String prefixAcademy = currentSendReport.getString("academyPrefix");
            this.countDiff(Optional.ofNullable(prefixAcademy), source, countEvent -> {
                if (countEvent != null) {
                    this.emailReport(vertx, emailFactory, currentSendReport, countEvent);
                }
            });
        }
    }
}

From source file:org.entcore.feeder.utils.Validator.java

License:Open Source License

protected Validator(JsonObject schema, boolean notStoreLogins) {
    if (schema == null || schema.size() == 0) {
        throw new IllegalArgumentException("Missing schema.");
    }// w ww  .  ja va2 s  .  c  o m
    this.validate = schema.getJsonObject("validate");
    this.generate = schema.getJsonObject("generate");
    this.required = schema.getJsonArray("required");
    this.modifiable = schema.getJsonArray("modifiable");
    if (validate == null || generate == null || required == null || modifiable == null) {
        throw new IllegalArgumentException("Invalid schema.");
    }
    this.notStoreLogins = notStoreLogins;
}

From source file:org.entcore.feeder.utils.Validator.java

License:Open Source License

private String[] getParameters(JsonObject object, JsonObject j) {
    String[] v = null;//  ww w.  jav  a 2  s . c  om
    JsonArray args = j.getJsonArray("args");
    if (args != null && args.size() > 0) {
        v = new String[args.size()];
        for (int i = 0; i < args.size(); i++) {
            v[i] = object.getString(args.getString(i));
        }
    }
    return v;
}

From source file:org.entcore.feeder.utils.Validator.java

License:Open Source License

private String getParameter(JsonObject object, JsonObject j) {
    String v = null;/*www . ja v  a 2 s  .  c  o  m*/
    JsonArray args = j.getJsonArray("args");
    if (args != null && args.size() == 1) {
        v = object.getString(args.getString(0));
    }
    return v;
}

From source file:org.entcore.infra.Starter.java

License:Open Source License

private void loadInvalidEmails() {
    MapFactory.getClusterMap("invalidEmails", vertx, new Handler<AsyncMap<Object, Object>>() {
        @Override//ww  w . j av a2  s.  com
        public void handle(final AsyncMap<Object, Object> invalidEmails) {
            if (invalidEmails != null) {
                invalidEmails.size(new Handler<AsyncResult<Integer>>() {
                    @Override
                    public void handle(AsyncResult<Integer> event) {
                        if (event.succeeded() && event.result() < 1) {
                            MongoDb.getInstance().findOne(HardBounceTask.PLATEFORM_COLLECTION,
                                    new JsonObject().put("type", HardBounceTask.PLATFORM_ITEM_TYPE),
                                    new Handler<Message<JsonObject>>() {
                                        @Override
                                        public void handle(Message<JsonObject> event) {
                                            JsonObject res = event.body().getJsonObject("result");
                                            if ("ok".equals(event.body().getString("status")) && res != null
                                                    && res.getJsonArray("invalid-emails") != null) {
                                                for (Object o : res.getJsonArray("invalid-emails")) {
                                                    invalidEmails.put(o, "", new Handler<AsyncResult<Void>>() {
                                                        @Override
                                                        public void handle(AsyncResult<Void> event) {
                                                            if (event.failed()) {
                                                                log.error("Error adding invalid email.",
                                                                        event.cause());
                                                            }
                                                        }
                                                    });
                                                }
                                            } else {
                                                log.error(event.body().getString("message"));
                                            }
                                        }
                                    });
                        }
                    }
                });
            }
            EmailFactory emailFactory = new EmailFactory(vertx, config);
            try {
                new CronTrigger(vertx, config.getString("hard-bounces-cron", "0 0 7 * * ? *")).schedule(
                        new HardBounceTask(emailFactory.getSender(), config.getInteger("hard-bounces-day", -1),
                                new TimelineHelper(vertx, getEventBus(vertx), config), invalidEmails));
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
                vertx.close();
            }
        }
    });
}

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

License:Open Source License

@Post("/role")
@SecuredAction(value = "", type = ActionType.RESOURCE)
public void createRole(final HttpServerRequest request) {
    bodyToJson(request, new Handler<JsonObject>() {
        @Override//from w w w  . j  av a2s.co  m
        public void handle(JsonObject body) {
            final String roleName = body.getString("role");
            final JsonArray actions = body.getJsonArray("actions");
            if (actions != null && roleName != null && actions.size() > 0 && !roleName.trim().isEmpty()) {
                final JsonObject role = new JsonObject().put("name", roleName);
                String structureId = request.params().get("structureId");
                appRegistryService.createRole(structureId, role, actions,
                        notEmptyResponseHandler(request, 201, 409));
            } else {
                badRequest(request, "invalid.parameters");
            }
        }
    });
}

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

License:Open Source License

@Post("/authorize/group")
@SecuredAction(value = "", type = ActionType.RESOURCE)
@ResourceFilter(LinkRoleGroupFilter.class)
public void linkGroup(final HttpServerRequest request) {
    bodyToJson(request, new Handler<JsonObject>() {
        @Override// w w w  .j a v a  2 s  . c  o  m
        public void handle(JsonObject body) {
            final JsonArray roleIds = body.getJsonArray("roleIds");
            final String groupId = body.getString("groupId");
            if (roleIds != null && groupId != null && !groupId.trim().isEmpty()) {
                appRegistryService.linkRolesToGroup(groupId, roleIds,
                        new Handler<Either<String, JsonObject>>() {
                            @Override
                            public void handle(Either<String, JsonObject> event) {
                                if (event.isRight()) {
                                    updatedProfileGroupActions(groupId);
                                    renderJson(request, event.right().getValue());
                                } else {
                                    leftToResponse(request, event.left());
                                }
                            }
                        });
            } else {
                badRequest(request, "invalid.parameters");
            }
        }
    });
}

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;/*  ww  w . j  a v  a2 s .c  om*/
    }

    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.LinkRoleGroupFilter.java

License:Open Source License

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

    if (functions == null || functions.isEmpty()) {
        handler.handle(false);/*  www .j  a  v a  2s .  co  m*/
        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;
    }

    bodyToJson(resourceRequest, new Handler<JsonObject>() {
        @Override
        public void handle(JsonObject body) {
            final JsonArray roleIds = body.getJsonArray("roleIds");
            final String groupId = body.getString("groupId");
            JsonObject params = new JsonObject();
            params.put("structures", new fr.wseduc.webutils.collections.JsonArray(adminLocal.getScope()));
            if (roleIds != null && groupId != null && roleIds.size() > 0 && !groupId.trim().isEmpty()) {
                String query = "MATCH (s:Structure)<-[:BELONGS*0..1]-()<-[:DEPENDS]-(:Group {id : {groupId}}), (r:Role) "
                        + "WHERE s.id IN {structures} AND r.id IN {roles} AND (NOT(HAS(r.structureId)) OR r.structureId IN {structures}) "
                        + "RETURN count(distinct r) = {nb} as exists ";
                params.put("groupId", groupId);
                params.put("roles", roleIds);
                params.put("nb", roleIds.size());
                check(resourceRequest, query, params, handler);
            } else {
                handler.handle(false);
            }
        }
    });
}

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

License:Open Source License

@Override
public void getApplication(String applicationId, final Handler<Either<String, JsonObject>> handler) {
    String query = "MATCH (n:Application) " + "WHERE n.id = {id} " + "RETURN n.id as id, n.name as name, "
            + "n.grantType as grantType, n.secret as secret, n.address as address, "
            + "n.icon as icon, n.target as target, n.displayName as displayName, "
            + "n.scope as scope, n.pattern as pattern, n.casType as casType";
    JsonObject params = new JsonObject().put("id", applicationId);
    neo.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override// w w  w.  ja v a 2s . co  m
        public void handle(Message<JsonObject> res) {
            JsonArray r = res.body().getJsonArray("result");
            if (r != null && r.size() == 1) {
                JsonObject j = r.getJsonObject(0);
                JsonArray scope = j.getJsonArray("scope");
                if (scope != null && scope.size() > 0) {
                    j.put("scope", Joiner.on(" ").join(scope));
                } else {
                    j.put("scope", "");
                }
            }
            handler.handle(validUniqueResult(res));
        }
    });
}