Exemple #1
0
  public static JSONObject getVersionInfo() {
    synchronized (LOGGER) {
      final JSONObject o = new JSONObject();
      try {
        final InputStream stream = Stats.class.getResourceAsStream("/version.prop");
        if (props == null) {
          props = new Properties();
          try {
            props.load(stream);
            stream.close();
          } catch (Exception e) {
            LOGGER.warn(e, e);
            props = null;
          }
        }
        props.put("uptime", Long.toString(MonitorApplication.getUptime()));

        final List<CacheDataModel> cwProps = CacheWatcher.getProps();
        for (CacheDataModel m : cwProps) {
          props.put(m.getKey(), m.getValue());
        }
        o.put("stats", props);

      } catch (JSONException e) {
        LOGGER.warn(e, e);
      }
      return o;
    }
  }
  /** Read the Request and construct an appropriate ResourceStream to respond with. */
  public void buildResourceStream() {
    JSONObject json;
    String cmd = null, id = null;
    JSONArray paramArray = null;

    HttpServletRequest req =
        ((ServletWebRequest) RequestCycle.get().getRequest()).getContainerRequest();
    BufferedReader reader = null;
    try {
      ServletInputStream sis = req.getInputStream();
      reader = new BufferedReader(new InputStreamReader(sis, "UTF-8"));
      // Used for debugging:
      // reader.mark(10);
      // if (reader.read() == -1) {
      // LOG.error("No request seen");
      // }
      // reader.reset();

      json = new JSONObject(new JSONTokener(reader));
      // LOG.debug("JSON Object: {}", json);

      id = json.getString("id");
      cmd = json.getString("method");
      paramArray = json.getJSONArray("params");

    } catch (IOException e) {
      jsonError("I/O exception while parsing");
    } catch (JSONException e) {
      jsonError("Could not parse command");
    } finally {
      try {
        if (reader != null) reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    if (paramArray == null) {
      handleEmptyCheckList(cmd, id);
    } else if ("checkWords".equals(cmd)) {
      doSpell(cmd, id, paramArray);
    } else if ("getSuggestions".equals(cmd)) {
      doSuggest(cmd, id, paramArray);
    } else {
      jsonError("Unknown command");
    }

    // LOG.debug("Processed command {}; output will be {}", cmd,
    // resourceStream.asString());
  }
  private void respond(Iterator<String> words, String cmd, String id) {
    JSONArray array = new JSONArray();
    if (words != null) {
      while (words.hasNext()) array.put(words.next());
    }

    JSONObject response = new JSONObject();
    try {
      response.put("id", id);
      response.put("error", (String) null);
      response.put("result", array);
      setResponse(response.toString());
    } catch (JSONException e) {
      jsonError("Failed to construct response");
    }
  }
  protected String generateJsonResponse(
      ServletWebRequest webRequest, WebResponse webResponse, List<FileItem> files) {
    JSONArray json = new JSONArray();

    for (FileItem fileItem : files) {
      JSONObject fileJson = new JSONObject();

      try {
        fileJson.put("name", fileItem.getName());
        fileJson.put("size", fileItem.getSize());
        fileJson.put("delete_type", "POST");
      } catch (JSONException e) {
        try {
          fileJson.put("error", e.getMessage());
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
      }

      json.put(fileJson);
    }

    return json.toString();
  }
 // This is used by src/main/bin/config-doc.sh which is used by
 // src/main/bin/traffic_monitor_config.pl which must be run after rpm install of traffic monitor
 @SuppressWarnings("PMD")
 public static void main(final String[] args) throws JSONException {
   final JSONObject doc = ConfigHandler.getConfig().getConfigDoc();
   System.out.println(doc.toString(2));
 }