diff flys-aft/src/main/java/de/intevation/aft/Notification.java @ 4095:da9df3641578

Send XML messages via HTTP POST around if the FLYS database has changed. flys-aft/trunk@3614 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 06 Jan 2012 12:10:40 +0000
parents
children 06891562e633
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/Notification.java	Fri Jan 06 12:10:40 2012 +0000
@@ -0,0 +1,101 @@
+package de.intevation.aft;
+
+import de.intevation.utils.XML;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.HttpURLConnection;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.apache.log4j.Logger;
+
+public class Notification
+{
+    private static Logger log = Logger.getLogger(Notification.class);
+
+    protected Document message;
+
+    public Notification() {
+    }
+
+    public Notification(Document message) {
+        this.message = message;
+    }
+
+    public Notification(Node message) {
+        this(wrap(message));
+    }
+
+    public static Document wrap(Node node) {
+        Document document = XML.newDocument();
+
+        // Send first element as message.
+        // Fall back to root node.
+        Node toImport = node;
+
+        NodeList children = node.getChildNodes();
+        for (int i = 0, N = children.getLength(); i < N; ++i) {
+            Node child = children.item(i);
+            if (child.getNodeType() == Node.ELEMENT_NODE) {
+                toImport = child;
+                break;
+            }
+        }
+
+        toImport = document.importNode(toImport, true);
+        document.appendChild(toImport);
+        document.normalizeDocument();
+        return document;
+    }
+
+    public Document sendPOST(URL url) {
+
+        OutputStream out    = null;
+        InputStream  in     = null;
+        Document     result = null;
+
+        try {
+            URLConnection ucon = url.openConnection();
+
+            if (!(ucon instanceof HttpURLConnection)) {
+                log.warn("'" + url + "' is not an HTTP(S) connection.");
+                return null;
+            }
+
+            HttpURLConnection con = (HttpURLConnection)ucon;
+
+            con.setRequestMethod("POST");
+            con.setDoInput(true);
+            con.setDoOutput(true);
+            con.setUseCaches(false);
+            con.setRequestProperty("Content-Type", "text/xml");
+
+            out = con.getOutputStream();
+            XML.toStream(message, out);
+            out.flush();
+            in = con.getInputStream();
+            result = XML.parseDocument(in);
+        }
+        catch (IOException ioe) {
+            log.error("Sending message to '" + url + "' failed.", ioe);
+        }
+        finally {
+            if (out != null) {
+                try { out.close(); } catch (IOException ioe) {}
+            }
+            if (in != null) {
+                try { in.close(); } catch (IOException ioe) {}
+            }
+        }
+
+        return result;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org