コード例 #1
0
  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);
  }
コード例 #2
0
ファイル: ClientExample.java プロジェクト: yeison/vert.x
 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);
                     }
                   });
             }
           });
 }
コード例 #3
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;
  }
  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);
  }