/**
   * The servlet method that responds to an HTTP POST.
   *
   * <p>This method interprets the posted parameters as a new script and stores it either as an
   * anonymizer script or a profile. It returns a text/plain string containing the "OK" if the store
   * succeeded, or an error message if it failed.
   *
   * <p>Note: This method is designed to be called by an AJAX method in the Javascript of the
   * anonymizer configurator page.
   *
   * @param req The HttpRequest provided by the servlet container.
   * @param res The HttpResponse provided by the servlet container.
   */
  public void doPost(HttpRequest req, HttpResponse res) {

    // Make sure the user is authorized to do this.
    if (!req.userHasRole("admin") || !req.isReferredFrom(context)) {
      res.setResponseCode(res.forbidden);
      res.send();
      return;
    }

    if (req.hasParameter("suppress")) home = "";

    // Set up the response
    res.disableCaching();
    res.setContentType("txt");

    // Get the possible query parameters
    // and get the script file, if one is specified
    int p = -1;
    int s = -1;
    File file = null;
    try {
      p = Integer.parseInt(req.getParameter("p"));
      s = Integer.parseInt(req.getParameter("s"));
      file = getScriptFile(p, s);
    } catch (Exception ex) {
    }

    // Get the XML text to store
    String xml = req.getParameter("xml");
    if (xml != null) xml = xml.trim();
    else xml = "";

    // Figure out what kind of POST this is
    Path path = new Path(req.getPath());
    int len = path.length();

    if ((len == 3) && (path.element(1).equals("profile")) && !xml.equals("")) {
      // This is a request to store a specific profile.
      File profileFile = new File(savedProfiles, filter(path.element(2)));
      if (FileUtil.setText(profileFile, FileUtil.utf8, xml)) res.write("OK");
      else res.write("Unable to store " + profileFile);
    } else if ((len == 2) && path.element(1).equals("script") && (file != null)) {
      // This is a request to save a specific script.
      // Don't force the extension on scripts because that
      // might invalidate the reference in the config file.
      if (FileUtil.setText(file, FileUtil.utf8, xml)) {
        res.write("OK");
        logger.debug("Successfully stored the posted script to " + file);
      } else {
        res.write("Unable to store " + file);
        logger.debug("Unable to store the posted script to " + file);
      }
    } else res.setResponseCode(res.notimplemented);
    res.send();
  }
  /**
   * The servlet method that responds to an HTTP GET.
   *
   * @param req The HttpServletRequest provided by the servlet container.
   * @param res The HttpServletResponse provided by the servlet container.
   */
  public void doGet(HttpRequest req, HttpResponse res) {

    // Make sure the user is authorized to do this.
    if (!req.userHasRole("admin")) {
      res.setResponseCode(res.forbidden);
      res.send();
      return;
    }

    if (req.hasParameter("suppress")) home = "";

    // Disable caching of the response
    res.disableCaching();

    // Get the possible query parameters
    // and get the script file, if one is specified
    int p = -1;
    int s = -1;
    File file = null;
    try {
      p = Integer.parseInt(req.getParameter("p"));
      s = Integer.parseInt(req.getParameter("s"));
      file = getScriptFile(p, s);
    } catch (Exception ex) {
    }

    // Figure out what kind of GET this is
    Path path = new Path(req.getPath());
    int len = path.length();

    if ((len == 1) && (file == null)) {
      // This is a request for the script selection page
      res.setContentType("html");
      res.write(getListPage());
    } else if ((len == 1) && (file != null)) {
      // This is a request for the editor for the script specified by p and s
      res.setContentType("html");
      res.write(getScriptPage(p, s, file));
    } else if ((len == 2) && path.element(1).equals("profiles")) {
      // This is a request for a list of all the stored profiles
      res.setContentType("xml");
      res.write(getProfilesXML());
    } else if ((len == 4) && path.element(1).equals("profile")) {
      // This is a request for a profile specified in the URL path
      res.setContentType("xml");
      res.write(getProfileXML(path.element(2), path.element(3)));
    } else if ((len == 2) && path.element(1).equals("script")) {
      // This is a request for the script specified by p and s
      res.setContentType("xml");
      res.write(getScriptXML(file));
    } else res.setResponseCode(res.notfound);
    res.send();
  }