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

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Get the number of values in this JSON array

Usage

From source file:org.entcore.archive.services.impl.DeleteOldExports.java

License:Open Source License

@Override
public void handle(Long event) {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.HOUR, -delay);
    final JsonObject query = new JsonObject().put("date",
            new JsonObject().put("$lt", new JsonObject().put("$date", c.getTime().getTime())));
    mongo.find(Archive.ARCHIVES, query, new Handler<Message<JsonObject>>() {
        @Override//  w w w.j av  a 2 s  . c  om
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("results");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() > 0) {
                JsonArray ids = new fr.wseduc.webutils.collections.JsonArray();
                for (Object object : res) {
                    if (!(object instanceof JsonObject))
                        continue;
                    ids.add(((JsonObject) object).getString("file_id"));
                }
                storage.removeFiles(ids, new Handler<JsonObject>() {
                    @Override
                    public void handle(JsonObject event) {
                        mongo.delete(Archive.ARCHIVES, query);
                    }
                });
            }
        }
    });
}

From source file:org.entcore.archive.services.impl.FileSystemExportService.java

License:Open Source License

private void sendExportEmail(final String exportId, final String locale, final String status,
        final String host) {
    final String userId = getUserId(exportId);
    String query = "MATCH (u:User {id : {userId}}) RETURN u.email as email ";
    JsonObject params = new JsonObject().put("userId", userId);
    Neo4j.getInstance().execute(query, params, new Handler<Message<JsonObject>>() {
        @Override//  w  w  w . j  a v a 2 s  . c  o m
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 1) {
                JsonObject e = res.getJsonObject(0);
                String email = e.getString("email");
                if (email != null && !email.trim().isEmpty()) {
                    HttpServerRequest r = new JsonHttpServerRequest(
                            new JsonObject().put("headers", new JsonObject().put("Accept-Language", locale)));
                    String subject, template;
                    JsonObject p = new JsonObject();
                    if ("ok".equals(status)) {
                        subject = "email.export.ok";
                        template = "email/export.ok.html";
                        p.put("download", host + "/archive/export/" + exportId);
                        if (log.isDebugEnabled()) {
                            log.debug(host + "/archive/export/" + exportId);
                        }
                    } else {
                        subject = "email.export.ko";
                        template = "email/export.ko.html";
                    }
                    notification.sendEmail(r, email, null, null, subject, template, p, true,
                            handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
                                @Override
                                public void handle(Message<JsonObject> event) {
                                    if (event == null || !"ok".equals(event.body().getString("status"))) {
                                        log.error("Error sending export email for user " + userId);
                                    }
                                }
                            }));
                } else {
                    log.info("User " + userId + " hasn't email.");
                }
            } else if (res != null) {
                log.warn("User " + userId + " not found.");
            } else {
                log.error("Error finding user " + userId + " email : " + event.body().getString("message"));
            }
        }
    });
}

From source file:org.entcore.auth.adapter.UserInfoAdapterV1_0Json.java

License:Open Source License

protected JsonObject getCommonFilteredInfos(JsonObject info, String clientId) {
    JsonObject filteredInfos = info.copy();
    String type = Utils.getOrElse(types.get(info.getString("type", "")), "");
    filteredInfos.put("type", type);
    filteredInfos.remove("cache");
    if (filteredInfos.getString("level") == null) {
        filteredInfos.put("level", "");
    } else if (filteredInfos.getString("level").contains("$")) {
        String[] level = filteredInfos.getString("level").split("\\$");
        filteredInfos.put("level", level[level.length - 1]);
    }//from  w ww . j  a va  2  s .  c o m
    if (clientId != null && !clientId.trim().isEmpty()) {
        JsonArray classNames = filteredInfos.getJsonArray("classNames");
        filteredInfos.remove("classNames");
        JsonArray structureNames = filteredInfos.getJsonArray("structureNames");
        filteredInfos.remove("structureNames");
        filteredInfos.remove("federated");
        if (classNames != null && classNames.size() > 0) {
            filteredInfos.put("classId", classNames.getString(0));
        }
        if (structureNames != null && structureNames.size() > 0) {
            filteredInfos.put("schoolName", structureNames.getString(0));
        }
        filteredInfos.remove("functions");
        filteredInfos.remove("groupsIds");
        filteredInfos.remove("structures");
        filteredInfos.remove("classes");
        filteredInfos.remove("apps");
        filteredInfos.remove("authorizedActions");
        filteredInfos.remove("children");
        JsonArray authorizedActions = new fr.wseduc.webutils.collections.JsonArray();
        for (Object o : info.getJsonArray("authorizedActions")) {
            JsonObject j = (JsonObject) o;
            String name = j.getString("name");
            if (name != null && name.startsWith(clientId + "|")) {
                authorizedActions.add(j);
            }
        }
        if (authorizedActions.size() > 0) {
            filteredInfos.put("authorizedActions", authorizedActions);
        }
    }
    return filteredInfos;
}

From source file:org.entcore.auth.Auth.java

License:Open Source License

@Override
public void start() throws Exception {
    final EventBus eb = getEventBus(vertx);
    super.start();
    setDefaultResourceFilter(new AuthResourcesProvider(new Neo(vertx, eb, null)));

    final UserAuthAccount userAuthAccount = new DefaultUserAuthAccount(vertx, config);
    final EventStore eventStore = EventStoreFactory.getFactory().getEventStore(Auth.class.getSimpleName());

    AuthController authController = new AuthController();
    authController.setEventStore(eventStore);
    authController.setUserAuthAccount(userAuthAccount);
    addController(authController);//from   w ww . ja v  a2 s.  c om

    final ConfigurationController configurationController = new ConfigurationController();
    configurationController.setConfigurationService(new DefaultConfigurationService());
    addController(configurationController);

    final String samlMetadataFolder = config.getString("saml-metadata-folder");
    if (samlMetadataFolder != null && !samlMetadataFolder.trim().isEmpty()) {
        vertx.fileSystem().readDir(samlMetadataFolder, new Handler<AsyncResult<List<String>>>() {
            @Override
            public void handle(AsyncResult<List<String>> event) {
                if (event.succeeded() && event.result().size() > 0) {
                    try {
                        SamlController samlController = new SamlController();
                        JsonObject conf = config;

                        vertx.deployVerticle(SamlValidator.class,
                                new DeploymentOptions().setConfig(conf).setWorker(true));
                        samlController.setEventStore(eventStore);
                        samlController.setUserAuthAccount(userAuthAccount);
                        samlController.setServiceProviderFactory(new DefaultServiceProviderFactory(
                                config.getJsonObject("saml-services-providers")));
                        samlController
                                .setSignKey((String) vertx.sharedData().getLocalMap("server").get("signKey"));
                        samlController.setSamlWayfParams(config.getJsonObject("saml-wayf"));
                        samlController.setIgnoreCallBackPattern(config.getString("ignoreCallBackPattern"));
                        addController(samlController);
                        LocalMap<Object, Object> server = vertx.sharedData().getLocalMap("server");
                        if (server != null) {
                            String loginUri = config.getString("loginUri");
                            String callbackParam = config.getString("callbackParam");
                            if (loginUri != null && !loginUri.trim().isEmpty()) {
                                server.putIfAbsent("loginUri", loginUri);
                            }
                            if (callbackParam != null && !callbackParam.trim().isEmpty()) {
                                server.putIfAbsent("callbackParam", callbackParam);
                            }
                            final JsonObject authLocations = config.getJsonObject("authLocations");
                            if (authLocations != null && authLocations.size() > 0) {
                                server.putIfAbsent("authLocations", authLocations.encode());
                            }
                        }
                    } catch (ConfigurationException e) {
                        log.error("Saml loading error.", e);
                    }
                }
            }
        });
    }
    final JsonObject openidFederate = config.getJsonObject("openid-federate");
    final JsonObject openidConnect = config.getJsonObject("openid-connect");
    final OpenIdConnectController openIdConnectController;
    if (openidFederate != null || openidConnect != null) {
        openIdConnectController = new OpenIdConnectController();
        addController(openIdConnectController);
    } else {
        openIdConnectController = null;
    }
    if (openidConnect != null) {
        final String certsPath = openidConnect.getString("certs");
        if (isNotEmpty(certsPath)) {
            JWT.listCertificates(vertx, certsPath, new Handler<JsonObject>() {
                @Override
                public void handle(JsonObject certs) {
                    openIdConnectController.setCertificates(certs);
                }
            });
        }
    }
    if (openidFederate != null) {
        openIdConnectController.setEventStore(eventStore);
        openIdConnectController.setUserAuthAccount(userAuthAccount);
        openIdConnectController.setSignKey((String) vertx.sharedData().getLocalMap("server").get("signKey"));
        openIdConnectController.setOpenIdConnectServiceProviderFactory(
                new DefaultOpenIdServiceProviderFactory(vertx, openidFederate.getJsonObject("domains")));
        openIdConnectController.setSubMapping(openidFederate.getBoolean("authorizeSubMapping", false));

        final JsonArray authorizedHostsLogin = openidFederate.getJsonArray("authorizedHostsLogin");
        if (authorizedHostsLogin != null && authorizedHostsLogin.size() > 0) {
            authController.setAuthorizedHostsLogin(authorizedHostsLogin);
        }
    }
}

From source file:org.entcore.auth.controllers.AuthController.java

License:Open Source License

@Post("/forgot-id")
public void forgetId(final HttpServerRequest request) {
    RequestUtils.bodyToJson(request, new io.vertx.core.Handler<JsonObject>() {
        public void handle(JsonObject data) {
            final String mail = data.getString("mail");
            final String service = data.getString("service");
            final String firstName = data.getString("firstName");
            final String structure = data.getString("structureId");
            if (mail == null || mail.trim().isEmpty()) {
                badRequest(request);/*ww w. j  a va2s .  com*/
                return;
            }
            userAuthAccount.findByMailAndFirstNameAndStructure(mail, firstName, structure,
                    new io.vertx.core.Handler<Either<String, JsonArray>>() {
                        @Override
                        public void handle(Either<String, JsonArray> event) {
                            //No user with that email, or more than one found.
                            if (event.isLeft()) {
                                badRequest(request, event.left().getValue());
                                return;
                            }
                            JsonArray results = event.right().getValue();
                            if (results.size() == 0) {
                                badRequest(request, "no.match");
                                return;
                            }
                            JsonArray structures = new fr.wseduc.webutils.collections.JsonArray();
                            if (results.size() > 1) {
                                for (Object ob : results) {
                                    JsonObject j = (JsonObject) ob;
                                    j.remove("login");
                                    j.remove("mobile");
                                    if (!structures.toString().contains(j.getString("structureId")))
                                        structures.add(j);
                                }
                                if (firstName != null && structures.size() == 1)
                                    badRequest(request, "non.unique.result");
                                else
                                    renderJson(request, new JsonObject().put("structures", structures));
                                return;
                            }
                            JsonObject match = results.getJsonObject(0);
                            final String id = match.getString("login", "");
                            final String mobile = match.getString("mobile", "");

                            //Force mail
                            if ("mail".equals(service)) {
                                userAuthAccount.sendForgottenIdMail(request, id, mail,
                                        new io.vertx.core.Handler<Either<String, JsonObject>>() {
                                            public void handle(Either<String, JsonObject> event) {
                                                if (event.isLeft()) {
                                                    badRequest(request, event.left().getValue());
                                                    return;
                                                }
                                                if (smsProvider != null && !smsProvider.isEmpty()) {
                                                    final String obfuscatedMobile = StringValidation
                                                            .obfuscateMobile(mobile);
                                                    renderJson(request,
                                                            new JsonObject().put("mobile", obfuscatedMobile));
                                                } else {
                                                    renderJson(request, new JsonObject());
                                                }
                                            }
                                        });
                            } else if ("mobile".equals(service) && !mobile.isEmpty() && smsProvider != null
                                    && !smsProvider.isEmpty()) {
                                eventStore.createAndStoreEvent(AuthEvent.SMS.name(), id);
                                userAuthAccount.sendForgottenIdSms(request, id, mobile,
                                        DefaultResponseHandler.defaultResponseHandler(request));
                            } else {
                                badRequest(request);
                            }
                        }
                    });
        }
    });
}

From source file:org.entcore.auth.oauth.OAuthDataHandler.java

License:Open Source License

@Override
public void validateClient(String clientId, String clientSecret, String grantType,
        final Handler<Boolean> handler) {

    String query = "MATCH (n:Application) " + "WHERE n.name = {clientId} " + "AND n.secret = {secret} ";
    if (!"refresh_token".equals(grantType)) {
        query += " AND n.grantType = {grantType} ";
    }//from   w  ww .  j  a va 2s .co m
    query += "RETURN count(n) as nb";
    Map<String, Object> params = new HashMap<>();
    params.put("clientId", clientId);
    params.put("secret", clientSecret);
    params.put("grantType", grantType);
    neo.execute(query, params, new io.vertx.core.Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> res) {
            JsonArray a = res.body().getJsonArray("result");
            if ("ok".equals(res.body().getString("status")) && a != null && a.size() == 1) {
                JsonObject r = a.getJsonObject(0);
                handler.handle(r != null && r.getInteger("nb") == 1);
            } else {
                handler.handle(false);
            }
        }
    });

}

From source file:org.entcore.auth.oauth.OAuthDataHandler.java

License:Open Source License

@Override
public void getUserId(final String username, final String password, final Handler<String> handler) {
    if (username != null && password != null && !username.trim().isEmpty() && !password.trim().isEmpty()) {
        String query = "MATCH (n:User) " + "WHERE n.login={login} AND NOT(n.password IS NULL) "
                + "AND (NOT(HAS(n.blocked)) OR n.blocked = false) ";
        if (checkFederatedLogin) {
            query += "AND (NOT(HAS(n.federated)) OR n.federated = false) ";
        }//from   www  .j a va  2 s .  co  m
        query += "OPTIONAL MATCH (p:Profile) " + "WHERE HAS(n.profiles) AND p.name = head(n.profiles) "
                + "RETURN DISTINCT n.id as userId, n.password as password, p.blocked as blockedProfile";
        Map<String, Object> params = new HashMap<>();
        params.put("login", username);
        neo.execute(query, params, new io.vertx.core.Handler<Message<JsonObject>>() {

            @Override
            public void handle(Message<JsonObject> res) {
                JsonArray result = res.body().getJsonArray("result");
                if ("ok".equals(res.body().getString("status")) && result != null && result.size() == 1) {
                    checkPassword(result, password, username, handler);
                } else {
                    getUserIdByLoginAlias(username, password, handler);
                }
            }
        });
    } else {
        handler.handle(null);
    }
}

From source file:org.entcore.auth.oauth.OAuthDataHandler.java

License:Open Source License

public void createOrUpdateAuthInfo(final String clientId, final String userId, final String scope,
        final String redirectUri, final Handler<AuthInfo> handler) {
    if (clientId != null && userId != null && !clientId.trim().isEmpty() && !userId.trim().isEmpty()) {
        if (scope != null && !scope.trim().isEmpty()) {
            String query = "MATCH (app:`Application` {name:{clientId}}) RETURN app.scope as scope";
            neo.execute(query, new JsonObject().put("clientId", clientId),
                    new io.vertx.core.Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> res) {
                            JsonArray r = res.body().getJsonArray("result");

                            if ("ok".equals(res.body().getString("status")) && r != null && r.size() == 1) {
                                JsonObject j = r.getJsonObject(0);
                                if (j != null && j
                                        .getJsonArray("scope", new fr.wseduc.webutils.collections.JsonArray())
                                        .getList().containsAll(Arrays.asList(scope.split("\\s")))) {
                                    createAuthInfo(clientId, userId, scope, redirectUri, handler);
                                } else {
                                    handler.handle(null);
                                }//from ww  w  .j  a  v  a2s  .c om
                            } else {
                                handler.handle(null);
                            }
                        }
                    });
        } else {
            createAuthInfo(clientId, userId, scope, redirectUri, handler);
        }
    } else {
        handler.handle(null);
    }
}

From source file:org.entcore.auth.oauth.OAuthDataHandler.java

License:Open Source License

@Override
public void validateClientById(String clientId, final Handler<Boolean> handler) {
    if (clientId != null && !clientId.trim().isEmpty()) {
        String query = "MATCH (n:Application) " + "WHERE n.name = {clientId} " + "RETURN count(n) as nb";
        Map<String, Object> params = new HashMap<>();
        params.put("clientId", clientId);
        neo.execute(query, params, new io.vertx.core.Handler<Message<JsonObject>>() {

            @Override//w w  w  . j a v  a 2s. com
            public void handle(Message<JsonObject> res) {
                JsonArray a = res.body().getJsonArray("result");
                if ("ok".equals(res.body().getString("status")) && a != null && a.size() == 1) {
                    JsonObject r = a.getJsonObject(0);
                    handler.handle(r != null && r.getInteger("nb") == 1);
                } else {
                    handler.handle(false);
                }
            }
        });
    } else {
        handler.handle(false);
    }
}

From source file:org.entcore.auth.oauth.OAuthDataHandler.java

License:Open Source License

@Override
public void validateUserById(String userId, final Handler<Boolean> handler) {
    if (userId != null && !userId.trim().isEmpty()) {
        String query = "MATCH (n:User) " + "WHERE n.id = {userId} " + "RETURN count(n) as nb";
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        neo.execute(query, params, new io.vertx.core.Handler<Message<JsonObject>>() {

            @Override//from  w  ww  .  j av a2s. com
            public void handle(Message<JsonObject> res) {
                JsonArray a = res.body().getJsonArray("result");
                if ("ok".equals(res.body().getString("status")) && a != null && a.size() == 1) {
                    JsonObject r = a.getJsonObject(0);
                    handler.handle(r != null && r.getInteger("nb") == 1);
                } else {
                    handler.handle(false);
                }
            }
        });
    } else {
        handler.handle(false);
    }
}