예제 #1
0
 /**
  * Parse JSON text into java object from the input source. Please use parseWithException() if you
  * don't want to ignore the exception.
  *
  * @see org.json.simpleForBukkit.parser.JSONParser#parse(Reader)
  * @see #parseWithException(Reader)
  * @param in
  * @return Instance of the following: org.json.simpleForBukkit.JSONObject,
  *     org.json.simpleForBukkit.JSONArray, java.lang.String, java.lang.Number, java.lang.Boolean,
  *     null
  */
 public static Object parse(Reader in) {
   try {
     JSONParser parser = new JSONParser();
     return parser.parse(in);
   } catch (Exception e) {
     return null;
   }
 }
예제 #2
0
 public static Object parseWithException(String s) throws ParseException {
   JSONParser parser = new JSONParser();
   return parser.parse(s);
 }
예제 #3
0
 /**
  * Parse JSON text into java object from the input source.
  *
  * @see org.json.simpleForBukkit.parser.JSONParser
  * @param in
  * @return Instance of the following: org.json.simpleForBukkit.JSONObject,
  *     org.json.simpleForBukkit.JSONArray, java.lang.String, java.lang.Number, java.lang.Boolean,
  *     null
  * @throws IOException
  * @throws ParseException
  */
 public static Object parseWithException(Reader in) throws IOException, ParseException {
   JSONParser parser = new JSONParser();
   return parser.parse(in);
 }
예제 #4
0
파일: JSONServer.java 프로젝트: Xon/jsonapi
  @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);
      }
    }
  }