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.common.storage.impl.FileStorage.java

License:Open Source License

private void decrementRemove(AtomicInteger count, JsonArray errors, Handler<JsonObject> handler,
        JsonObject res) {/*from  w  ww .j a  va  2  s .c  om*/
    if (count.decrementAndGet() <= 0) {

        if (errors.size() == 0) {
            handler.handle(res.put("status", "ok"));
        } else {
            handler.handle(res.put("status", "error").put("errors", errors));
        }
    }
}

From source file:org.entcore.common.storage.impl.FileStorage.java

License:Open Source License

private void decrementWriteToFS(AtomicInteger count, JsonArray errors, Handler<JsonObject> handler) {
    if (count.decrementAndGet() <= 0) {
        JsonObject j = new JsonObject();
        if (errors.size() == 0) {
            handler.handle(j.put("status", "ok"));
        } else {//from  w  ww .j  a  v  a2s. com
            handler.handle(j.put("status", "error").put("errors", errors).put("message", errors.encode()));
        }
    }
}

From source file:org.entcore.common.storage.impl.GridfsStorage.java

License:Open Source License

@Override
public void removeFile(String id, Handler<JsonObject> handler) {
    JsonArray ids = new fr.wseduc.webutils.collections.JsonArray().add(id);
    JsonObject find = new JsonObject();
    find.put("action", "remove");
    JsonObject query = new JsonObject();
    if (ids != null && ids.size() == 1) {
        query.put("_id", ids.getString(0));
    } else {//from w w w .  j  a  v  a  2  s .c o  m
        query.put("_id", new JsonObject().put("$in", ids));
    }
    find.put("query", query);
    byte[] header = null;
    try {
        header = find.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        handler.handle(new JsonObject().put("status", "error"));
    }
    if (header != null) {
        Buffer buf = Buffer.buffer(header);
        buf.appendInt(header.length);
        eb.send(gridfsAddress, buf, handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> res) {
                if (handler != null) {
                    handler.handle(res.body());
                }
            }
        }));
    }
}

From source file:org.entcore.common.storage.impl.PostgresqlApplicationStorage.java

License:Open Source License

protected void getInfoProcess(final String fileId, final Handler<AsyncResult<FileInfos>> handler) {
    sql.prepared(getInfoQuery(), new fr.wseduc.webutils.collections.JsonArray().add(fileId),
            new Handler<Message<JsonObject>>() {
                @Override//from  www .  jav  a 2 s  . co  m
                public void handle(Message<JsonObject> event) {
                    Either<String, JsonArray> r = validResult(event);
                    if (r.isRight()) {
                        final JsonArray result = r.right().getValue();
                        if (result.size() == 1) {
                            final JsonObject res = result.getJsonObject(0);
                            final FileInfos fi = new FileInfos();
                            fi.setApplication(application);
                            fi.setId(fileId);
                            fi.setName(res.getString(mapping.getString("name", "name")));
                            fi.setOwner(res.getString(mapping.getString("owner", "owner")));
                            fi.setSize(res.getInteger(mapping.getString("size", "size")));
                            handler.handle(new DefaultAsyncResult<>(fi));
                        } else {
                            handler.handle(new DefaultAsyncResult<>((FileInfos) null));
                        }
                    } else {
                        handler.handle(
                                new DefaultAsyncResult<FileInfos>(new StorageException(r.left().getValue())));
                    }
                }
            });
}

From source file:org.entcore.common.storage.impl.SwiftStorage.java

License:Open Source License

@Override
public void removeFiles(JsonArray ids, final Handler<JsonObject> handler) {
    final AtomicInteger count = new AtomicInteger(ids.size());
    final JsonArray errors = new fr.wseduc.webutils.collections.JsonArray();
    for (final Object o : ids) {
        swiftClient.deleteFile(o.toString(), new Handler<AsyncResult<Void>>() {
            @Override//  w  w  w.ja  v a  2 s.  c o  m
            public void handle(AsyncResult<Void> event) {
                if (event.failed()) {
                    errors.add(new JsonObject().put("id", o.toString()).put("message",
                            event.cause().getMessage()));
                }
                if (count.decrementAndGet() <= 0) {
                    JsonObject j = new JsonObject();
                    if (errors.size() == 0) {
                        handler.handle(j.put("status", "ok"));
                    } else {
                        handler.handle(j.put("status", "error").put("errors", errors));
                    }
                }
            }
        });
    }
}

From source file:org.entcore.common.storage.impl.SwiftStorage.java

License:Open Source License

@Override
public void writeToFileSystem(String[] ids, String destinationPath, JsonObject alias,
        final Handler<JsonObject> handler) {
    final AtomicInteger count = new AtomicInteger(ids.length);
    final JsonArray errors = new fr.wseduc.webutils.collections.JsonArray();
    for (final String id : ids) {
        if (id == null || id.isEmpty()) {
            count.decrementAndGet();//from   ww  w. j a  va2 s  . c o m
            continue;
        }
        String d = destinationPath + File.separator + alias.getString(id, id);
        swiftClient.writeToFileSystem(id, d, new Handler<AsyncResult<String>>() {
            @Override
            public void handle(AsyncResult<String> event) {
                if (event.failed()) {
                    errors.add(new JsonObject().put("id", id).put("message", event.cause().getMessage()));
                }
                if (count.decrementAndGet() <= 0) {
                    JsonObject j = new JsonObject();
                    if (errors.size() == 0) {
                        handler.handle(j.put("status", "ok"));
                    } else {
                        handler.handle(
                                j.put("status", "error").put("errors", errors).put("message", errors.encode()));
                    }
                }
            }
        });
    }
}

From source file:org.entcore.common.storage.StorageFactory.java

License:Open Source License

public Storage getStorage() {
    Storage storage = null;/*from w w w.jav a 2  s  .co m*/
    if (swift != null) {
        String uri = swift.getString("uri");
        String container = swift.getString("container");
        String username = swift.getString("user");
        String password = swift.getString("key");
        try {
            storage = new SwiftStorage(vertx, new URI(uri), container, username, password);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    } else if (fs != null) {
        storage = new FileStorage(vertx, fs.getString("path"), fs.getBoolean("flat", false));
        JsonObject antivirus = fs.getJsonObject("antivirus");
        if (antivirus != null) {
            final String h = antivirus.getString("host");
            final String c = antivirus.getString("credential");
            if (isNotEmpty(h) && isNotEmpty(c)) {
                AntivirusClient av = new HttpAntivirusClient(vertx, h, c);
                ((FileStorage) storage).setAntivirus(av);
            }
        }
        FileValidator fileValidator = new QuotaFileSizeValidation();
        JsonArray blockedExtensions = fs.getJsonArray("blockedExtensions");
        if (blockedExtensions != null && blockedExtensions.size() > 0) {
            fileValidator.setNext(new ExtensionValidator(blockedExtensions));
        }
        ((FileStorage) storage).setValidator(fileValidator);
    } else {
        storage = new GridfsStorage(vertx, Server.getEventBus(vertx), gridfsAddress);
    }
    return storage;
}

From source file:org.entcore.communication.controllers.CommunicationController.java

License:Open Source License

@Post("/visible")
@SecuredAction(value = "", type = ActionType.AUTHENTICATED)
public void searchVisible(HttpServerRequest request) {
    RequestUtils.bodyToJson(request, filter -> UserUtils.getUserInfos(eb, request, user -> {
        if (user != null) {
            String preFilter = "";
            String match = "";
            String where = "";
            String nbUsers = "";
            String groupTypes = "";
            JsonObject params = new JsonObject();
            JsonArray expectedTypes = null;
            if (filter != null && filter.size() > 0) {
                for (String criteria : filter.fieldNames()) {
                    switch (criteria) {
                    case "structures":
                    case "classes":
                        JsonArray itemssc = filter.getJsonArray(criteria);
                        if (itemssc == null || itemssc.isEmpty()
                                || ("structures".equals(criteria) && filter.getJsonArray("classes") != null
                                        && !filter.getJsonArray("classes").isEmpty()))
                            continue;
                        if (!params.containsKey("nIds")) {
                            params.put("nIds", itemssc);
                        } else {
                            params.getJsonArray("nIds").addAll(itemssc);
                        }//from  w w  w .jav a 2s  .  c o  m
                        if (!match.contains("-[:DEPENDS")) {
                            if (!match.contains("MATCH ")) {
                                match = "MATCH ";
                                where = " WHERE ";
                            } else {
                                match += ", ";
                                where += "AND ";
                            }
                            match += "(visibles)-[:IN*0..1]->()-[:DEPENDS]->(n) ";
                            where += "n.id IN {nIds} ";
                        }
                        if ("structures".equals(criteria)) {
                            match = match.replaceFirst("\\[:DEPENDS]", "[:DEPENDS*1..2]");
                        }
                        break;
                    case "profiles":
                    case "functions":
                        JsonArray itemspf = filter.getJsonArray(criteria);
                        if (itemspf == null || itemspf.isEmpty())
                            continue;
                        if (!params.containsKey("filters")) {
                            params.put("filters", itemspf);
                        } else {
                            //params.getJsonArray("filters").addAll(itemspf);
                            params.put("filters2", itemspf);
                        }
                        if (!match.contains("MATCH ")) {
                            match = "MATCH ";
                            where = " WHERE ";
                        } else {
                            match += ", ";
                            where += "AND ";
                        }
                        if (!match.contains("(visibles)-[:IN*0..1]->(g)")) {
                            match += "(visibles)-[:IN*0..1]->(g)";
                            where += "g.filter IN {filters} ";
                        } else {
                            match += "(visibles)-[:IN*0..1]->(g2) ";
                            where += "g2.filter IN {filters2} ";
                        }
                        break;
                    case "search":
                        final String search = filter.getString(criteria);
                        if (isNotEmpty(search)) {
                            preFilter = "AND m.displayNameSearchField CONTAINS {search} ";
                            String sanitizedSearch = StringValidation.sanitize(search);
                            params.put("search", sanitizedSearch);
                        }
                        break;
                    case "types":
                        JsonArray types = filter.getJsonArray(criteria);
                        if (types != null && types.size() > 0
                                && CommunicationService.EXPECTED_TYPES.containsAll(types.getList())) {
                            expectedTypes = types;
                        }
                        break;
                    case "nbUsersInGroups":
                        if (filter.getBoolean("nbUsersInGroups", false)) {
                            nbUsers = ", visibles.nbUsers as nbUsers";
                        }
                        break;
                    case "groupType":
                        if (filter.getBoolean("groupType", false)) {
                            groupTypes = ", labels(visibles) as groupType, visibles.filter as groupProfile";
                        }
                        break;
                    }
                }
            }
            final boolean returnGroupType = !groupTypes.isEmpty();
            final String customReturn = match + where
                    + "RETURN DISTINCT visibles.id as id, visibles.name as name, "
                    + "visibles.displayName as displayName, visibles.groupDisplayName as groupDisplayName, "
                    + "HEAD(visibles.profiles) as profile" + nbUsers + groupTypes;
            communicationService.visibleUsers(user.getUserId(), null, expectedTypes, true, true, false,
                    preFilter, customReturn, params, user.getType(), visibles -> {
                        if (visibles.isRight()) {
                            renderJson(request, UserUtils.translateAndGroupVisible(visibles.right().getValue(),
                                    I18n.acceptLanguage(request), returnGroupType));
                        } else {
                            leftToResponse(request, visibles.left());
                        }
                    });
        } else {
            badRequest(request, "invalid.user");
        }
    }));
}

From source file:org.entcore.communication.controllers.CommunicationController.java

License:Open Source License

@Put("/init/rules")
@SecuredAction("communication.init.default.rules")
public void initDefaultCommunicationRules(final HttpServerRequest request) {
    RequestUtils.bodyToJson(request, new Handler<JsonObject>() {
        @Override/*from   ww  w .  j a  v  a 2s  . c o m*/
        public void handle(JsonObject body) {
            JsonObject initDefaultRules = config.getJsonObject("initDefaultCommunicationRules");
            JsonArray structures = body.getJsonArray("structures");
            if (structures != null && structures.size() > 0) {
                communicationService.initDefaultRules(structures, initDefaultRules,
                        defaultResponseHandler(request));
            } else {
                badRequest(request, "invalid.structures");
            }
        }
    });
}

From source file:org.entcore.communication.filters.CommunicationFilter.java

License:Open Source License

private void check(String query, JsonObject params, final Handler<Boolean> handler) {
    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override/*from w  w  w  .  ja v  a  2 s  .  c om*/
        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));
        }
    });
}