sascha@4095: package de.intevation.aft; sascha@4095: sascha@4095: import de.intevation.utils.XML; sascha@4095: sascha@4095: import java.io.IOException; sascha@4095: import java.io.InputStream; sascha@4095: import java.io.OutputStream; sascha@4095: sascha@4095: import java.net.URL; sascha@4095: import java.net.URLConnection; sascha@4095: import java.net.HttpURLConnection; sascha@4095: sascha@4095: import org.w3c.dom.Document; sascha@4095: import org.w3c.dom.Node; sascha@4095: import org.w3c.dom.NodeList; sascha@4095: sascha@4095: import org.apache.log4j.Logger; sascha@4095: sascha@4095: public class Notification sascha@4095: { sascha@4095: private static Logger log = Logger.getLogger(Notification.class); sascha@4095: sascha@4095: protected Document message; sascha@4095: sascha@4095: public Notification() { sascha@4095: } sascha@4095: sascha@4095: public Notification(Document message) { sascha@4095: this.message = message; sascha@4095: } sascha@4095: sascha@4095: public Notification(Node message) { sascha@4095: this(wrap(message)); sascha@4095: } sascha@4095: sascha@4095: public static Document wrap(Node node) { sascha@4095: Document document = XML.newDocument(); sascha@4095: sascha@4095: // Send first element as message. sascha@4095: // Fall back to root node. sascha@4095: Node toImport = node; sascha@4095: sascha@4095: NodeList children = node.getChildNodes(); sascha@4095: for (int i = 0, N = children.getLength(); i < N; ++i) { sascha@4095: Node child = children.item(i); sascha@4095: if (child.getNodeType() == Node.ELEMENT_NODE) { sascha@4095: toImport = child; sascha@4095: break; sascha@4095: } sascha@4095: } sascha@4095: sascha@4095: toImport = document.importNode(toImport, true); sascha@4095: document.appendChild(toImport); sascha@4095: document.normalizeDocument(); sascha@4095: return document; sascha@4095: } sascha@4095: sascha@4095: public Document sendPOST(URL url) { sascha@4095: sascha@4095: OutputStream out = null; sascha@4095: InputStream in = null; sascha@4095: Document result = null; sascha@4095: sascha@4095: try { sascha@4095: URLConnection ucon = url.openConnection(); sascha@4095: sascha@4095: if (!(ucon instanceof HttpURLConnection)) { sascha@4095: log.warn("'" + url + "' is not an HTTP(S) connection."); sascha@4095: return null; sascha@4095: } sascha@4095: sascha@4095: HttpURLConnection con = (HttpURLConnection)ucon; sascha@4095: sascha@4095: con.setRequestMethod("POST"); sascha@4095: con.setDoInput(true); sascha@4095: con.setDoOutput(true); sascha@4095: con.setUseCaches(false); sascha@4095: con.setRequestProperty("Content-Type", "text/xml"); sascha@4095: sascha@4095: out = con.getOutputStream(); sascha@4095: XML.toStream(message, out); sascha@4095: out.flush(); sascha@4095: in = con.getInputStream(); sascha@4095: result = XML.parseDocument(in); sascha@4095: } sascha@4095: catch (IOException ioe) { sascha@4095: log.error("Sending message to '" + url + "' failed.", ioe); sascha@4095: } sascha@4095: finally { sascha@4095: if (out != null) { sascha@4095: try { out.close(); } catch (IOException ioe) {} sascha@4095: } sascha@4095: if (in != null) { sascha@4095: try { in.close(); } catch (IOException ioe) {} sascha@4095: } sascha@4095: } sascha@4095: sascha@4095: return result; sascha@4095: } sascha@4095: } sascha@4095: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :