예제 #1
0
  /** Generic method that posts a plugin to the metrics website */
  private void postPlugin(final boolean isPing) throws IOException {
    // Server software specific section
    PluginDescriptionFile description = plugin.getDescription();
    String pluginName = description.getName();
    boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled
    String pluginVersion = description.getVersion();
    String serverVersion = Bukkit.getVersion();
    int playersOnline = Bukkit.getServer().getOnlinePlayers().length;

    // END server software specific section -- all code below does not use any code outside of this
    // class / Java

    // Construct the post data
    StringBuilder json = new StringBuilder(1024);
    json.append('{');

    // The plugin's description file containg all of the plugin data such as name, version, author,
    // etc
    appendJSONPair(json, "guid", guid);
    appendJSONPair(json, "plugin_version", pluginVersion);
    appendJSONPair(json, "server_version", serverVersion);
    appendJSONPair(json, "players_online", Integer.toString(playersOnline));

    // New data as of R6
    String osname = System.getProperty("os.name");
    String osarch = System.getProperty("os.arch");
    String osversion = System.getProperty("os.version");
    String java_version = System.getProperty("java.version");
    int coreCount = Runtime.getRuntime().availableProcessors();

    // normalize os arch .. amd64 -> x86_64
    if (osarch.equals("amd64")) {
      osarch = "x86_64";
    }

    appendJSONPair(json, "osname", osname);
    appendJSONPair(json, "osarch", osarch);
    appendJSONPair(json, "osversion", osversion);
    appendJSONPair(json, "cores", Integer.toString(coreCount));
    appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0");
    appendJSONPair(json, "java_version", java_version);

    // If we're pinging, append it
    if (isPing) {
      appendJSONPair(json, "ping", "1");
    }

    if (graphs.size() > 0) {
      synchronized (graphs) {
        json.append(',');
        json.append('"');
        json.append("graphs");
        json.append('"');
        json.append(':');
        json.append('{');

        boolean firstGraph = true;

        final Iterator<Graph> iter = graphs.iterator();

        while (iter.hasNext()) {
          Graph graph = iter.next();

          StringBuilder graphJson = new StringBuilder();
          graphJson.append('{');

          for (Plotter plotter : graph.getPlotters()) {
            appendJSONPair(
                graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue()));
          }

          graphJson.append('}');

          if (!firstGraph) {
            json.append(',');
          }

          json.append(escapeJSON(graph.getName()));
          json.append(':');
          json.append(graphJson);

          firstGraph = false;
        }

        json.append('}');
      }
    }

    // close json
    json.append('}');

    // Create the url
    URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName)));

    // Connect to the website
    URLConnection connection;

    // Mineshafter creates a socks proxy, so we can safely bypass it
    // It does not reroute POST requests so we need to go around it
    if (isMineshafterPresent()) {
      connection = url.openConnection(Proxy.NO_PROXY);
    } else {
      connection = url.openConnection();
    }

    byte[] uncompressed = json.toString().getBytes();
    byte[] compressed = gzip(json.toString());

    // Headers
    connection.addRequestProperty("User-Agent", "MCStats/" + REVISION);
    connection.addRequestProperty("Content-Type", "application/json");
    connection.addRequestProperty("Content-Encoding", "gzip");
    connection.addRequestProperty("Content-Length", Integer.toString(compressed.length));
    connection.addRequestProperty("Accept", "application/json");
    connection.addRequestProperty("Connection", "close");

    connection.setDoOutput(true);

    if (debug) {
      System.out.println(
          "[Metrics] Prepared request for "
              + pluginName
              + " uncompressed="
              + uncompressed.length
              + " compressed="
              + compressed.length);
    }

    // Write the data
    OutputStream os = connection.getOutputStream();
    os.write(compressed);
    os.flush();

    // Now read the response
    final BufferedReader reader =
        new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String response = reader.readLine();

    // close resources
    os.close();
    reader.close();

    if (response == null || response.startsWith("ERR") || response.startsWith("7")) {
      if (response == null) {
        response = "null";
      } else if (response.startsWith("7")) {
        response = response.substring(response.startsWith("7,") ? 2 : 1);
      }

      throw new IOException(response);
    } else {
      // Is this the first update this hour?
      if (response.equals("1") || response.contains("This is your first update this hour")) {
        synchronized (graphs) {
          final Iterator<Graph> iter = graphs.iterator();

          while (iter.hasNext()) {
            final Graph graph = iter.next();

            for (Plotter plotter : graph.getPlotters()) {
              plotter.reset();
            }
          }
        }
      }
    }
  }