Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  @Override
  public Response serve(String uri, String method, Properties header, Properties parms) {
    String callback = parms.getProperty("callback");

    if (inst.whitelist.size() > 0 && !inst.whitelist.contains(header.get("X-REMOTE-ADDR"))) {
      outLog.warning(
          "[JSONAPI] An API call from "
              + header.get("X-REMOTE-ADDR")
              + " was blocked because "
              + header.get("X-REMOTE-ADDR")
              + " is not on the whitelist.");
      return jsonRespone(
          returnAPIError("", "You are not allowed to make API calls."), callback, HTTP_FORBIDDEN);
    }

    if (uri.equals("/api/subscribe")) {
      String source = parms.getProperty("source");
      String key = parms.getProperty("key");

      if (!testLogin(source, key)) {
        info("[Streaming API] " + header.get("X-REMOTE-ADDR") + ": Invalid API Key.");
        return jsonRespone(returnAPIError(source, "Invalid API key."), callback, HTTP_FORBIDDEN);
      }

      info("[Streaming API] " + header.get("X-REMOTE-ADDR") + ": source=" + source);

      try {
        if (source == null) {
          throw new Exception();
        }

        StreamingResponse out = new StreamingResponse(inst, source, callback);

        return new NanoHTTPD.Response(HTTP_OK, MIME_PLAINTEXT, out);
      } catch (Exception e) {
        e.printStackTrace();
        return jsonRespone(
            returnAPIError(source, "'" + source + "' is not a valid stream source!"),
            callback,
            HTTP_NOTFOUND);
      }
    }

    if (!uri.equals("/api/call") && !uri.equals("/api/call-multiple")) {
      return new NanoHTTPD.Response(HTTP_NOTFOUND, MIME_PLAINTEXT, "File not found.");
    }

    Object args = parms.getProperty("args", "[]");
    String calledMethod = (String) parms.getProperty("method");

    if (calledMethod == null) {
      info("[API Call] " + header.get("X-REMOTE-ADDR") + ": Parameter 'method' was not defined.");
      return jsonRespone(
          returnAPIError("", "Parameter 'method' was not defined."), callback, HTTP_NOTFOUND);
    }

    String key = parms.getProperty("key");
    if (!inst.method_noauth_whitelist.contains(calledMethod) && !testLogin(calledMethod, key)) {
      info("[API Call] " + header.get("X-REMOTE-ADDR") + ": Invalid API Key.");
      return jsonRespone(returnAPIError("", "Invalid API key."), callback, HTTP_FORBIDDEN);
    }

    info(
        "[API Call] "
            + header.get("X-REMOTE-ADDR")
            + ": method="
            + parms.getProperty("method").concat("?args=").concat((String) args));

    if (args == null || calledMethod == null) {
      return jsonRespone(
          returnAPIError(calledMethod, "You need to pass a method and an array of arguments."),
          callback,
          HTTP_NOTFOUND);
    } else {
      try {
        JSONParser parse = new JSONParser();
        args = parse.parse((String) args);

        if (uri.equals("/api/call-multiple")) {
          List<String> methods = new ArrayList<String>();
          List<Object> arguments = new ArrayList<Object>();
          Object o = parse.parse(calledMethod);
          if (o instanceof List<?> && args instanceof List<?>) {
            methods = (List<String>) o;
            arguments = (List<Object>) args;
          } else {
            return jsonRespone(
                returnAPIException(
                    calledMethod,
                    new Exception("method and args both need to be arrays for /api/call-multiple")),
                callback);
          }

          int size = methods.size();
          JSONArray arr = new JSONArray();
          for (int i = 0; i < size; i++) {
            arr.add(
                serveAPICall(
                    methods.get(i),
                    (arguments.size() - 1 >= i ? arguments.get(i) : new ArrayList<Object>())));
          }

          return jsonRespone(returnAPISuccess(o, arr), callback);
        } else {
          return jsonRespone(serveAPICall(calledMethod, args), callback);
        }
      } catch (Exception e) {
        return jsonRespone(returnAPIException(calledMethod, e), callback);
      }
    }
  }
Exemplo n.º 2
0
  public static Object handle(Object obj) {
    if (obj instanceof World.Environment) {
      World.Environment e = (World.Environment) obj;
      if (e == World.Environment.NETHER) {
        return "nether";
      } else {
        return "normal";
      }
    } else if (obj instanceof File) {
      return ((File) obj).toString();
    } else if (obj instanceof Player) {
      Player p = (Player) obj;
      JSONObject o = new JSONObject();

      o.put("name", p.getName());
      o.put("op", p.isOp());
      o.put("health", p.getHealth());
      o.put("ip", p.getAddress().toString());
      o.put("itemInHand", p.getItemInHand());
      o.put("location", p.getLocation());
      o.put("inventory", p.getInventory());
      o.put("sneaking", p.isSneaking());
      o.put("inVehicle", p.isInsideVehicle());
      o.put("sleeping", p.isSleeping());
      o.put("world", p.getServer().getWorlds().indexOf(p.getWorld()));

      return o;
    } else if (obj instanceof Server) {
      Server s = (Server) obj;

      JSONObject o = new JSONObject();

      o.put("maxPlayers", s.getMaxPlayers());
      o.put("players", Arrays.asList(s.getOnlinePlayers()));
      o.put("port", s.getPort());
      o.put("name", s.getName());
      o.put("serverName", s.getServerName());
      o.put("version", s.getVersion());
      o.put("worlds", s.getWorlds());

      return o;
    } else if (obj instanceof World) {
      World w = (World) obj;

      JSONObject o = new JSONObject();

      o.put("environment", w.getEnvironment());
      o.put("fullTime", w.getFullTime());
      o.put("time", w.getTime());
      o.put("name", w.getName());
      o.put("isThundering", w.isThundering());
      o.put("hasStorm", w.hasStorm());

      return o;
    } else if (obj instanceof Plugin) {
      Plugin p = (Plugin) obj;
      PluginDescriptionFile d = p.getDescription();

      JSONObject o = new JSONObject();

      o.put("name", d.getName());
      o.put("description", d.getDescription());
      o.put("authors", d.getAuthors());
      o.put("version", d.getVersion());
      o.put("website", d.getWebsite());
      o.put("enabled", JSONAPI.instance.getServer().getPluginManager().isPluginEnabled(p));

      return o;
    } else if (obj instanceof ItemStack) {
      ItemStack i = (ItemStack) obj;

      JSONObject o = new JSONObject();

      o.put("type", i.getTypeId());
      o.put("durability", i.getDurability());
      o.put("amount", i.getAmount());

      return o;
    } else if (obj instanceof PlayerInventory) {
      PlayerInventory p = (PlayerInventory) obj;

      JSONObject o = new JSONObject();

      JSONObject armor = new JSONObject();
      armor.put("boots", p.getBoots());
      armor.put("chestplate", p.getChestplate());
      armor.put("helmet", p.getHelmet());
      armor.put("leggings", p.getLeggings());

      o.put("armor", armor);
      o.put("hand", p.getItemInHand());
      o.put("inventory", Arrays.asList(p.getContents()));

      return o;
    } else if (obj instanceof Location) {
      Location l = (Location) obj;

      JSONObject o = new JSONObject();

      o.put("x", l.getX());
      o.put("y", l.getY());
      o.put("z", l.getZ());
      o.put("pitch", l.getPitch());
      o.put("yaw", l.getYaw());

      return o;
    } else if (obj instanceof Plugin[]) {
      List<Plugin> l = Arrays.asList((Plugin[]) obj);

      Collections.sort(l, new PluginSorter());

      return l;
    } else if (obj instanceof Object[]) {
      int l = ((Object[]) obj).length;
      JSONArray a = new JSONArray();
      for (int i = 0; i < l; i++) {
        a.add(((Object[]) obj)[i]);
      }

      return a;
    }
    Logger.getLogger("JSONAPI").warning("Uncaugh object! Value:");
    Logger.getLogger("JSONAPI").warning(obj.toString());
    Logger.getLogger("JSONAPI").warning("Type:");
    Logger.getLogger("JSONAPI").warning(obj.getClass().getName());

    return new Object();
  }