List of usage examples for io.vertx.core.json JsonObject JsonObject
public JsonObject()
From source file:com.company.vertxstarter.MainVerticle.java
@Override public void start(Future<Void> fut) { // Create a router object. Router router = Router.router(vertx); //CORS handler router.route().handler(CorsHandler.create("*").allowedMethod(HttpMethod.GET).allowedMethod(HttpMethod.POST) .allowedMethod(HttpMethod.OPTIONS).allowedHeader("Content-Type").allowedHeader("Accept")); //default headers router.route().handler(ctx -> {/*from w w w . ja v a2 s. c o m*/ ctx.response().putHeader("Cache-Control", "no-store, no-cache").putHeader("Content-Type", "application/json"); if (StringUtils.isEmpty(ctx.request().getHeader("Accept"))) { ctx.fail(Failure.NO_MEDIA_TYPE); return; } else if (!"application/json".equalsIgnoreCase(ctx.request().getHeader("Accept"))) { ctx.fail(Failure.UNSUPPORTED_MEDIA_TYPE); return; } ctx.next(); }); //error handling router.route().failureHandler(ctx -> { HttpServerResponse response = ctx.response(); final JsonObject error = new JsonObject(); Failure ex; if (ctx.failure() instanceof Failure) { //specific error ex = (Failure) ctx.failure(); } else { //general error ctx.failure().printStackTrace(); ex = Failure.INTERNAL_ERROR; } error.put("message", ex.getMessage()); response.setStatusCode(ex.getCode()).end(error.encode()); }); //default 404 handling router.route().last().handler(ctx -> { HttpServerResponse response = ctx.response(); final JsonObject error = new JsonObject(); error.put("message", Failure.NOT_FOUND.getMessage()); response.setStatusCode(404).end(error.encode()); }); //routes Injector injector = Guice.createInjector(new AppInjector()); router.route(HttpMethod.GET, "/people").handler(injector.getInstance(PersonResource.class)::get); // Create the HTTP server and pass the "accept" method to the request handler. HttpServerOptions serverOptions = new HttpServerOptions(); serverOptions.setCompressionSupported(true); vertx.createHttpServer(serverOptions).requestHandler(router::accept) .listen(config().getInteger("http.port", 8080), result -> { if (result.succeeded()) { fut.complete(); } else { fut.fail(result.cause()); } }); }
From source file:com.cyngn.vertx.bosun.BosunPublisher.java
License:Apache License
/** * Put the metric data passed in, in the right format for vertx-bosun * * @param action the action desired, ie index or put * @param metric the metric name// w w w . j a v a2s. c o m * @param value the value * @param tags the tags associated * @param <T> the type of value int, double etc.. * @return the JsonObject representing the metric data */ private static <T> JsonObject getBosunMessage(String action, String metric, T value, JsonObject tags) { return new JsonObject().put(BosunReporter.ACTION_FIELD, action).put(OpenTsDbMetric.METRIC_FIELD, metric) .put(OpenTsDbMetric.VALUE_FIELD, value).put(OpenTsDbMetric.TAGS_FIELD, tags); }
From source file:com.cyngn.vertx.bosun.BosunReporter.java
License:Apache License
/** * Handles posting to the index endpoint * * @param message the message to send/*from www .j av a 2 s.c o m*/ */ private void doIndex(Message<JsonObject> message) { OpenTsDbMetric metric = getMetricFromMessage(message); if (metric == null) { return; } // ignore it we've seen it lately String key = metric.getDistinctKey(); if (distinctMetrics.getIfPresent(key) != null) { message.reply(new JsonObject().put(RESULT_FIELD, BosunResponse.EXISTS_MSG)); return; } // cache it distinctMetrics.put(key, true); metricsIndexed.incrementAndGet(); sendData(INDEX_API, metric.asJson().encode(), message); }
From source file:com.cyngn.vertx.bosun.BosunReporter.java
License:Apache License
/** * Send data to the bosun instance/* www. ja v a 2 s . c o m*/ * * @param api the api on bosun to send to * @param data the json data to send * @param message the event bus message the request originated from */ private void sendData(String api, String data, Message message) { HttpClient client = getNextHost(); Buffer buffer = Buffer.buffer(data.getBytes()); client.post(api).exceptionHandler(error -> { sendError(message, "Got ex contacting bosun, " + error.getLocalizedMessage()); }).handler(response -> { int statusCode = response.statusCode(); // is it 2XX if (statusCode >= HttpResponseStatus.OK.code() && statusCode < HttpResponseStatus.MULTIPLE_CHOICES.code()) { message.reply(new JsonObject().put(RESULT_FIELD, BosunResponse.OK_MSG)); } else { response.bodyHandler(responseData -> { sendError(message, "got non 200 response from bosun, error: " + responseData, statusCode); }); } }).setTimeout(timeout).putHeader(HttpHeaders.CONTENT_LENGTH, buffer.length() + "") .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).write(buffer).end(); }
From source file:com.cyngn.vertx.bosun.OpenTsDbMetric.java
License:Apache License
/** * Get the object as a vertx JsonOBject//from w w w. j a va2 s . c om * * @return the metric data in Json form */ public JsonObject asJson() { JsonObject obj = new JsonObject().put(METRIC_FIELD, metric).put(VALUE_FIELD, value).put(TIMESTAMP_FIELD, timestamp); JsonObject tagsObj = new JsonObject(); for (String key : tags.fieldNames()) { tagsObj.put(key, tags.getValue(key)); } // there has to be at least one tag obj.put(TAGS_FIELD, tagsObj); return obj; }
From source file:com.cyngn.vertx.opentsdb.client.TsMetric.java
License:Apache License
/** * Convert this object to a metric JsonObject suitable for transferring via the EventBus. * * @return the TsMetric in JsonObject form *///from w w w . j ava2 s . c om public JsonObject asJson() { JsonObject jsonObject = new JsonObject().put(MetricsParser.NAME_FIELD, name).put(MetricsParser.VALUE_FIELD, value); if (tags != null && tags.size() > 0) { JsonObject tagMap = new JsonObject(); for (Map.Entry<String, String> entry : tags.entrySet()) { tagMap.put(entry.getKey(), entry.getValue()); } jsonObject.put(MetricsParser.TAGS_FIELD, tagMap); } return jsonObject; }
From source file:com.cyngn.vertx.opentsdb.service.client.OpenTsDbClient.java
License:Apache License
/** * Reading data when you haven't sent a command in OpenTsDb means there are errors coming back from an agent * * @param buffer//from w w w. ja v a 2 s . c o m */ private void onDataReceived(Buffer buffer) { String openTsDbError = buffer.toString(StandardCharsets.UTF_8.toString()); logger.error("Got data from agent: " + openTsDbError + " this is not expected"); // let the user know if they failed to write because the data is invalid bus.send(OpenTsDbService.ERROR_MESSAGE_ADDRESS, new JsonObject().put("error", EventBusMessage.INVALID_DATA).put("message", openTsDbError)); errorsReceived++; }
From source file:com.cyngn.vertx.opentsdb.service.MetricsProcessor.java
License:Apache License
private Buffer write(MetricsSender sender, Buffer data) { boolean success = sender.write(data); if (!success) { bus.send(OpenTsDbService.ERROR_MESSAGE_ADDRESS, new JsonObject().put("error", EventBusMessage.WRITE_FAILURE.toString())); }/*from w w w . jav a 2 s. com*/ return Buffer.buffer(); }
From source file:com.cyngn.vertx.opentsdb.Util.java
License:Apache License
/** * Create a metric object for sending to the OpenTsDb lib * * @param name the metric name/* w w w. j ava2 s.co m*/ * @param value the metric value * @param tags the tags to associate to this metric * @return the properly constructed metric object */ public static JsonObject createMetric(String name, String value, JsonObject tags) { JsonObject obj = new JsonObject().put(OpenTsDbService.ACTION_FIELD, OpenTsDbService.ADD_COMMAND) .put(MetricsParser.NAME_FIELD, name).put(MetricsParser.VALUE_FIELD, value); if (tags != null && tags.size() > 0) { obj.put(MetricsParser.TAGS_FIELD, tags); } return obj; }
From source file:com.cyngn.vertx.opentsdb.Util.java
License:Apache License
/** * Create a metric object for sending to the OpenTsDb lib but with no implicit command * * @param name the metric name//from w w w . j av a2 s .c om * @param value the metric value * @param tags the tags to associate to this metric * @return the properly constructed metric object */ public static JsonObject createRawMetric(String name, String value, JsonObject tags) { JsonObject obj = new JsonObject().put(MetricsParser.NAME_FIELD, name).put(MetricsParser.VALUE_FIELD, value); if (tags != null && tags.size() > 0) { obj.put(MetricsParser.TAGS_FIELD, tags); } return obj; }