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

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

Introduction

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

Prototype

public JsonObject getJsonObject(String key) 

Source Link

Document

Get the JsonObject value with the specified key

Usage

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void copyFile(final HttpServerRequest request, final GenericDao dao, final String owner,
        final long emptySize) {
    dao.findById(request.params().get("id"), owner, new Handler<JsonObject>() {

        @Override//from  w w w.  j  a v  a  2 s.c  o m
        public void handle(JsonObject src) {
            if ("ok".equals(src.getString("status")) && src.getJsonObject("result") != null) {
                JsonObject orig = src.getJsonObject("result");
                if (orig.getJsonObject("metadata", new JsonObject()).getLong("size", 0l) > emptySize) {
                    badRequest(request, "file.too.large");
                    return;
                }
                final JsonObject dest = orig.copy();
                String now = MongoDb.formatDate(new Date());
                dest.remove("_id");
                dest.remove("protected");
                dest.put("application", WORKSPACE_NAME);
                if (owner != null) {
                    dest.put("owner", owner);
                    dest.put("ownerName", dest.getString("toName"));
                    dest.remove("to");
                    dest.remove("from");
                    dest.remove("toName");
                    dest.remove("fromName");
                }
                dest.put("created", now);
                dest.put("modified", now);
                String folder = getOrElse(request.params().get("folder"), "");
                try {
                    folder = URLDecoder.decode(folder, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    log.warn(e.getMessage(), e);
                }
                dest.put("folder", folder);
                String filePath = orig.getString("file");
                if (filePath != null) {
                    storage.copyFile(filePath, new Handler<JsonObject>() {

                        @Override
                        public void handle(JsonObject event) {
                            if (event != null && "ok".equals(event.getString("status"))) {
                                dest.put("file", event.getString("_id"));
                                persist(dest);
                            }
                        }
                    });
                } else {
                    persist(dest);
                }
            } else {
                renderJson(request, src, 404);
            }
        }

        private void persist(final JsonObject dest) {
            documentDao.save(dest, new Handler<JsonObject>() {
                @Override
                public void handle(JsonObject res) {
                    if ("ok".equals(res.getString("status"))) {
                        incrementStorage(dest);
                        renderJson(request, res);
                    } else {
                        renderError(request, res);
                    }
                }
            });
        }

    });
}

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void restore(final HttpServerRequest request, final GenericDao dao, final String to) {
    final String id = request.params().get("id");
    if (id != null && !id.trim().isEmpty()) {
        dao.findById(id, to, new Handler<JsonObject>() {

            @Override/*from   w w w  .  j  ava 2s. c o m*/
            public void handle(JsonObject res) {
                if ("ok".equals(res.getString("status"))) {
                    JsonObject doc = res.getJsonObject("result");
                    if (doc.getString("old-folder") != null) {
                        doc.put("folder", doc.getString("old-folder"));
                    } else {
                        doc.remove("folder");
                    }
                    doc.remove("old-folder");
                    dao.update(id, doc, to, new Handler<JsonObject>() {
                        @Override
                        public void handle(JsonObject res) {
                            if ("ok".equals(res.getString("status"))) {
                                renderJson(request, res);
                            } else {
                                renderJson(request, res, 404);
                            }
                        }
                    });
                } else {
                    renderJson(request, res, 404);
                }
            }
        });
    } else {
        badRequest(request);
    }
}

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void copyFile(JsonObject message, final GenericDao dao, final long emptySize,
        Handler<JsonObject> handler) {
    try {/*w w  w.j a  v  a2  s .co m*/
        dao.findById(message.getString("documentId"), message.getString("ownerId"), new Handler<JsonObject>() {
            @Override
            public void handle(JsonObject src) {
                if ("ok".equals(src.getString("status")) && src.getJsonObject("result") != null) {
                    JsonObject orig = src.getJsonObject("result");
                    if (orig.getJsonObject("metadata", new JsonObject()).getLong("size", 0l) > emptySize) {
                        handler.handle(new JsonObject().put("status", "ko").put("error", "file.too.large"));
                        return;
                    }
                    final JsonObject dest = orig.copy();
                    String now = MongoDb.formatDate(new Date());
                    dest.remove("_id");
                    dest.remove("protected");
                    if (message.getBoolean("protected") != null)
                        dest.put("protected", message.getBoolean("protected"));
                    dest.put("application", WORKSPACE_NAME);
                    if (message.getJsonObject("user") != null) {
                        dest.put("owner", message.getJsonObject("user").getString("userId"));
                        dest.put("ownerName", message.getJsonObject("user").getString("userName"));
                        dest.remove("to");
                        dest.remove("from");
                        dest.remove("toName");
                        dest.remove("fromName");
                    }
                    dest.put("created", now);
                    dest.put("modified", now);
                    String folder = getOrElse(message.getString("folder"), "");
                    try {
                        folder = URLDecoder.decode(folder, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        log.warn(e.getMessage(), e);
                    }
                    dest.put("folder", folder);
                    String filePath = orig.getString("file");
                    if (filePath != null) {
                        storage.copyFile(filePath, new Handler<JsonObject>() {

                            @Override
                            public void handle(JsonObject event) {
                                if (event != null && "ok".equals(event.getString("status"))) {
                                    dest.put("file", event.getString("_id"));
                                    persist(dest);
                                }
                            }
                        });
                    } else {
                        persist(dest);
                    }
                } else {
                    handler.handle(new JsonObject().put("status", "ko").put("error", "not.found"));
                }
            }

            private void persist(final JsonObject dest) {
                documentDao.save(dest, new Handler<JsonObject>() {
                    @Override
                    public void handle(JsonObject res) {
                        if ("ok".equals(res.getString("status"))) {
                            incrementStorage(dest);
                            handler.handle(res);
                        } else {
                            handler.handle(res);
                        }
                    }
                });
            }

        });
    } catch (Exception e) {
        log.error(e);
    }
}

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void getRevisionFile(final HttpServerRequest request, final String documentId,
        final String revisionId) {
    final QueryBuilder builder = QueryBuilder.start("_id").is(revisionId).and("documentId").is(documentId);

    //Find revision
    mongo.findOne(DOCUMENT_REVISION_COLLECTION, MongoQueryBuilder.build(builder),
            MongoDbResult.validResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override/* w w w .j av a  2s .c  o m*/
                public void handle(Either<String, JsonObject> event) {
                    if (event.isLeft()) {
                        notFound(request);
                        return;
                    }
                    JsonObject result = event.right().getValue();
                    String file = result.getString("file");
                    if (file != null && !file.trim().isEmpty()) {
                        if (ETag.check(request, file)) {
                            notModified(request, file);
                        } else {
                            storage.sendFile(file, result.getString("name"), request, false,
                                    result.getJsonObject("metadata"));
                        }
                        eventStore.createAndStoreEvent(WokspaceEvent.GET_RESOURCE.name(), request,
                                new JsonObject().put("resource", documentId));
                    } else {
                        notFound(request);
                    }
                }
            }));
}

From source file:org.etourdot.vertx.marklogic.http.impl.response.DefaultMultiPartResponse.java

License:Open Source License

private List<Document> transformPartToDocument(Map<String, List<HttpPart>> responseParts) {
    List<Document> documents = new ArrayList<>();
    for (Map.Entry<String, List<HttpPart>> responsePartEntry : responseParts.entrySet()) {
        Document document = Document.create();
        for (HttpPart part : responsePartEntry.getValue()) {
            document.format(part.getFormat());
            document.uri(part.getDocumentUri());
            if (part.isMetadata()) {
                JsonObject metadata = part.getBuffer().toJsonObject();
                document.collections(metadata.getJsonArray(MarkLogicConstants.COLLECTIONS));
                document.permissions(metadata.getJsonArray(MarkLogicConstants.PERMISSIONS));
                document.properties(metadata.getJsonObject(MarkLogicConstants.PROPERTIES));
                document.quality(metadata.getInteger(MarkLogicConstants.QUALITY));
            } else if (part.isContent()) {
                if (document.isJson()) {
                    document.content(part.getBuffer().toJsonObject());
                } else {
                    document.content(part.getBuffer());
                }/*w ww  . j a va2 s.com*/
            }
        }
        documents.add(document);
    }
    return documents;
}

From source file:org.etourdot.vertx.marklogic.model.client.impl.DocumentImpl.java

License:Open Source License

public DocumentImpl(JsonObject jsonObject) {
    this();/* ww w  .j  a  va 2 s  . c o  m*/
    requireNonNull(jsonObject);

    ofNullable(jsonObject.getString(URI)).ifPresent(this::uri);
    ofNullable(jsonObject.getValue(CONTENT)).ifPresent(this::contentObject);
    ofNullable(jsonObject.getJsonArray(COLLECTIONS)).ifPresent(this::collections);
    ofNullable(jsonObject.getJsonArray(PERMISSIONS)).ifPresent(this::permissions);
    ofNullable(jsonObject.getInteger(QUALITY)).ifPresent(this::quality);
    ofNullable(jsonObject.getJsonObject(PROPERTIES)).ifPresent(this::properties);
    ofNullable(jsonObject.getString(EXTENSION)).ifPresent(this::extension);
    ofNullable(jsonObject.getString(DIRECTORY)).ifPresent(this::directory);
    ofNullable(jsonObject.getLong(VERSION)).ifPresent(this::version);
    ofNullable(jsonObject.getString(EXTRACT)).ifPresent(this::extract);
    ofNullable(jsonObject.getString(LANG)).ifPresent(this::lang);
    ofNullable(jsonObject.getString(REPAIR)).ifPresent(this::repair);
    ofNullable(jsonObject.getString(FORMAT)).ifPresent(this::format);
    ofNullable(jsonObject.getString(MIME_TYPE)).ifPresent(this::contentType);
    if (!hasContentType()) {
        contentType(jsonObject.getString(CONTENT_TYPE));
    }
    forceFormatOrContentType();
}

From source file:org.etourdot.vertx.marklogic.model.client.impl.DocumentsImpl.java

License:Open Source License

public DocumentsImpl(JsonObject jsonObject) {
    this();//from ww  w . ja va  2 s.c  om
    requireNonNull(jsonObject);
    documents(jsonObject.getJsonArray(DOCUMENTS));
    ofNullable(jsonObject.getJsonObject(TRANSFORM)).ifPresent(this::transform);
    ofNullable(jsonObject.getString(TXID)).ifPresent(this::txid);
    ofNullable(jsonObject.getString(FOREST_NAME)).ifPresent(this::forestName);
    ofNullable(jsonObject.getString(TEMPORAL_COLLECTION)).ifPresent(this::temporalCollection);
    ofNullable(jsonObject.getString(SYSTEM_TIME)).ifPresent(this::systemTime);
    ofNullable(jsonObject.getJsonArray(CATEGORIES)).ifPresent(this::categories);
}

From source file:org.etourdot.vertx.marklogic.model.management.HostArray.java

License:Open Source License

public HostArray(JsonObject jsonObject) {
    this();/*from   w  ww . jav a 2  s. c om*/
    requireNonNull(jsonObject);

    this.hosts = jsonObject.getJsonObject("host-default-list").getJsonObject("list-items")
            .getJsonArray("list-item");
}

From source file:org.etourdot.vertx.marklogic.model.options.DatabasesOptions.java

License:Open Source License

public DatabasesOptions(JsonObject jsonObject) {
    super(jsonObject);

    ofNullable(jsonObject.getJsonObject("operation")).ifPresent(this::operation);
}

From source file:org.etourdot.vertx.marklogic.model.options.RestApiOptions.java

License:Open Source License

public RestApiOptions(JsonObject jsonObject) {
    this();//  w  ww  . j  av a2  s.c o m
    requireNonNull(jsonObject);

    ofNullable(jsonObject.getValue("remove-content")).ifPresent(this::removeContent);
    ofNullable(jsonObject.getValue("remove-modules")).ifPresent(this::removeModules);
    ofNullable(jsonObject.getString("retrieve-instance")).ifPresent(this::retrieveInstance);
    ofNullable(jsonObject.getString("retrieve-database")).ifPresent(this::retrieveDatabase);
    ofNullable(jsonObject.getJsonArray("rest-apis")).ifPresent(this::restApis);
    ofNullable(jsonObject.getJsonObject("rest-api")).ifPresent(this::restApi);
}