Esempio n. 1
0
  public void handleMethodGET(URI uri, HTTPRequest request, ToadletContext ctx)
      throws ToadletContextClosedException, IOException, RedirectException, URISyntaxException {
    if (!normalizePath(request.getPath()).equals("/")) {
      sendErrorPage(ctx, 404, "Not found", "the path '" + uri + "' was not found");
      return;
    }
    String key;
    String type;
    boolean automf;
    boolean deep;
    boolean ml;
    int hexWidth = request.getIntParam(PARAM_HEXWIDTH, Configuration.getHexWidth());
    if (request.isParameterSet(PARAM_AUTOMF)) {
      automf = request.getParam(PARAM_AUTOMF).length() > 0;
    } else {
      automf = Configuration.getAutoMF();
    }
    if (request.isParameterSet(Globals.PARAM_RECURSIVE)) {
      deep = request.getParam(Globals.PARAM_RECURSIVE).length() > 0;
    } else {
      deep = Configuration.getDeep();
    }
    if (request.isParameterSet(Globals.PARAM_MULTILEVEL)) {
      ml = request.getParam(Globals.PARAM_MULTILEVEL).length() > 0;
    } else {
      ml = Configuration.getMultilevel();
    }

    if (request.isParameterSet(Globals.PARAM_URI)) {
      key = request.getParam(Globals.PARAM_URI);
      type = request.getParam(Globals.PARAM_MFTYPE);
    } else {
      key = null;
      type = null;
    }

    String extraParams = "&hexwidth=" + hexWidth;
    if (automf) {
      extraParams += "&automf=checked";
    }
    if (deep) {
      extraParams += "&deep=checked";
    }
    if (ml) {
      extraParams += "&ml=checked";
    }

    List<String> errors = new LinkedList<String>();
    if (hexWidth < 1 || hexWidth > 1024) {
      errors.add("Hex display columns out of range. (1-1024). Set to 32 (default).");
      hexWidth = 32;
    }

    if (Globals.MFTYPE_ZIP.equals(type)) {
      throw new RedirectException(
          KeyUtilsPlugin.PLUGIN_URI + "/Site/?mftype=ZIPmanifest&key=" + key + extraParams);
    }
    if (Globals.MFTYPE_TAR.equals(type)) {
      throw new RedirectException(
          KeyUtilsPlugin.PLUGIN_URI + "/Site/?mftype=TARmanifest&key=" + key + extraParams);
    }
    if (Globals.MFTYPE_SIMPLE.equals(type)) {
      throw new RedirectException(
          KeyUtilsPlugin.PLUGIN_URI + "/Site/?mftype=simplemanifest&key=" + key + extraParams);
    }
    makeMainPage(ctx, errors, key, hexWidth, automf, deep, ml);
  }
  public void handleHTTPRequest(HTTPRequest request, boolean isPost) {

    clear();

    boolean rename = request.isParameterSet("rename");

    if (request.isPartSet("submit")) {
      String name = request.getPartAsString("category-name", MAX_CATEGORY_NAME_LENGTH).trim();
      String catId = request.getParam("rename");

      if (!"".equals(name)) {
        if (!nodesManager.renameCategory(catId, name)) nodesManager.newCategory(name);
        try {
          nodesManager.writeCategories();
        } catch (IOException ioe) {
          appendError(ioe);
        }

        rename = false;

      } else {
        appendError("Fied \"name\" is empty");
      }
    }

    if (request.isParameterSet("delete")) {
      try {
        String cat = request.getParam("delete");
        if (!nodesManager.deleteCategory(cat))
          appendError("The category \"" + cat + "\" does not exist.");

        nodesManager.writeCategories();
      } catch (ParsingException pe) {
        appendError(pe);
      } catch (IOException ioe) {
        appendError(ioe);
      }
    }

    if (nodesManager.countCategories() > 0 && !rename) {

      Element table = new Element("table");
      Element tHeader = new Element("tr");
      table.appendChild(tHeader);
      HTMLHelper.i18nElement(tHeader, "th", "echo.common.name");
      Element actionCell = HTMLHelper.i18nElement(tHeader, "th", "echo.common.action");
      actionCell.addAttribute(new Attribute("colspan", "2"));

      String[] ids = nodesManager.getCategoriesIds();
      boolean alternate = false;
      for (String id : ids) {
        Element row = new Element("tr");
        if (alternate) row.addAttribute(new Attribute("class", "alternate"));

        HTMLHelper.element(row, "td", nodesManager.getCategoryNameById(id));
        HTMLHelper.element(row, "td", HTMLHelper.i18nLink("?rename=" + id, "echo.common.rename"));
        HTMLHelper.element(row, "td", HTMLHelper.i18nLink("?delete=" + id, "echo.common.delete"));

        table.appendChild(row);
        alternate = !alternate;
      }

      appendContent(table);
    }

    String action = "categories";
    if (request.isParameterSet("rename")) action += "?rename=" + request.getParam("rename");

    Element form = HTMLHelper.form(action, formPsw);
    form.addAttribute(new Attribute("class", "inline"));
    HTMLHelper.i18nLabel(
        form, "category-name", (rename) ? "echo.common.rename" : "echo.manage.newCategory");
    Element nameInput = HTMLHelper.input(form, "text", "category-name");
    if (rename)
      nameInput.addAttribute(
          new Attribute("value", nodesManager.getCategoryNameById(request.getParam("rename"))));

    HTMLHelper.input(form, "submit", "submit");

    appendContent(form);
  }