Ejemplo n.º 1
0
  private void addDataPoints(final Message<JsonObject> message) {
    HttpClientRequest request =
        client.post(
            ADD_DATAPOINTS_URI,
            new Handler<HttpClientResponse>() {
              @Override
              public void handle(HttpClientResponse response) {
                int responseCode = response.statusCode();
                if (responseCode == 204) {
                  sendOK(message);
                } else {
                  String errorMessage =
                      "error adding data points: "
                          + response.statusCode()
                          + " "
                          + response.statusMessage();
                  container.logger().error(errorMessage);
                  sendError(message, errorMessage);
                }
              }
            });

    JsonObject dataPoints = message.body().getObject("datapoints");
    if (dataPoints == null) {
      sendError(message, "data points object is not specified");
      return;
    }
    JsonValidator validator = new JsonValidator();
    if (!validator.validateDataPoints(dataPoints)) {
      sendError(message, "data points object was incorrectly formatted");
      return;
    }
    writeObject(message, request, dataPoints);
  }
Ejemplo n.º 2
0
 private void deleteDataPoints(final Message<JsonObject> message) {
   JsonObject query = message.body().getObject("query");
   if (query == null) {
     sendError(message, "metric query must be specified");
     return;
   }
   HttpClientRequest request =
       client.post(
           DELETE_DATAPOINTS_URI,
           new Handler<HttpClientResponse>() {
             @Override
             public void handle(final HttpClientResponse response) {
               response.bodyHandler(
                   new Handler<Buffer>() {
                     public void handle(Buffer body) {
                       int responseCode = response.statusCode();
                       if (responseCode == 204) {
                         sendOK(message);
                       } else {
                         String errorMessage =
                             "error deleting data points: "
                                 + response.statusCode()
                                 + " "
                                 + response.statusMessage();
                         container.logger().error(errorMessage);
                         sendError(message, errorMessage);
                       }
                     }
                   });
             }
           });
   writeObject(message, request, query);
 }
Ejemplo n.º 3
0
 private void queryMetricTags(final Message<JsonObject> message) {
   JsonObject query = message.body().getObject("query");
   if (query == null) {
     sendError(message, "metric query must be specified");
     return;
   }
   HttpClientRequest request =
       client.post(
           QUERY_DATAPOINTS_TAGS_URI,
           new Handler<HttpClientResponse>() {
             @Override
             public void handle(final HttpClientResponse response) {
               response.bodyHandler(
                   new Handler<Buffer>() {
                     public void handle(Buffer body) {
                       int responseCode = response.statusCode();
                       if (responseCode == 200) {
                         JsonObject responseObject = new JsonObject(body.toString());
                         sendOK(message, responseObject);
                       } else {
                         String errorMessage =
                             "error querying metric tags: "
                                 + response.statusCode()
                                 + " "
                                 + response.statusMessage();
                         container.logger().error(errorMessage);
                         sendError(message, errorMessage);
                       }
                     }
                   });
             }
           });
   writeObject(message, request, query);
 }
  private void hecPost(String bodyContent) throws Exception {

    Buffer buff = new Buffer();
    buff.appendString(bodyContent);

    HttpClientRequest request =
        client.post(
            "/services/collector",
            new Handler<HttpClientResponse>() {
              public void handle(HttpClientResponse resp) {
                if (resp.statusCode() != 200) logger.error("Got a response: " + resp.statusCode());
              }
            });
    request.headers().set("Authorization", "Splunk " + token);
    request.headers().set("Content-Length", String.valueOf(bodyContent.length()));
    request.write(buff);

    request.end();
  }
Ejemplo n.º 5
0
  private void submitGCM(
      final ResponseHelper helper,
      final JsonObject notif,
      final String apiKey,
      final HttpClient client,
      final Message<JsonObject> message,
      final int attempt) {
    final Buffer toSend;
    try {
      toSend = new Buffer(notif.encode().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      logger.error(e.getMessage());
      return;
    }
    logger.debug(
        "Attempt #"
            + (attempt + 1)
            + " to send notification to regIds "
            + notif.getArray("registration_ids").encode());
    HttpClientRequest request =
        client
            .post(
                uri.getPath(),
                new Handler<HttpClientResponse>() {
                  @Override
                  public void handle(final HttpClientResponse resp) {
                    final Buffer body = new Buffer(0);
                    resp.dataHandler(
                        new Handler<Buffer>() {
                          @Override
                          public void handle(Buffer data) {
                            body.appendBuffer(data);
                          }
                        });
                    resp.endHandler(
                        new VoidHandler() {
                          @Override
                          public void handle() {
                            boolean tryAgain = false;
                            JsonObject[] reply = {null};
                            JsonObject newNotif = new JsonObject();
                            int status = resp.statusCode();
                            if (status == 200) {
                              logger.debug("GCM response: " + body);
                              reply[0] = new JsonObject(new String(body.getBytes()));
                            } else {
                              logger.error("GCM error response: " + body);
                            }
                            if (reply[0] != null) {
                              helper.setMulticastId(
                                  reply[0].getLong("multicast_id") == null
                                      ? 0
                                      : reply[0].getLong("multicast_id"));
                              newNotif = updateStatus(notif, helper.getResponse(), reply[0]);
                              tryAgain =
                                  newNotif.getArray("registration_ids").size() != 0
                                      && attempt < gcm_backoff_retries;
                            } else {
                              tryAgain = attempt < gcm_backoff_retries;
                            }
                            if (tryAgain) {
                              int sleepTime =
                                  helper.getBackoff() / 2
                                      + helper.getRandom().nextInt(helper.getBackoff());
                              try {
                                Thread.sleep(sleepTime);
                              } catch (InterruptedException ie) {
                              }
                              if (2 * helper.getBackoff() < gcm_max_backoff_delay) {
                                helper.setBackoff(helper.getBackoff() * 2);
                              }
                              submitGCM(helper, newNotif, apiKey, client, message, attempt + 1);
                            } else {
                              if (helper.getResponse().isEmpty()) {
                                // all JSON posts failed due to GCM unavailability
                                sendError(message, "GCM is unavailable");
                              } else {
                                //                                JsonObject sendBack =
                                // calculateSummary( message.body().getObject( "notification"
                                // ).getArray( "registration_ids" ), helper.getResponse(),
                                // helper.getMulticastId() );
                                JsonObject sendBack =
                                    calculateSummary(
                                        notif.getArray("registration_ids"),
                                        helper.getResponse(),
                                        helper.getMulticastId());
                                sendOK(message, sendBack);
                              }
                            }
                          }
                        });
                  }
                })
            .putHeader("Content-Type", "application/json")
            .putHeader("Content-Length", String.valueOf(toSend.length()))
            .putHeader("Authorization", "key=" + apiKey)
            .write(toSend);

    request.end();
  }