Example usage for io.vertx.core.eventbus Message body

List of usage examples for io.vertx.core.eventbus Message body

Introduction

In this page you can find the example usage for io.vertx.core.eventbus Message body.

Prototype

@CacheReturn
T body();

Source Link

Document

The body of the message.

Usage

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Handler<Message<JsonObject>> validUniqueResultHandler(final int idx,
        final Handler<Either<String, JsonObject>> handler, final String... jsonbFields) {
    return new Handler<Message<JsonObject>>() {
        @Override/*from   w  ww .  j a  va  2s .  com*/
        public void handle(Message<JsonObject> event) {
            if (jsonbFields != null && jsonbFields.length > 0) {
                event.body().put("jsonb_fields",
                        new fr.wseduc.webutils.collections.JsonArray(Arrays.asList(jsonbFields)));
            }
            handler.handle(validUniqueResult(idx, event));
        }
    };
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Handler<Message<JsonObject>> validResultHandler(final int idx,
        final Handler<Either<String, JsonArray>> handler, final String... jsonbFields) {
    return new Handler<Message<JsonObject>>() {
        @Override/*from w  ww .j  a  v  a  2  s  .  c om*/
        public void handle(Message<JsonObject> event) {
            if (jsonbFields != null && jsonbFields.length > 0) {
                event.body().put("jsonb_fields",
                        new fr.wseduc.webutils.collections.JsonArray(Arrays.asList(jsonbFields)));
            }
            handler.handle(validResult(idx, event));
        }
    };
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Handler<Message<JsonObject>> validResultHandler(final Handler<Either<String, JsonArray>> handler,
        final String... jsonbFields) {
    return new Handler<Message<JsonObject>>() {
        @Override//from w ww  . j ava 2 s.c o  m
        public void handle(Message<JsonObject> event) {
            if (jsonbFields != null && jsonbFields.length > 0) {
                event.body().put("jsonb_fields",
                        new fr.wseduc.webutils.collections.JsonArray(Arrays.asList(jsonbFields)));
            }
            handler.handle(validResult(event));
        }
    };
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Handler<Message<JsonObject>> validResultsHandler(final Handler<Either<String, JsonArray>> handler,
        final String... jsonbFields) {
    return new Handler<Message<JsonObject>>() {
        @Override/*  w  w w. ja v  a2s . c  om*/
        public void handle(Message<JsonObject> event) {
            if (jsonbFields != null && jsonbFields.length > 0) {
                event.body().put("jsonb_fields",
                        new fr.wseduc.webutils.collections.JsonArray(Arrays.asList(jsonbFields)));
            }
            handler.handle(validResults(event));
        }
    };
}

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

License:Open Source License

@Override
public void handle(final Message<JsonObject> event) {
    switch (event.body().getString("action", "")) {
    case "getInfos":
        getInfo(event.body().getString("id", ""), new Handler<AsyncResult<FileInfos>>() {
            @Override/*  w w w  . ja  v  a  2 s  .c  om*/
            public void handle(AsyncResult<FileInfos> infos) {
                JsonObject response;
                if (infos.succeeded()) {
                    if (infos.result() == null)
                        return;
                    response = infos.result().toJsonExcludeEmpty();
                    response.put("status", "ok");
                } else {
                    response = new JsonObject().put("status", "error").put("message",
                            infos.cause().getMessage());
                    log.error("Error retrieving file infos.", infos.cause());
                }
                reply(event, response);
            }
        });
        break;
    case "updateInfos":
        ObjectMapper mapper = new ObjectMapper();
        final JsonObject response = new JsonObject();
        try {
            final FileInfos fi = mapper.readValue(event.body().encode(), FileInfos.class);
            final String fileId = fi.getId();
            if (fileId == null) {
                response.put("status", "error").put("message", "missing.file.id");
                log.error("Missing file id");
                reply(event, response);
                return;
            }
            fi.setId(null);
            updateInfo(fileId, fi, new Handler<AsyncResult<Integer>>() {
                @Override
                public void handle(AsyncResult<Integer> updated) {
                    if (updated.succeeded()) {
                        response.put("count", updated.result()).put("status", "ok");
                    } else {
                        response.put("status", "error").put("message", updated.cause().getMessage());
                        log.error("Error updating file infos.", updated.cause());
                    }
                    reply(event, response);
                }
            });
        } catch (IOException e) {
            response.put("status", "error").put("message", e.getMessage());
            log.error("Error  deserializing file infos.", e);
            reply(event, response);
        }
        break;
    }
}

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

License:Open Source License

private void reply(Message<JsonObject> event, JsonObject response) {
    final String replyTo = event.body().getString("replyTo");
    if (isNotEmpty(replyTo)) {
        final String replyAction = event.body().getString("replyAction");
        if (isNotEmpty(replyAction)) {
            response.put("action", replyAction);
        }//ww  w . j a  v a2  s.c o m
        vertx.eventBus().send(replyTo, response, handlerToAsyncHandler(this));
    } else {
        event.reply(response, handlerToAsyncHandler(this));
    }
}

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

License:Open Source License

private void writeBuffer(String id, Buffer buff, Long maxSize, String contentType, String filename,
        final JsonObject m, Handler<JsonObject> handler) {
    JsonObject save = new JsonObject();
    save.put("action", "save");
    save.put("content-type", contentType);
    save.put("filename", filename);
    if (id != null && !id.trim().isEmpty()) {
        save.put("_id", id);
    }//from   w  w w  . j  av a  2s.co m
    final JsonObject metadata = (m != null) ? m
            : new JsonObject().put("content-type", contentType).put("filename", filename);
    if (metadata.getLong("size", 0l).equals(0l)) {
        metadata.put("size", buff.length());
    }
    if (maxSize != null && maxSize < metadata.getLong("size", 0l)) {
        handler.handle(new JsonObject().put("status", "error").put("message", "file.too.large"));
        return;
    }
    byte[] header = null;
    try {
        header = save.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        JsonObject json = new JsonObject().put("status", "error").put("message", e.getMessage());
        handler.handle(json);
    }
    if (header != null) {
        buff.appendBytes(header).appendInt(header.length);
        eb.send(gridfsAddress, buff, handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> message) {
                handler.handle(message.body().put("metadata", metadata));
            }
        }));
    }
}

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

License:Open Source License

public void saveChunk(String id, Buffer buff, int n, String contentType, String filename, long fileSize,
        final Handler<JsonObject> handler) {
    JsonObject save = new JsonObject();
    save.put("action", "saveChunk");
    save.put("content-type", contentType);
    save.put("filename", filename);
    save.put("_id", id);
    save.put("n", n);
    save.put("length", fileSize);

    byte[] header = null;
    try {//from  w w w.  ja v a2s  .com
        header = save.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        JsonObject json = new JsonObject().put("status", "error").put("message", e.getMessage());
        handler.handle(json);
    }
    if (header != null) {
        buff.appendBytes(header).appendInt(header.length);
        eb.send(gridfsAddress, buff, handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                handler.handle(event.body());
            }
        }));
    }
}

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 {//w  ww.ja  v  a 2s  . 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.GridfsStorage.java

License:Open Source License

@Override
public void removeFiles(final JsonArray ids, final Handler<JsonObject> handler) {
    final JsonObject filesQuery = new JsonObject().put("_id", new JsonObject().put("$in", ids));
    mongoDb.delete(getBucket() + ".files", filesQuery, new Handler<Message<JsonObject>>() {
        @Override/*from   w ww .  j  a v a  2  s . c  o  m*/
        public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body().getString("status"))) {
                final JsonObject chunksQuery = new JsonObject().put("files_id",
                        new JsonObject().put("$in", ids));
                mongoDb.delete(getBucket() + ".chunks", chunksQuery, new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> eventChunks) {
                        if (handler != null) {
                            handler.handle(eventChunks.body());
                        }
                    }
                });
            } else {
                // TODO find and delete orphaned chunks
                if (handler != null) {
                    handler.handle(event.body());
                }
            }
        }
    });
}