Mercurial > dive4elements > river
annotate flys-aft/src/main/java/de/intevation/aft/Notification.java @ 5520:d5e1e4d9559e
compile manual from within importer packaging script
author | Tom Gottfried <tom.gottfried@intevation.de> |
---|---|
date | Thu, 28 Mar 2013 17:42:00 +0100 |
parents | f939e1e6cfa4 |
children |
rev | line source |
---|---|
4095
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.aft; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import de.intevation.utils.XML; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.io.IOException; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import java.io.InputStream; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import java.io.OutputStream; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 |
4772
f939e1e6cfa4
Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4110
diff
changeset
|
9 import java.net.HttpURLConnection; |
4095
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 import java.net.URL; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import java.net.URLConnection; |
4772
f939e1e6cfa4
Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4110
diff
changeset
|
12 |
f939e1e6cfa4
Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4110
diff
changeset
|
13 import org.apache.log4j.Logger; |
4095
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 import org.w3c.dom.Document; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import org.w3c.dom.Node; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 import org.w3c.dom.NodeList; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 public class Notification |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 private static Logger log = Logger.getLogger(Notification.class); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 protected Document message; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 public Notification() { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 public Notification(Document message) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 this.message = message; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 public Notification(Node message) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 this(wrap(message)); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 public static Document wrap(Node node) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 Document document = XML.newDocument(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 // Send first element as message. |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 // Fall back to root node. |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 Node toImport = node; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 NodeList children = node.getChildNodes(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 for (int i = 0, N = children.getLength(); i < N; ++i) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 Node child = children.item(i); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 if (child.getNodeType() == Node.ELEMENT_NODE) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 toImport = child; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 break; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 toImport = document.importNode(toImport, true); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 document.appendChild(toImport); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 document.normalizeDocument(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 return document; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 public Document sendPOST(URL url) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 OutputStream out = null; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 InputStream in = null; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 Document result = null; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 try { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 URLConnection ucon = url.openConnection(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 if (!(ucon instanceof HttpURLConnection)) { |
4110
06891562e633
Extracted and documented the error messages of the process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4095
diff
changeset
|
68 log.warn("NOTIFY: '" + url + "' is not an HTTP(S) connection."); |
4095
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 return null; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 HttpURLConnection con = (HttpURLConnection)ucon; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 con.setRequestMethod("POST"); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 con.setDoInput(true); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 con.setDoOutput(true); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 con.setUseCaches(false); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 con.setRequestProperty("Content-Type", "text/xml"); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 out = con.getOutputStream(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 XML.toStream(message, out); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 out.flush(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 in = con.getInputStream(); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 result = XML.parseDocument(in); |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 catch (IOException ioe) { |
4110
06891562e633
Extracted and documented the error messages of the process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4095
diff
changeset
|
87 log.error("NOTIFY: Sending message to '" + url + "' failed.", ioe); |
4095
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 finally { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 if (out != null) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 try { out.close(); } catch (IOException ioe) {} |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 if (in != null) { |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 try { in.close(); } catch (IOException ioe) {} |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 return result; |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 } |
da9df3641578
Send XML messages via HTTP POST around if the FLYS database has changed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |