Beispiel #1
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  public HttpMessage handleApiOther(HttpMessage msg, String name, JSONObject params)
      throws ApiException {
    if (OTHER_CHEETSHEET_ACTION_ORDER.equals(name) || OTHER_CHEETSHEET_KEY_ORDER.equals(name)) {

      List<KeyboardShortcut> shortcuts = this.extension.getShortcuts();

      if (OTHER_CHEETSHEET_ACTION_ORDER.equals(name)) {
        Collections.sort(
            shortcuts,
            new Comparator() {
              @Override
              public int compare(Object o1, Object o2) {
                return ((KeyboardShortcut) o1)
                    .getName()
                    .compareTo(((KeyboardShortcut) o2).getName());
              }
            });
      } else {
        Collections.sort(
            shortcuts,
            new Comparator() {
              @Override
              public int compare(Object o1, Object o2) {
                return ((KeyboardShortcut) o1)
                    .getKeyStrokeKeyCodeString()
                    .compareTo(((KeyboardShortcut) o2).getKeyStrokeKeyCodeString());
              }
            });
      }

      StringBuilder response = new StringBuilder();
      response.append(Constant.messages.getString("keyboard.api.cheatsheet.header"));
      boolean incUnset = this.getParam(params, PARAM_INC_UNSET, false);

      for (KeyboardShortcut shortcut : shortcuts) {
        if (incUnset || shortcut.getKeyStrokeKeyCodeString().length() > 0) {
          // Only show actions with actual shortcuts
          response.append(
              MessageFormat.format(
                  Constant.messages.getString("keyboard.api.cheatsheet.tablerow"),
                  shortcut.getName(),
                  shortcut.getKeyStrokeModifiersString(),
                  shortcut.getKeyStrokeKeyCodeString()));
        }
      }
      response.append(Constant.messages.getString("keyboard.api.cheatsheet.footer"));

      try {
        msg.setResponseHeader(
            "HTTP/1.1 200 OK\r\n"
                + "Pragma: no-cache\r\n"
                + "Cache-Control: no-cache\r\n"
                + "Content-Length: "
                + response.length()
                + "\r\nContent-Type: text/html;");
      } catch (HttpMalformedHeaderException e) {
        throw new ApiException(ApiException.Type.INTERNAL_ERROR, name, e);
      }
      msg.setResponseBody(response.toString());

      return msg;

    } else {
      throw new ApiException(ApiException.Type.BAD_OTHER, name);
    }
  }