예제 #1
0
  /**
   * 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();
  }
예제 #2
0
 private void save() {
   try {
     Document doc = XmlUtil.getDocument();
     Element root = doc.createElement("profiles");
     doc.appendChild(root);
     Enumeration<String> keys = table.keys();
     while (keys.hasMoreElements()) {
       table.get(keys.nextElement()).appendTo(root);
     }
     FileUtil.setText(new File(filename), FileUtil.utf8, XmlUtil.toString(doc));
   } catch (Exception ignore) {
   }
 }