001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.chatclient;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035: import java.net.HttpURLConnection;
036: import java.net.URL;
037:
038: import javax.xml.parsers.DocumentBuilder;
039: import javax.xml.parsers.DocumentBuilderFactory;
040: import javax.xml.parsers.ParserConfigurationException;
041: import javax.xml.transform.Transformer;
042: import javax.xml.transform.TransformerException;
043: import javax.xml.transform.TransformerFactory;
044: import javax.xml.transform.dom.DOMSource;
045: import javax.xml.transform.stream.StreamResult;
046:
047: import org.w3c.dom.Document;
048: import org.xml.sax.SAXException;
049:
050: /**
051: * Utility class to POST XML messages (represented as DOM <code>Document</code>
052: * objects) to web services and retrieve XML response messages (again
053: * represented as DOM <code>Document</code> objects).
054: */
055: public class XmlHttpConnection {
056:
057: /**
058: * POSTs an XML message to a web service.
059: *
060: * @param url the URL of the service
061: * @param requestDocument the DOM document to POST
062: * @return the XML response
063: */
064: public static Document send(String url, Document requestDocument)
065: throws IOException {
066: try {
067: URL u = new URL(url);
068: HttpURLConnection conn = (HttpURLConnection) u
069: .openConnection();
070: conn.setRequestMethod("POST");
071: conn.setDoOutput(true);
072:
073: // Attach Document
074: OutputStream out = conn.getOutputStream();
075: TransformerFactory tFactory = TransformerFactory
076: .newInstance();
077: Transformer transformer = tFactory.newTransformer();
078: DOMSource source = new DOMSource(requestDocument);
079: StreamResult result = new StreamResult(out);
080: transformer.transform(source, result);
081: out.close();
082:
083: conn.connect();
084:
085: InputStream in = conn.getInputStream();
086: DocumentBuilderFactory factory = DocumentBuilderFactory
087: .newInstance();
088: factory.setNamespaceAware(true);
089: DocumentBuilder builder = factory.newDocumentBuilder();
090: return builder.parse(in);
091: } catch (ParserConfigurationException ex) {
092: throw new IOException("Unable to parse response: "
093: + ex.toString());
094: } catch (SAXException ex) {
095: throw new IOException("Unable to parse response: "
096: + ex.toString());
097: } catch (TransformerException ex) {
098: throw new IOException(
099: "Unable to write document to OutputStream: "
100: + ex.toString());
101: }
102: }
103:
104: }
|