diff flys-aft/src/main/java/de/intevation/aft/Sync.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 b20b710aa86f
children cdcf98245e36
line wrap: on
line diff
--- a/flys-aft/src/main/java/de/intevation/aft/Sync.java	Thu Jan 05 18:58:49 2012 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/Sync.java	Fri Jan 06 12:10:40 2012 +0000
@@ -2,9 +2,14 @@
 
 import java.io.File;
 
+import java.net.URL;
+import java.net.MalformedURLException;
+
 import java.sql.SQLException;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
 
 import org.apache.log4j.Logger;
 
@@ -23,12 +28,45 @@
 
     public static final String XPATH_DIPS   = "/sync/dips/file/text()";
     public static final String XPATH_REPAIR = "/sync/dips/repair/text()";
+
     public static final String XPATH_NOTIFICATIONS =
         "/sync/notifications/notification";
 
     public static final String CONFIG_FILE =
         System.getProperty("config.file", "config.xml");
 
+    public static void sendNotifications(Document config) {
+        NodeList notifications = (NodeList)XML.xpath(
+            config, XPATH_NOTIFICATIONS, XPathConstants.NODESET, null, null);
+
+        if (notifications == null) {
+            return;
+        }
+
+        for (int i = 0, N = notifications.getLength(); i < N; ++i) {
+            Element notification = (Element)notifications.item(i);
+            String urlString = notification.getAttribute("url");
+
+            URL url;
+            try {
+                url = new URL(urlString);
+            }
+            catch (MalformedURLException mfue) {
+                log.warn("Invalid URL '" + urlString + "'. Ignored.", mfue);
+                continue;
+            }
+
+            Notification n = new Notification(notification);
+
+            Document result = n.sendPOST(url);
+
+            if (result != null) {
+                log.info("Send notifcation to '" + urlString + "'.");
+                log.info(XML.toString(result));
+            }
+        }
+    }
+
     public static void main(String [] args) {
 
         File configFile = new File(CONFIG_FILE);
@@ -116,6 +154,10 @@
             }
         }
 
+        if (modified) {
+            sendNotifications(config);
+        }
+
         if (exitCode != 0) {
             System.exit(1);
         }

http://dive4elements.wald.intevation.org