Esempio n. 1
0
  /**
   * Sends the planner metrics to the metrics server
   *
   * @param metrics the metrics to log
   * @param url the url to send the metrics to
   */
  private SendMetricsResult send(PlannerMetrics metrics, String url) throws IOException {
    SendMetricsResult result = new SendMetricsResult();

    URL u = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setDoOutput(true);
    // connection.setDoInput( true );
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Content-Type", "application/json");

    try {
      OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

      try {
        // String payload = URLEncoder.encode( metrics.toJson(), "UTF-8") ;
        String payload = metrics.toJson();
        out.write(payload);
      } finally {
        out.close();
      }

      result.setCode(connection.getResponseCode());
      result.setResponseMessage(connection.getResponseMessage());

      /*
      BufferedReader in = new BufferedReader(
                              new InputStreamReader(
                               connection.getInputStream()));
      String result;
      while (( result = in.readLine()) != null) {
          System.out.println( result );
      }*/
    } finally {
      connection.disconnect();
    }

    return result;
  }