/** * 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(); }
/** * Export a file. * * @param fileToExport the file to export. * @return the status of the attempt to export the file. */ public Status export(File fileToExport) { HttpURLConnection conn; OutputStream svros; OutputStreamWriter writer; XmlObject xmlObject = null; // Parse the file and make sure we can handle it try { xmlObject = new XmlObject(fileToExport); } catch (Exception invalid) { return Status.FAIL; } Document doc = xmlObject.getDocument(); Element root = doc.getDocumentElement(); String rootName = root.getTagName(); if (!rootName.equals("ImageAnnotation")) { logger.warn(name + ": XmlObject with illegal root (" + rootName + ") not transmitted."); return Status.FAIL; } // Get the text. String text = XmlUtil.toString(doc.getDocumentElement()); // Make the connection and send the text. try { // Establish the connection conn = HttpUtil.getConnection(url); conn.connect(); // Get a writer for UTF-8 svros = conn.getOutputStream(); writer = new OutputStreamWriter(svros, FileUtil.utf8); // Send the text to the server writer.write(text, 0, text.length()); writer.flush(); writer.close(); // Get the response code int responseCode = conn.getResponseCode(); // Get the response. String response = FileUtil.getText(conn.getInputStream()); // Check the response, make any necessary log entries, and return the appropriate Status // instance. boolean ok = response.toLowerCase().contains("document submitted"); if (logAll || (!ok && logFailed)) logger.info(name + ": export response code: " + responseCode + "\n" + response); return (ok ? Status.OK : Status.FAIL); } catch (Exception e) { // This indicates a network failure; log it and set up for a retry. logger.warn(name + ": export failed: " + e.getMessage()); logger.debug(e); } return Status.RETRY; }
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) { } }
// Create an HTML page containing the form for configuring the file. private String getScriptPage(int pipe, int stage, File file) { String template = "/DAEditor.html"; String page = FileUtil.getText(Cache.getInstance().getFile(template)); String table = makeList(); Properties props = new Properties(); props.setProperty("closebox", "/icons/home.png"); props.setProperty("home", home); props.setProperty("context", "/" + context); props.setProperty("profilespath", "/profiles"); props.setProperty("profilepath", "/profile"); props.setProperty("scriptpath", "/script"); props.setProperty("scriptfile", file.getAbsolutePath().replaceAll("\\\\", "/")); props.setProperty("pipe", "" + pipe); props.setProperty("stage", "" + stage); page = StringUtil.replace(page, props); return page; }
// Create an HTML page containing the list of script files. private String getListPage() { String template = "/DAList.html"; String page = FileUtil.getText(Cache.getInstance().getFile(template)); String table = makeList(); Properties props = new Properties(); props.setProperty("home", home); props.setProperty("table", table); props.setProperty("homeicon", ""); if (!home.equals("")) { props.setProperty( "homeicon", "<img src=\"/icons/home.png\" onclick=\"window.open('" + home + "','_self');\" title=\"Return to the home page\" style=\"margin:2\"/>"); } page = StringUtil.replace(page, props); return page; }