/** * 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; }
// 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; }