List of usage examples for io.vertx.core.json JsonObject getJsonObject
public JsonObject getJsonObject(String key)
From source file:org.entcore.common.http.response.SecurityHookRender.java
License:Open Source License
private void csrfToken(final HttpServerRequest request, final Handler<Void> handler, JsonObject session) { if (!csrf || request.path().contains("preview") || "XMLHttpRequest".equals(request.headers().get("X-Requested-With"))) { handler.handle(null);// w w w. j a va 2 s . c om return; } String token = null; final String xsrfToken; final String userId = session.getString("userId"); if (session.getJsonObject("cache") != null) { token = session.getJsonObject("cache").getString("xsrf-token"); if (token == null) { // TODO remove when support session cache persistence String t = CookieHelper.get("XSRF-TOKEN", request); xsrfToken = ((t != null) ? t : UUID.randomUUID().toString()); } else { xsrfToken = token; } } else { xsrfToken = UUID.randomUUID().toString(); } if (token == null) { UserUtils.addSessionAttribute(eb, userId, "xsrf-token", xsrfToken, new Handler<Boolean>() { @Override public void handle(Boolean s) { if (Boolean.TRUE.equals(s)) { CookieHelper.set("XSRF-TOKEN", xsrfToken, request); } handler.handle(null); } }); } else { handler.handle(null); } }
From source file:org.entcore.common.neo4j.Neo.java
License:Open Source License
@Deprecated public static JsonArray resultToJsonArray(JsonObject j) { JsonArray r = new fr.wseduc.webutils.collections.JsonArray(); if (j != null) { for (String idx : j.fieldNames()) { r.add(j.getJsonObject(idx)); }/*from www. j ava2 s . c om*/ } return r; }
From source file:org.entcore.common.neo4j.Neo4jResult.java
License:Open Source License
public static Either<String, JsonObject> fullNodeMerge(String nodeAttr, Message<JsonObject> res, String... otherNodes) {/*from w ww . ja va2 s. c o m*/ Either<String, JsonObject> r = validUniqueResult(res); if (r.isRight() && r.right().getValue().size() > 0) { JsonObject j = r.right().getValue(); JsonObject data = j.getJsonObject(nodeAttr, new JsonObject()).getJsonObject("data"); if (otherNodes != null && otherNodes.length > 0) { for (String attr : otherNodes) { Object e = j.getValue(attr); if (e == null) continue; if (e instanceof JsonObject) { data.put(attr, ((JsonObject) e).getJsonObject("data")); } else if (e instanceof JsonArray) { JsonArray a = new fr.wseduc.webutils.collections.JsonArray(); for (Object o : (JsonArray) e) { if (!(o instanceof JsonObject)) continue; JsonObject jo = (JsonObject) o; a.add(jo.getJsonObject("data")); } data.put(attr, a); } j.remove(attr); } } if (data != null) { j.remove(nodeAttr); return new Either.Right<>(data.mergeIn(j)); } } return r; }
From source file:org.entcore.common.notification.ws.OssFcm.java
License:Open Source License
private void getAccessToken(final Handler<String> handler) throws Exception { if (accessToken != null && tokenExpiresDate > (System.currentTimeMillis() + 1000) / 1000) { handler.handle(accessToken);//from w w w. j a v a 2 s . c om } else { try { final Long date = System.currentTimeMillis() / 1000; payload.put("iat", Long.toString(date)); payload.put("exp", Long.toString(date + 3600)); client.client2LO(payload, this.key, new Handler<JsonObject>() { @Override public void handle(JsonObject json) { JsonObject token = json.getJsonObject("token"); if ("ok".equals(json.getString("status")) && token != null) { accessToken = token.getString("access_token"); tokenExpiresDate = date + token.getInteger("expires_in"); handler.handle(accessToken); } else { handler.handle(null); } } }); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); handler.handle(null); } } }
From source file:org.entcore.common.share.impl.GenericShareService.java
License:Open Source License
protected JsonArray getResoureActions(Map<String, SecuredAction> securedActions) { if (resourceActions != null) { return resourceActions; }/*from www . java 2s .c om*/ JsonObject resourceActions = new JsonObject(); for (SecuredAction action : securedActions.values()) { if (ActionType.RESOURCE.name().equals(action.getType()) && !action.getDisplayName().isEmpty()) { JsonObject a = resourceActions.getJsonObject(action.getDisplayName()); if (a == null) { a = new JsonObject() .put("name", new fr.wseduc.webutils.collections.JsonArray() .add(action.getName().replaceAll("\\.", "-"))) .put("displayName", action.getDisplayName()).put("type", action.getType()); resourceActions.put(action.getDisplayName(), a); } else { a.getJsonArray("name").add(action.getName().replaceAll("\\.", "-")); } } } this.resourceActions = new fr.wseduc.webutils.collections.JsonArray( new ArrayList<>(resourceActions.getMap().values())); return this.resourceActions; }
From source file:org.entcore.common.share.impl.GenericShareService.java
License:Open Source License
protected void shareValidation(String resourceId, String userId, JsonObject share, Handler<Either<String, JsonObject>> handler) { final JsonObject groups = share.getJsonObject("groups"); final JsonObject users = share.getJsonObject("users"); final JsonObject shareBookmark = share.getJsonObject("bookmarks"); final HashMap<String, Set<String>> membersActions = new HashMap<>(); if (groups != null && groups.size() > 0) { for (String attr : groups.fieldNames()) { JsonArray actions = groups.getJsonArray(attr); if (actionsExists(actions.getList())) { membersActions.put(attr, new HashSet<>(actions.getList())); }// ww w .j ava 2 s . c om } } if (users != null && users.size() > 0) { for (String attr : users.fieldNames()) { JsonArray actions = users.getJsonArray(attr); if (actionsExists(actions.getList())) { membersActions.put(attr, new HashSet<>(actions.getList())); } } } if (shareBookmark != null && shareBookmark.size() > 0) { final JsonObject p = new JsonObject().put("userId", userId); StatementsBuilder statements = new StatementsBuilder(); for (String sbId : shareBookmark.fieldNames()) { final String csbId = cleanId(sbId); final String query = "MATCH (:User {id:{userId}})-[:HAS_SB]->(sb:ShareBookmark) " + "RETURN DISTINCT '" + csbId + "' as id, TAIL(sb." + csbId + ") as members "; statements.add(query, p); } Neo4j.getInstance().executeTransaction(statements.build(), null, true, Neo4jResult.validResultsHandler(sbRes -> { if (sbRes.isRight()) { JsonArray a = sbRes.right().getValue(); for (Object o : a) { JsonObject r = ((JsonArray) o).getJsonObject(0); JsonArray actions = shareBookmark.getJsonArray(r.getString("id")); JsonArray mIds = r.getJsonArray("members"); if (actions != null && mIds != null && mIds.size() > 0 && actionsExists(actions.getList())) { for (Object mId : mIds) { Set<String> actionsShare = membersActions.get(mId.toString()); if (actionsShare == null) { actionsShare = new HashSet<>(new HashSet<>(actions.getList())); membersActions.put(mId.toString(), actionsShare); // } else { // actionsShare.addAll(new HashSet<>(actions.getList())); } } } } shareValidationVisible(userId, resourceId, handler, membersActions, shareBookmark.fieldNames()); } else { handler.handle(new Either.Left<>(sbRes.left().getValue())); } })); } else { shareValidationVisible(userId, resourceId, handler, membersActions, null); } }
From source file:org.entcore.common.storage.StorageFactory.java
License:Open Source License
public StorageFactory(Vertx vertx, JsonObject config, AbstractApplicationStorage applicationStorage) { this.vertx = vertx; LocalMap<Object, Object> server = vertx.sharedData().getLocalMap("server"); String s = (String) server.get("swift"); if (s != null) { this.swift = new JsonObject(s); }/*from w w w . j a v a 2s. c o m*/ s = (String) server.get("file-system"); if (s != null) { this.fs = new JsonObject(s); } this.gridfsAddress = (String) server.get("gridfsAddress"); if (config != null && config.getJsonObject("swift") != null) { this.swift = config.getJsonObject("swift"); } else if (config != null && config.getJsonObject("file-system") != null) { this.fs = config.getJsonObject("file-system"); } else if (config != null && config.getString("gridfs-address") != null) { this.gridfsAddress = config.getString("gridfs-address"); } if (applicationStorage != null) { applicationStorage.setVertx(vertx); vertx.eventBus().localConsumer("storage", applicationStorage); } }
From source file:org.entcore.communication.services.impl.DefaultCommunicationService.java
License:Open Source License
@Override public void initDefaultRules(JsonArray structureIds, JsonObject defaultRules, final Handler<Either<String, JsonObject>> handler) { final StatementsBuilder s1 = new StatementsBuilder(); final StatementsBuilder s2 = new StatementsBuilder(); final StatementsBuilder s3 = new StatementsBuilder(); s3.add("MATCH (s:Structure)<-[:DEPENDS*1..2]-(g:ProfileGroup) " + "WHERE NOT(HAS(g.communiqueWith)) " + "SET g.communiqueWith = [] ") .add("MATCH (fg:FunctionGroup) " + "WHERE fg.name ENDS WITH 'AdminLocal' " + "SET fg.users = 'BOTH' ") .add("MATCH (ag:FunctionalGroup) " + "SET ag.users = 'BOTH' "); for (String attr : defaultRules.fieldNames()) { initDefaultRules(structureIds, attr, defaultRules.getJsonObject(attr), s1, s2); }/* w ww. j a v a 2 s. co m*/ neo4j.executeTransaction(s1.build(), null, false, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status"))) { Integer transactionId = event.body().getInteger("transactionId"); neo4j.executeTransaction(s2.build(), transactionId, false, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status"))) { Integer transactionId = event.body().getInteger("transactionId"); neo4j.executeTransaction(s3.build(), transactionId, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> message) { if ("ok".equals(message.body().getString("status"))) { handler.handle( new Either.Right<String, JsonObject>(new JsonObject())); log.info("Default communication rules initialized."); } else { handler.handle(new Either.Left<String, JsonObject>( message.body().getString("message"))); log.error("Error init default com rules : " + message.body().getString("message")); } } }); } else { handler.handle( new Either.Left<String, JsonObject>(event.body().getString("message"))); log.error("Error init default com rules : " + event.body().getString("message")); } } }); } else { handler.handle(new Either.Left<String, JsonObject>(event.body().getString("message"))); log.error("Error init default com rules : " + event.body().getString("message")); } } }); }
From source file:org.entcore.conversation.service.impl.DefaultConversationService.java
License:Open Source License
@Override public void addAttachment(String messageId, UserInfos user, JsonObject uploaded, Handler<Either<String, JsonObject>> result) { if (validationParamsError(user, result, messageId)) return;/*w w w . ja v a 2s .c o m*/ String query = "MATCH (m:ConversationMessage)<-[r:HAS_CONVERSATION_MESSAGE]-(f:ConversationSystemFolder)" + "<-[:HAS_CONVERSATION_FOLDER]-(c:Conversation) " + "WHERE m.id = {messageId} AND c.userId = {userId} AND c.active = {true} AND m.state = {draft} " + "CREATE (m)-[:HAS_ATTACHMENT]->(attachment: MessageAttachment {attachmentProps}) " + "SET r.attachments = coalesce(r.attachments, []) + {fileId} " + "RETURN attachment.id as id"; JsonObject params = new JsonObject().put("userId", user.getUserId()).put("messageId", messageId) .put("attachmentProps", new JsonObject().put("id", uploaded.getString("_id")) .put("name", uploaded.getJsonObject("metadata").getString("name")) .put("filename", uploaded.getJsonObject("metadata").getString("filename")) .put("contentType", uploaded.getJsonObject("metadata").getString("content-type")) .put("contentTransferEncoding", uploaded.getJsonObject("metadata").getString("content-transfer-encoding")) .put("charset", uploaded.getJsonObject("metadata").getString("charset")) .put("size", uploaded.getJsonObject("metadata").getLong("size"))) .put("fileId", uploaded.getString("_id")).put("true", true).put("trash", "TRASH") .put("draft", "DRAFT"); neo.execute(query, params, validUniqueResultHandler(result)); }
From source file:org.entcore.directory.controllers.UserBookController.java
License:Open Source License
@Override public void init(final Vertx vertx, JsonObject config, RouteMatcher rm, Map<String, fr.wseduc.webutils.security.SecuredAction> securedActions) { pathPrefix = "/userbook"; super.init(vertx, config, rm, securedActions); this.neo = new Neo(vertx, Server.getEventBus(vertx), log); this.config = config; userBookData = config.getJsonObject("user-book-data"); final HttpClientOptions options = new HttpClientOptions().setDefaultHost(config.getString("workspace-url")) .setDefaultPort(config.getInteger("workspace-port")).setMaxPoolSize(16).setKeepAlive(false); client = vertx.createHttpClient(options); getWithRegEx(".*", "proxyDocument"); eventStore = EventStoreFactory.getFactory().getEventStore(ANNUAIRE_MODULE); if (config.getBoolean("activation-welcome-message", false)) { activationWelcomeMessage = new HashMap<>(); String assetsPath = (String) vertx.sharedData().getLocalMap("server").get("assetPath"); Map<String, String> skins = vertx.sharedData().getLocalMap("skins"); if (skins != null) { activationWelcomeMessage = new HashMap<>(); for (final Map.Entry<String, String> e : skins.entrySet()) { String path = assetsPath + "/assets/themes/" + e.getValue() + "/template/directory/welcome/"; vertx.fileSystem().readDir(path, new Handler<AsyncResult<List<String>>>() { @Override/*from www. j a v a2 s.c om*/ public void handle(AsyncResult<List<String>> event) { if (event.succeeded()) { final Map<String, String> messages = new HashMap<>(); activationWelcomeMessage.put(e.getKey(), messages); for (final String file : event.result()) { vertx.fileSystem().readFile(file, new Handler<AsyncResult<Buffer>>() { @Override public void handle(AsyncResult<Buffer> event) { if (event.succeeded()) { String filename = file.substring( file.lastIndexOf(File.separator) + 1, file.lastIndexOf(".")); messages.put(filename, event.result().toString()); if (log.isDebugEnabled()) { log.debug("Load welcome message " + file + " as " + filename); } } } }); } } } }); } } } }