示例#1
0
  public void start() throws Exception {

    HttpClient client = vertx.createHttpClient().setPort(8080).setHost("localhost");

    final HttpClientRequest req =
        client.put(
            "/some-url",
            new Handler<HttpClientResponse>() {
              public void handle(HttpClientResponse response) {
                System.out.println("File uploaded " + response.statusCode);
              }
            });

    String filename = "upload/upload.txt";

    // For a non-chunked upload you need to specify size of upload up-front
    req.headers().put("Content-Length", Files.size(Paths.get(filename)));

    // For a chunked upload you don't need to specify size, just do:
    // req.setChunked(true);

    vertx
        .fileSystem()
        .open(
            filename,
            new AsyncResultHandler<AsyncFile>() {
              public void handle(AsyncResult<AsyncFile> ar) {
                final AsyncFile file = ar.result;
                Pump pump = Pump.createPump(file.getReadStream(), req);
                pump.start();

                file.getReadStream()
                    .endHandler(
                        new SimpleHandler() {
                          public void handle() {

                            file.close(
                                new AsyncResultHandler<Void>() {
                                  public void handle(AsyncResult<Void> ar) {
                                    if (ar.exception == null) {
                                      req.end();
                                      System.out.println("Sent request");
                                    } else {
                                      ar.exception.printStackTrace(System.err);
                                    }
                                  }
                                });
                          }
                        });
              }

              public void onException(Exception e) {
                e.printStackTrace();
              }
            });
  }
  private void send(Message<JsonObject> message, JsonObject notif, String apiKey) {
    logger.debug("Sending POST to: " + gcm_url + " port:" + gcm_port + " with body: " + notif);

    HttpClient client = vertx.createHttpClient().setHost(uri.getHost()).setPort(gcm_port);

    if (gcm_url.toLowerCase().startsWith("https")) {
      client = client.setSSL(true).setTrustAll(true);
    }

    ResponseHelper helper = new ResponseHelper(gcm_min_backoff_delay);

    submitGCM(helper, notif, apiKey, client, message, 0);
  }
 private void listTagValues(final Message<JsonObject> message) {
   HttpClientRequest request =
       client.get(
           TAG_VALUES_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 listing tag values: "
                                 + response.statusCode()
                                 + " "
                                 + response.statusMessage();
                         container.logger().error(errorMessage);
                         sendError(message, errorMessage);
                       }
                     }
                   });
             }
           });
   request.end();
 }
  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);
  }
 private void deleteMetric(final Message<JsonObject> message) {
   String metricName = message.body().getString("metric_name");
   if (metricName == null) {
     sendError(message, "metric name must be specified");
     return;
   }
   HttpClientRequest request =
       client.delete(
           String.format(DELETE_METRIC_URI, metricName),
           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 metric: "
                                 + response.statusCode()
                                 + " "
                                 + response.statusMessage();
                         container.logger().error(errorMessage);
                         sendError(message, errorMessage);
                       }
                     }
                   });
             }
           });
   request.end();
 }
 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 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);
 }
示例#8
0
 private void makeRequest() {
   if (start == 0) {
     start = System.currentTimeMillis();
   }
   while (requestCredits > 0) {
     startTimeMs.add(System.currentTimeMillis());
     client.getNow("/", this);
     requestCredits--;
   }
 }
示例#9
0
  public static HttpClient client(Vertx vertx) {
    HttpClient client =
        vertx
            .createHttpClient()
            .setHost(remoteHost())
            .setPort(remotePort())
            // .setKeepAlive(false)
            .setConnectTimeout(10000);

    if (remotePort() == 443) {
      client.setSSL(true).setTrustAll(true).setVerifyHost(false);
    }

    client.exceptionHandler(
        new Handler<Exception>() {
          public void handle(Exception e) {
            e.printStackTrace();
          }
        });

    return client;
  }
示例#10
0
  private void connect() {
    if (!connecting) {
      // We defer actual connection until the first part of body is written or end is called
      // This gives the user an opportunity to set an exception handler before connecting so
      // they can capture any exceptions on connection
      client.getConnection(
          new Handler<ClientConnection>() {
            public void handle(ClientConnection conn) {
              connected(conn);
            }
          },
          context);

      connecting = true;
    }
  }
示例#11
0
 public void start() {
   client = new HttpClient();
   client
       .setPort(8080)
       .setHost("localhost")
       .getNow(
           "/",
           new Handler<HttpClientResponse>() {
             public void handle(HttpClientResponse response) {
               response.bodyHandler(
                   new Handler<Buffer>() {
                     public void handle(Buffer data) {
                       System.out.println(data);
                     }
                   });
             }
           });
 }
  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();
  }
示例#13
0
 public void start() {
   client = new HttpClient();
   client
       .setSSL(true)
       .setTrustAll(true)
       .setPort(4443)
       .setHost("localhost")
       .getNow(
           "/",
           new Handler<HttpClientResponse>() {
             public void handle(HttpClientResponse response) {
               response.dataHandler(
                   new Handler<Buffer>() {
                     public void handle(Buffer data) {
                       System.out.println(data);
                     }
                   });
             }
           });
 }
示例#14
0
  public Buffer getModule(final String moduleName) {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Buffer> mod = new AtomicReference<>();
    HttpClient client = vertx.createHttpClient();
    if (proxyHost != null) {
      client.setHost(proxyHost);
      if (proxyPort != 80) {
        client.setPort(proxyPort);
      } else {
        client.setPort(80);
      }
    } else {
      client.setHost(repoHost);
      client.setPort(repoPort);
    }
    client.exceptionHandler(
        new Handler<Exception>() {
          public void handle(Exception e) {
            log.error("Unable to connect to repository");
            latch.countDown();
          }
        });
    String uri = getRepoURI(moduleName);

    if (proxyHost != null) {
      // We use an absolute URI
      uri = new StringBuilder("http://").append(repoHost).append(uri).toString();
    }
    final String theURI = uri;
    HttpClientRequest req =
        client.get(
            uri,
            new Handler<HttpClientResponse>() {
              public void handle(HttpClientResponse resp) {
                if (resp.statusCode == 200) {
                  String msg =
                      "Downloading module "
                          + moduleName
                          + " from http://"
                          + repoHost
                          + ":"
                          + repoPort
                          + theURI;
                  if (proxyHost != null) {
                    msg += " Using proxy host " + proxyHost + ":" + proxyPort;
                  }
                  log.info(msg);
                  resp.bodyHandler(
                      new Handler<Buffer>() {
                        public void handle(Buffer buffer) {
                          mod.set(buffer);
                          latch.countDown();
                        }
                      });
                } else if (resp.statusCode == 404) {
                  latch.countDown();
                } else {
                  log.error("Failed to query repository: " + resp.statusCode);
                  latch.countDown();
                }
              }
            });
    if (proxyHost != null) {
      req.putHeader("host", proxyHost);
    } else {
      req.putHeader("host", repoHost);
    }
    req.putHeader("user-agent", "Vert.x Module Installer");
    req.end();
    while (true) {
      try {
        if (!latch.await(30, TimeUnit.SECONDS)) {
          throw new IllegalStateException("Timed out waiting to download module");
        }
        break;
      } catch (InterruptedException ignore) {
      }
    }
    return mod.get();
  }
  public void start() {

    // handler config JSON
    JsonObject config = container.config();

    int port = config.getNumber("hec_port").intValue();
    int poolsize = config.getNumber("hec_poolsize").intValue();
    this.token = config.getString("hec_token");
    this.index = config.getString("index");
    this.source = config.getString("source");
    this.sourcetype = config.getString("sourcetype");
    boolean useHTTPs = config.getBoolean("hec_https");

    this.hecBatchMode = config.getBoolean("hec_batch_mode");
    this.hecMaxBatchSizeBytes = config.getNumber("hec_max_batch_size_bytes").longValue();
    this.hecMaxBatchSizeEvents = config.getNumber("hec_max_batch_size_events").longValue();
    this.hecMaxInactiveTimeBeforeBatchFlush =
        config.getNumber("hec_max_inactive_time_before_batch_flush").longValue();

    this.batchBuffer = Collections.synchronizedList(new LinkedList<String>());
    this.lastEventReceivedTime = System.currentTimeMillis();

    // Event Bus (so we can receive the data)
    String eventBusAddress = config.getString("output_address");
    EventBus eb = vertx.eventBus();

    client = vertx.createHttpClient().setPort(port).setHost("localhost").setMaxPoolSize(poolsize);
    if (useHTTPs) {
      client.setSSLContext(getSSLContext());
      client.setVerifyHost(false);
      client.setSSL(true);
      client.setTrustAll(true);
    }

    // data handler that will process our received data
    Handler<Message<String>> myHandler =
        new Handler<Message<String>>() {

          public void handle(Message<String> message) {

            try {

              String messageContent = escapeMessageIfNeeded(message.body());

              StringBuffer json = new StringBuffer();
              json.append("{\"").append("event\":").append(messageContent).append(",\"");

              if (!index.equalsIgnoreCase("default"))
                json.append("index\":\"").append(index).append("\",\"");

              json.append("source\":\"")
                  .append(source)
                  .append("\",\"")
                  .append("sourcetype\":\"")
                  .append(sourcetype)
                  .append("\"")
                  .append("}");

              String bodyContent = json.toString();

              if (hecBatchMode) {
                lastEventReceivedTime = System.currentTimeMillis();
                currentBatchSizeBytes += bodyContent.length();
                batchBuffer.add(bodyContent);
                if (flushBuffer()) {
                  bodyContent = rollOutBatchBuffer();
                  batchBuffer.clear();
                  currentBatchSizeBytes = 0;
                  hecPost(bodyContent);
                }
              } else {
                hecPost(bodyContent);
              }

            } catch (Exception e) {
              logger.error("Error writing received data: " + ModularInput.getStackTrace(e));
            }
          }

          /**
           * from Tivo
           *
           * @param message
           * @return
           */
          private String escapeMessageIfNeeded(String message) {
            String trimmedMessage = message.trim();
            if (trimmedMessage.startsWith("{") && trimmedMessage.endsWith("}")) {
              // this is *probably* JSON.
              return trimmedMessage;
            } else if (trimmedMessage.startsWith("\"")
                && trimmedMessage.endsWith("\"")
                && !message.substring(1, message.length() - 1).contains("\"")) {
              // this appears to be a quoted string with no internal
              // quotes
              return trimmedMessage;
            } else {
              // don't know what this thing is, so need to escape all
              // quotes, and
              // then wrap the result in quotes
              return "\"" + message.replace("\"", "\\\"") + "\"";
            }
          }
        };

    if (hecBatchMode) {
      new BatchBufferActivityCheckerThread().start();
    }
    // start listening for data
    eb.registerHandler(eventBusAddress, myHandler);
  }
示例#16
0
 public void stop() {
   client.close();
 }
  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();
  }
示例#18
0
 @Override
 public void stop() {
   if (client != null) {
     client.close();
   }
 }