/** * 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) { } }