Example #1
0
  @Override
  public void run() {
    this.sender.sendMessage(Text.of(TextColors.GREEN, "Preparing Timings Report..."));

    this.out.add("data", JSONUtil.mapArray(this.history, TimingHistory::export));

    String response = null;
    try {
      HttpURLConnection con =
          (HttpURLConnection) new URL("http://timings.aikar.co/post").openConnection();
      con.setDoOutput(true);
      con.setRequestProperty(
          "User-Agent",
          "Sponge/" + getServerName() + "/" + InetAddress.getLocalHost().getHostName());
      con.setRequestMethod("POST");
      con.setInstanceFollowRedirects(false);

      OutputStream request =
          new GZIPOutputStream(con.getOutputStream()) {

            {
              this.def.setLevel(7);
            }
          };

      request.write(JSONUtil.toString(this.out).getBytes("UTF-8"));
      request.close();

      response = getResponse(con);

      if (con.getResponseCode() != 302) {
        this.sender.sendMessage(
            Text.of(
                TextColors.RED,
                "Upload Error: " + con.getResponseCode() + ": " + con.getResponseMessage()));
        this.sender.sendMessage(Text.of(TextColors.RED, "Check your logs for more information"));
        if (response != null) {
          SpongeImpl.getLogger().fatal(response);
        }
        return;
      }

      String location = con.getHeaderField("Location");
      this.sender.sendMessage(
          Text.of(
              TextColors.GREEN,
              "View Timings Report: ",
              TextActions.openUrl(new URL(location)),
              location));
      if (!(this.sender instanceof ConsoleSource)) {
        SpongeImpl.getLogger().info("View Timings Report: " + location);
      }

      if (response != null && !response.isEmpty()) {
        SpongeImpl.getLogger().info("Timing Response: " + response);
      }
    } catch (IOException ex) {
      this.sender.sendMessage(
          Text.of(TextColors.RED, "Error uploading timings, check your logs for more information"));
      if (response != null) {
        SpongeImpl.getLogger().fatal(response);
      }
      SpongeImpl.getLogger().fatal("Could not paste timings", ex);
    }
  }