List of usage examples for io.vertx.core.json JsonObject put
public JsonObject put(String key, Object value)
From source file:com.groupon.vertx.redis.RedisCommand.java
License:Apache License
/** * Renders the command into a JsonObject for transport across the event bus. * * @return - A JsonObject containing the command. *///from w w w. java 2 s. c o m public JsonObject toJson() { JsonObject jsonObject = new JsonObject(); jsonObject.put("command", type.getCommand()); JsonArray arrayArgs = new JsonArray(); for (String arg : arguments) { arrayArgs.add(arg); } jsonObject.put("arguments", arrayArgs); return jsonObject; }
From source file:com.groupon.vertx.redis.RedisCommandHandler.java
License:Apache License
private JsonObject buildReply(String status, JsonObject data, String message) { JsonObject reply = new JsonObject(); reply.put("status", status); if ("success".equals(status) || data != null) { reply.putNull("data"); } else {//w w w . j a va2s. co m reply.put("message", message); } return reply; }
From source file:com.groupon.vertx.redis.RedisInputStream.java
License:Apache License
/** * This method is fired when enough data is in the Buffer to complete a command. If the * command does not match the signature of the buffered data then an exception is thrown * and the socket should be closed as the command/response queues are no longer in sync. * * @param command - The command to process from the response buffer. *///from w ww. j a va 2s . com private void processCommand(RedisCommand command) { if (command == null) { // No command to process so return. Should add log message here. log.warn("processCommand", "noCommandFound"); return; } JsonObject response = new JsonObject(); byte[] line = completedLines.poll(); if (line == null) { log.warn("processCommand", "noCompletedLinesFound", new String[] { "command" }, command.getCommand()); response.put("status", "error"); response.put("message", "Unable to find completed line for command: " + command.getCommand()); } else if (line[0] == RedisResponseType.ERROR.marker) { log.warn("processCommand", "redisReturnedError", new String[] { "command" }, command.getCommand()); response.put("status", "fail"); response.put("data", processLine(line)); } else if (line[0] == RedisResponseType.BULK_REPLY.marker && line[1] == '-') { log.debug("processCommand", "redisReturnedNil", new String[] { "command" }, command.getCommand()); response.put("status", "success"); response.put("data", processBulkLine(line)); } else if (line[0] != command.getResponseType().marker) { log.warn("processCommand", "mismatchedResponse", new String[] { "command", "expectedDelim", "foundDelim" }, command.getCommand(), (char) command.getResponseType().marker, (char) line[0]); throw new RedisCommandException("Invalid response found"); } else { response.put("status", "success"); if (command.getResponseType() == RedisResponseType.MULTI_BULK_REPLY) { response.put("data", processMultiLine(line)); } else if (command.getResponseType() == RedisResponseType.BULK_REPLY) { response.put("data", processBulkLine(line)); } else if (command.getResponseType() == RedisResponseType.INTEGER_REPLY) { response.put("data", processIntegerLine(line)); } else { response.put("data", processLine(line)); } log.trace("processCommand", "redisCommandSuccess", new String[] { "command", "data" }, command.getCommand(), response.getValue("data")); } command.setResponse(response); }
From source file:com.groupon.vertx.redis.RedisTransaction.java
License:Apache License
public Future<JsonObject> exec() { final Future<JsonObject> finalResult = Future.future(); if (!pendingCommands.isEmpty()) { JsonArray commands = new JsonArray(); final List<Future<JsonObject>> clientCommandResponses = new ArrayList<>(); clientCommandResponses.add(finalResult); RedisCommand command = pendingCommands.poll(); while (command != null) { clientCommandResponses.add(command.getClientCommandResponse()); commands.add(command.toJson()); command = pendingCommands.poll(); }/*from w ww .j a v a 2s.c o m*/ JsonObject transactionCommands = new JsonObject(); transactionCommands.put("isTransaction", true); transactionCommands.put("commands", commands); final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(replyTimeout); eventBus.send(eventBusAddress, transactionCommands, deliveryOptions, new Handler<AsyncResult<Message<JsonObject>>>() { @Override public void handle(AsyncResult<Message<JsonObject>> messageAsyncResult) { JsonObject response; if (messageAsyncResult.failed()) { response = new JsonObject().put("status", "error").put("code", HttpURLConnection.HTTP_GATEWAY_TIMEOUT); } else { response = messageAsyncResult.result().body(); } int index = 0; executeResponse(clientCommandResponses.remove(0), response); // EXEC response for (Future<JsonObject> clientCommandResponse : clientCommandResponses) { if (clientCommandResponse != null) { JsonObject result = constructTransactionCommandResult(response, index); executeResponse(clientCommandResponse, result); } index++; } } }); } else { // Nothing to execute. finalResult.complete(null); } return finalResult; }
From source file:com.groupon.vertx.redis.RedisTransaction.java
License:Apache License
private JsonObject constructTransactionCommandResult(JsonObject response, int index) { JsonArray responses = response.getJsonArray("data"); if (responses != null && responses.size() > index) { Object commandResult = responses.getValue(index); JsonObject result = new JsonObject(); result.put("status", response.getString("status")); if (commandResult instanceof JsonArray) { result.put("data", (JsonArray) commandResult); } else if (commandResult instanceof String) { result.put("data", (String) commandResult); } else if (commandResult instanceof Number) { result.put("data", (Number) commandResult); }/*from w w w.j a v a2s.co m*/ return result; } return response; }
From source file:com.hpe.sw.cms.store.Image.java
License:Apache License
public static JsonObject cloneImage(JsonObject sourceImage) { JsonObject targetImage = new JsonObject(); targetImage.put("host", (sourceImage).getString("host")); targetImage.put("name", (sourceImage).getString("name")); targetImage.put("tag", (sourceImage).getString("tag")); targetImage.put("imageid", (sourceImage).getString("imageid")); targetImage.put("timestamp", (sourceImage).getLong("timestamp")); targetImage.put("isEnriched", (sourceImage).getBoolean("isEnriched")); targetImage.put("isScanned", (sourceImage).getBoolean("isScanned")); targetImage.put(Image.IS_SCANNED_FAILED, (sourceImage).getBoolean(Image.IS_SCANNED_FAILED)); return targetImage; }
From source file:com.hpe.sw.cms.verticle.ApiVerticle.java
License:Apache License
/** * Handler to receive POST /_scanfile.// w ww. j a v a 2s . c om * * @return */ private Handler<RoutingContext> uploadHandler() { return routingContext -> { int resposeCode = 201; String resposeMsg = "OK"; String host = routingContext.request().getParam(Image.HOST); String name = routingContext.request().getParam(Image.NAME); String tag = routingContext.request().getParam(Image.TAG); String imageid = routingContext.request().getParam(Image.IMAGE_ID); String errorMsg = routingContext.request().getParam("errorMsg"); JsonObject upFile = new JsonObject(); upFile.put(Image.HOST, host); upFile.put(Image.NAME, name); upFile.put(Image.TAG, tag); if (errorMsg != null && "0".equals(imageid)) { upFile.put(Image.IS_SCANNED_FAILED, true); LOG.error("scanfile uploaded failed " + Image.getImageKey(upFile) + " with error message " + errorMsg); } else { Set<FileUpload> upFiles = routingContext.fileUploads(); FileUpload upF = upFiles.iterator().next(); ByteArrayOutputStream out = null; BufferedInputStream in = null; upFile.put(Image.IMAGE_ID, imageid); upFile.put(Image.IS_SCANNED_FAILED, false); try { File file = new File(upF.uploadedFileName()); in = new BufferedInputStream(new FileInputStream(file)); out = new ByteArrayOutputStream(); IOUtils.copy(in, out); upFile.put(Image.SCANNED_FILE, out.toByteArray()); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); out.close(); } catch (IOException e) { upFile.put(Image.IS_SCANNED_FAILED, true); resposeCode = 500; resposeMsg = "Error happens when in reading file"; LOG.error(resposeMsg, e); } } LOG.debug("scanfile uploaded " + Image.getImageKey(upFile)); } routingContext.response().setStatusCode(resposeCode).end(resposeMsg); getVertx().eventBus().publish(Events.SCANFILE_UPLOADED.name(), upFile); }; }
From source file:com.hpe.sw.cms.verticle.ApiVerticle.java
License:Apache License
/** * Handler to handle GET /images/:id request. Response is the image scan files (with/without enrich). * * @return//from w ww . j a v a 2 s . co m */ private Handler<RoutingContext> downloadHandler() { return routingContext -> { String category = routingContext.request().getParam("fileCategory"); JsonObject params = new JsonObject(); if (routingContext.request().getParam("id") != null) { params.put(Image.IMAGE_ID, routingContext.request().getParam("id")); } else { params.put(Image.HOST, routingContext.request().getParam(Image.HOST)); params.put(Image.NAME, routingContext.request().getParam(Image.NAME)); params.put(Image.TAG, routingContext.request().getParam(Image.TAG)); } vertx.eventBus().send(Events.DOWNLOAD_FILE.name(), params, event -> { if (event.succeeded() && event.result() != null) { Message msg = event.result(); JsonObject file = (JsonObject) msg.body(); routingContext.response().setChunked(true); if (file == null) { routingContext.response().setStatusCode(404).end("There is no image found."); return; } if ("enrich".equals(category) && file.getBinary(Image.ENRICHED_FILE) != null) { String fileName = file.getString(Image.HOST) + "___" + file.getString(Image.NAME) + "___" + file.getString(Image.IMAGE_ID) + ".xsf"; routingContext.response().putHeader("Content-Disposition", "attachment; filename = " + fileName); Buffer buffer = Buffer.buffer(file.getBinary(Image.ENRICHED_FILE)); routingContext.response().end(buffer); } else if ("enrich_xml".equals(category) && file.getBinary(Image.SCANNED_FILE) != null) { routingContext.response().end(decompressGzip(file.getBinary(Image.ENRICHED_FILE))); } else if ("scan".equals(category) && file.getBinary(Image.SCANNED_FILE) != null) { String fileName = file.getString(Image.HOST) + "___" + file.getString(Image.NAME) + "___" + file.getString(Image.IMAGE_ID) + ".xsf"; routingContext.response().putHeader("Content-Disposition", "attachment; filename = " + fileName); Buffer buffer = Buffer.buffer(file.getBinary(Image.SCANNED_FILE)); routingContext.response().end(buffer); } else if ("scan_xml".equals(category) && file.getBinary(Image.SCANNED_FILE) != null) { routingContext.response().end(decompressGzip(file.getBinary(Image.SCANNED_FILE))); } } else if (event.result() == null) { routingContext.response().setStatusCode(404).end("There is no image found."); } else { routingContext.response().setStatusCode(500).end("Server has error."); } }); }; }
From source file:com.hpe.sw.cms.verticle.ApiVerticle.java
License:Apache License
/** * Handler to handle GET /images request. The response is a list of images * * @return/*from ww w . j a v a 2 s. c o m*/ */ private Handler<RoutingContext> imagesHandler() { return routingContext -> { String timestamp = routingContext.request().getParam("timestamp"); String id = routingContext.request().getParam("id"); String include = routingContext.request().getParam("include"); JsonObject param = new JsonObject(); if (timestamp != null) { param.put("timestamp", timestamp); } if (id != null) { param.put("imageid", id); } if (include != null) { param.put("include", include); } getVertx().eventBus().send(Events.GET_IMAGES.name(), param, event -> { if (event.succeeded() && event.result() != null) { Message msg = event.result(); JsonArray updates = (JsonArray) msg.body(); HttpServerResponse response = routingContext.response(); if (updates.size() == 0) { response.end("There is no image found."); } else { response.end(updates.toString()); } } else if (event.result() == null) { routingContext.response().setStatusCode(404).end("There is no image found."); } else { routingContext.response().setStatusCode(500).end("Server has error."); } }); }; }
From source file:com.hpe.sw.cms.verticle.ApiVerticle.java
License:Apache License
private JsonObject createObj(JsonObject event) { try {/*w ww.ja va 2 s .co m*/ JsonObject image = new JsonObject(); String timestamp = event.getString("timestamp"); if (timestamp != null && timestamp.length() > 19) { timestamp = timestamp.substring(0, 10) + " " + timestamp.substring(11, 19); image.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestamp).getTime()); } String url = event.getJsonObject("target").getString("url"); image.put("name", event.getJsonObject("target").getString("repository")); image.put("host", event.getJsonObject("request").getString("host")); image.put(Image.EVENT_URL, url); return image; } catch (Exception e) { LOG.error("Error in reading event to object ", e); return null; } }