Beispiel #1
0
  /** Generic method that posts a plugin to the metrics website */
  private void postPlugin(boolean isPing) throws IOException {
    // The plugin's description file containg all of the plugin data such as name, version, author,
    // etc
    final PluginDescriptionFile description = this.plugin.getDescription();

    // Construct the post data
    String data =
        Metrics.encode("guid")
            + '='
            + Metrics.encode(this.guid)
            + Metrics.encodeDataPair("version", description.getVersion())
            + Metrics.encodeDataPair("server", Bukkit.getVersion())
            + Metrics.encodeDataPair(
                "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length))
            + Metrics.encodeDataPair("revision", String.valueOf(Metrics.REVISION));

    // If we're pinging, append it
    if (isPing) {
      data += Metrics.encodeDataPair("ping", "true");
    }

    // Create the url
    final URL url =
        new URL(
            Metrics.BASE_URL
                + String.format(Metrics.REPORT_URL, this.plugin.getDescription().getName()));

    // 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 (this.isMineshafterPresent()) {
      connection = url.openConnection(Proxy.NO_PROXY);
    } else {
      connection = url.openConnection();
    }

    connection.setDoOutput(true);

    // Write the data
    final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(data);
    writer.flush();

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

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

    if (response.startsWith("ERR")) {
      throw new IOException(response); // Throw the exception
    }
    // if (response.startsWith("OK")) - We should get "OK" followed by an optional description if
    // everything goes right
  }
Beispiel #2
0
 /**
  * Encode a key/value data pair to be used in a HTTP post request. This INCLUDES a & so the first
  * key/value pair MUST be included manually, e.g: <code>
  * String httpData = encode("guid") + '=' + encode("1234") + encodeDataPair("authors") + "..";
  * </code>
  *
  * @param key
  * @param value
  * @return
  */
 private static String encodeDataPair(String key, String value)
     throws UnsupportedEncodingException {
   return '&' + Metrics.encode(key) + '=' + Metrics.encode(value);
 }