List of usage examples for io.vertx.core.json JsonObject getJsonArray
public JsonArray getJsonArray(String key)
From source file:org.entcore.cas.services.PronoteRegisteredService.java
License:Open Source License
@Override protected void prepareUser(User user, String userId, String service, JsonObject data) { user.setUser(data.getString(principalAttributeName)); user.setAttributes(new HashMap<String, String>()); try {/* ww w . j av a2 s .c o m*/ if (data.getString("lastName") != null && data.getString("firstName") != null) { user.getAttributes().put("nom", data.getString("lastName")); user.getAttributes().put("prenom", data.getString("firstName")); } if (data.getString("birthDate") != null) { user.getAttributes().put("dateNaissance", data.getString("birthDate").replaceAll("([0-9]+)-([0-9]+)-([0-9]+)", "$3/$2/$1")); } if (data.getString("postalCode") != null) { user.getAttributes().put("codePostal", data.getString("postalCode")); } String category = null; JsonArray types = data.getJsonArray("type"); for (Object type : types.getList()) { switch (type.toString()) { case "Student": category = checkProfile(category, "National_1"); break; case "Teacher": category = checkProfile(category, "National_3"); break; case "Relative": category = checkProfile(category, "National_2"); break; case "Personnel": category = checkProfile(category, "National_4"); break; } } if (category != null) { user.getAttributes().put("categories", category); } } catch (Exception e) { log.error("Failed to transform User for Pronote"); } }
From source file:org.entcore.cas.services.WebclasseursRegisteredService.java
License:Open Source License
@Override protected void prepareUserCas20(User user, String userId, String service, JsonObject data, Document doc, List<Element> additionnalAttributes) { user.setUser(data.getString(principalAttributeName)); try {/*from www .j av a 2 s.com*/ // Lastname if (data.containsKey("login")) { additionnalAttributes.add(createTextElement(WEBCLASSEURS_LOGIN, data.getString("login"), doc)); } // Profile JsonArray profiles = data.getJsonArray("type"); if (profiles.contains("Teacher") || profiles.contains("Personnel")) { // Teacher and Personnel seen alike for Webclasseurs additionnalAttributes.add(createTextElement(WEBCLASSEURS_PROFILE, "National_3", doc)); } else if (profiles.contains("Student")) { additionnalAttributes.add(createTextElement(WEBCLASSEURS_PROFILE, "National_1", doc)); } else if (profiles.contains("Relative")) { additionnalAttributes.add(createTextElement(WEBCLASSEURS_PROFILE, "National_2", doc)); } } catch (Exception e) { log.error("Failed to transform User for Webclasseurs", e); } }
From source file:org.entcore.common.elasticsearch.ElasticSearch.java
License:Open Source License
public void init(Vertx vertx, JsonObject config) { this.vertx = vertx; JsonArray serverUris = config.getJsonArray("server-uris"); String serverUri = config.getString("server-uri"); if (serverUris == null && serverUri != null) { serverUris = new fr.wseduc.webutils.collections.JsonArray().add(serverUri); }/*from www. j av a 2s . c o m*/ if (serverUris != null) { try { URI[] uris = new URI[serverUris.size()]; for (int i = 0; i < serverUris.size(); i++) { uris[i] = new URI(serverUris.getString(i)); } init(uris, vertx, config.getInteger("poolSize", 16), config.getBoolean("keepAlive", true), config); } catch (Exception e) { log.error(e.getMessage(), e); } } else { log.error("Invalid ElasticSearch URI"); } }
From source file:org.entcore.common.http.filter.AbstractActionFilter.java
License:Open Source License
private void authorizeWorkflowAction(JsonObject session, Binding binding, Handler<Boolean> handler) { JsonArray actions = session.getJsonArray("authorizedActions"); if (binding != null && binding.getServiceMethod() != null && actions != null && actions.size() > 0) { for (Object a : actions) { JsonObject action = (JsonObject) a; if (binding.getServiceMethod().equals(action.getString("name"))) { handler.handle(true);// w w w . ja v a 2s.c o m return; } } } if (session.getJsonObject("functions", new JsonObject()).containsKey("SUPER_ADMIN")) { handler.handle(true); return; } handler.handle(false); }
From source file:org.entcore.common.http.response.SecurityHookRender.java
License:Open Source License
private void contentSecurityPolicyHeader(HttpServerRequest request, JsonObject session) { if (contentSecurityPolicy == null) return;/*from ww w. j a v a 2 s . com*/ final String csp; if (session != null && session.getJsonObject("cache") != null && session.getJsonObject("cache").getString("content-security-policy") != null) { csp = session.getJsonObject("cache").getString("content-security-policy"); } else if (session != null && session.getJsonArray("apps") != null) { final StringBuilder sb = new StringBuilder(contentSecurityPolicy); if (!contentSecurityPolicy.contains("frame-src")) { if (!contentSecurityPolicy.trim().endsWith(";")) { sb.append("; "); } sb.append("frame-src 'self'"); } for (Object o : session.getJsonArray("apps")) { if (!(o instanceof JsonObject)) continue; String address = ((JsonObject) o).getString("address"); if (address != null && address.contains("adapter#")) { String[] s = address.split("adapter#"); if (s.length == 2 && isNotEmpty(s[1])) { try { URI uri = new URI(s[1]); sb.append(" ").append(uri.getHost()); } catch (URISyntaxException e) { log.warn("Invalid adapter URI : " + s[1], e); } } } } csp = sb.append(";").toString(); UserUtils.addSessionAttribute(eb, session.getString("userId"), "content-security-policy", csp, null); } else { csp = contentSecurityPolicy; } request.response().putHeader("Content-Security-Policy", csp); }
From source file:org.entcore.common.neo4j.Neo.java
License:Open Source License
@Deprecated public void sendBatch(JsonArray queries, final Handler<Message<JsonObject>> handler) { neo4j.executeBatch(queries, new Handler<Message<JsonObject>>() { @Override//from w w w . j a v a 2s . c o m public void handle(Message<JsonObject> event) { if (handler != null) { JsonArray results = event.body().getJsonArray("results"); if ("ok".equals(event.body().getString("status")) && results != null) { for (Object o : results) { if (!(o instanceof JsonObject)) continue; JsonObject j = (JsonObject) o; int i = 0; JsonObject r = new JsonObject(); for (Object o2 : j.getJsonArray("result")) { if (!(o2 instanceof JsonObject)) continue; r.put(String.valueOf(i++), (JsonObject) o2); } j.put("result", r); } } handler.handle(event); } } }); }
From source file:org.entcore.common.neo4j.Neo4j.java
License:Open Source License
public void init(Vertx vertx, JsonObject config) { this.eb = Server.getEventBus(vertx); JsonArray serverUris = config.getJsonArray("server-uris"); String serverUri = config.getString("server-uri"); if (serverUris == null && serverUri != null) { serverUris = new fr.wseduc.webutils.collections.JsonArray().add(serverUri); }/* w ww.j av a 2s.c o m*/ if (serverUris != null) { try { URI[] uris = new URI[serverUris.size()]; for (int i = 0; i < serverUris.size(); i++) { uris[i] = new URI(serverUris.getString(i)); } database = new Neo4jRest(uris, config.getBoolean("slave-readonly", false), vertx, config.getLong("checkDelay", 3000l), config.getInteger("poolSize", 16), config.getBoolean("keepAlive", true), config); } catch (Exception e) { log.error(e.getMessage(), e); } } else { log.error("Invalid Neo4j URI"); } }
From source file:org.entcore.common.neo4j.Neo4jRest.java
License:Open Source License
public Neo4jRest(URI[] uris, boolean ro, Vertx vertx, long checkDelay, int poolSize, boolean keepAlive, JsonObject neo4jConfig) { nodeManager = new Neo4jRestNodeClient(uris, vertx, checkDelay, poolSize, keepAlive); this.ro = ro; String path = uris[0].getPath(); if (path != null && path.endsWith("/")) { this.basePath = path.substring(0, path.length() - 1); } else {/*from www . j a va 2 s. c o m*/ this.basePath = path; } if (neo4jConfig != null) { JsonArray legacyIndexes = neo4jConfig.getJsonArray("legacy-indexes"); if (legacyIndexes != null && legacyIndexes.size() > 0) { for (Object o : legacyIndexes) { if (!(o instanceof JsonObject)) continue; JsonObject j = (JsonObject) o; createIndex(j); } } } }
From source file:org.entcore.common.neo4j.Neo4jRest.java
License:Open Source License
public void executeTransaction(final JsonArray statements, final Integer transactionId, final boolean commit, final boolean allowRetry, final Handler<JsonObject> handler) { String uri = "/transaction"; if (transactionId != null) { uri += "/" + transactionId; }//from ww w.ja v a 2s . com if (commit) { uri += "/commit"; } try { sendRequest(uri, new JsonObject().put("statements", statements), new Handler<HttpClientResponse>() { @Override public void handle(final HttpClientResponse resp) { resp.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer b) { logger.debug(b.toString()); if (resp.statusCode() != 404 && resp.statusCode() != 500) { JsonObject json = new JsonObject(b.toString("UTF-8")); JsonArray results = json.getJsonArray("results"); if (json.getJsonArray("errors", new fr.wseduc.webutils.collections.JsonArray()) .size() == 0 && results != null) { JsonArray out = new fr.wseduc.webutils.collections.JsonArray(); for (Object o : results) { if (!(o instanceof JsonObject)) continue; out.add(transformJson((JsonObject) o)); } json.put("results", out); String commit = json.getString("commit"); if (commit != null) { String[] c = commit.split("/"); if (c.length > 2) { json.put("transactionId", Integer.parseInt(c[c.length - 2])); } } json.remove("errors"); handler.handle(json); } else { if (transactionId == null && commit && allowRetry && json.getJsonArray("errors") != null && json.getJsonArray("errors").size() > 0) { JsonArray errors = json.getJsonArray("errors"); for (Object o : errors) { if (!(o instanceof JsonObject)) continue; switch (((JsonObject) o).getString("code", "")) { case "Neo.TransientError.Transaction.ConstraintsChanged": case "Neo.TransientError.Transaction.DeadlockDetected": case "Neo.TransientError.Transaction.InstanceStateChanged": case "Neo.TransientError.Schema.SchemaModifiedConcurrently": executeTransaction(statements, transactionId, commit, false, handler); if (logger.isDebugEnabled()) { logger.debug("Retry transaction : " + statements.encode()); } return; } } } handler.handle( new JsonObject().put("message", json.getJsonArray("errors", new fr.wseduc.webutils.collections.JsonArray()) .encode())); } } else { handler.handle(new JsonObject().put("message", resp.statusMessage() + " : " + b.toString())); } } }); } }); } catch (Neo4jConnectionException e) { ExceptionUtils.exceptionToJson(e); } }
From source file:org.entcore.common.neo4j.Neo4jRest.java
License:Open Source License
private JsonArray transformJson(JsonObject json) { final JsonArray columns = json.getJsonArray("columns"); final JsonArray data = json.getJsonArray("data"); final JsonArray out = new fr.wseduc.webutils.collections.JsonArray(); if (data != null && columns != null) { for (Object r : data) { JsonArray row;//from w ww . ja v a2 s.c o m if (r instanceof JsonArray) { row = (JsonArray) r; } else if (r instanceof JsonObject) { row = ((JsonObject) r).getJsonArray("row"); } else { continue; } JsonObject outRow = new fr.wseduc.webutils.collections.JsonObject(); out.add(outRow); for (int j = 0; j < row.size(); j++) { Object value = row.getValue(j); if (value == null) { outRow.put(columns.getString(j), (String) null); } else if (value instanceof String) { outRow.put(columns.getString(j), (String) value); } else if (value instanceof JsonArray) { outRow.put(columns.getString(j), (JsonArray) value); } else if (value instanceof JsonObject) { outRow.put(columns.getString(j), (JsonObject) value); } else if (value instanceof Boolean) { outRow.put(columns.getString(j), (Boolean) value); } else if (value instanceof Number) { outRow.put(columns.getString(j), (Number) value); } else { outRow.put(columns.getString(j), value.toString()); } } } } return out; }