001: /* Copyright 2002, 2003 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.samples;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.net.HttpURLConnection;
028: import java.net.URL;
029: import java.net.URLConnection;
030:
031: import nu.xom.Attribute;
032: import nu.xom.Builder;
033: import nu.xom.Document;
034: import nu.xom.Element;
035: import nu.xom.Elements;
036: import nu.xom.ParsingException;
037: import nu.xom.Serializer;
038:
039: /**
040: * <p>
041: * Demonstrates communication with a SOAP
042: * server via the creation of an XML document,
043: * transmission of that document over the network,
044: * and reception and parsing of the server's response
045: * document.
046: * </p>
047: *
048: * @author Elliotte Rusty Harold
049: * @version 1.0
050: *
051: */
052: public class FibonacciSOAPClient {
053:
054: public final static String defaultServer = "http://www.elharo.com/fibonacci/SOAP";
055: public final static String SOAP_ACTION = "http://www.example.com/fibonacci";
056:
057: public static void main(String[] args) {
058:
059: if (args.length == 0) {
060: System.out
061: .println("Usage: java nu.xom.samples.FibonacciSOAPClient index serverURL");
062: return;
063: }
064:
065: String index = args[0];
066:
067: String server;
068: if (args.length <= 1)
069: server = defaultServer;
070: else
071: server = args[1];
072:
073: Document request = buildRequest(index);
074:
075: try {
076: URL u = new URL(server);
077: URLConnection uc = u.openConnection();
078: HttpURLConnection connection = (HttpURLConnection) uc;
079: connection.setDoOutput(true);
080: connection.setDoInput(true);
081: connection.setRequestMethod("POST");
082: connection.setRequestProperty("SOAPAction", SOAP_ACTION);
083:
084: OutputStream out = connection.getOutputStream();
085: Serializer serializer = new Serializer(out, "US-ASCII");
086: serializer.write(request);
087: serializer.flush();
088:
089: InputStream in = connection.getInputStream();
090:
091: Builder parser = new Builder();
092: Document response = parser.build(in);
093: in.close();
094: out.close();
095: connection.disconnect();
096:
097: /* This is the response we expect:
098: * <?xml version="1.0"?>
099: <SOAP-ENV:Envelope
100: xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" />
101: <SOAP-ENV:Body>
102: <Fibonacci_Numbers
103: xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/">
104: <fibonacci index="1">1</fibonacci>
105: <fibonacci index="2">1</fibonacci>
106: <fibonacci index="3">2</fibonacci>
107: <fibonacci index="4">3</fibonacci>
108: <fibonacci index="5">5</fibonacci>
109: <fibonacci index="6">8</fibonacci>
110: <fibonacci index="7">13</fibonacci>
111: <fibonacci index="8">21</fibonacci>
112: <fibonacci index="9">34</fibonacci>
113: <fibonacci index="10">55</fibonacci>
114: </Fibonacci_Numbers>
115: </SOAP-ENV:Body>
116: </SOAP-ENV:Envelope>
117: *
118: */
119:
120: Element responseEnvelope = response.getRootElement();
121: Element responseBody = responseEnvelope
122: .getFirstChildElement("Body",
123: "http://schemas.xmlsoap.org/soap/envelope/");
124:
125: // Check for fault
126: Element fault = responseBody.getFirstChildElement("Fault",
127: "http://schemas.xmlsoap.org/soap/envelope/");
128: if (fault == null) { // no fault
129: Element responseNumbers = responseBody
130: .getFirstChildElement("Fibonacci_Numbers",
131: "http://namespaces.cafeconleche.org/xmljava/ch3/");
132: Elements results = responseNumbers
133: .getChildElements("fibonacci",
134: "http://namespaces.cafeconleche.org/xmljava/ch3/");
135: for (int i = 0; i < results.size(); i++) {
136: System.out.println(results.get(i).getValue());
137: }
138: } else {
139: handleFault(fault);
140: }
141:
142: } catch (ParsingException ex) {
143: System.err.println("Server sent malformed output");
144: System.err.println(ex);
145: } catch (NullPointerException ex) {
146: System.err
147: .println("Server sent invalid output without the expected content.");
148: System.err.println(ex);
149: } catch (IOException ex) {
150: System.err.println(ex);
151: ex.printStackTrace();
152: }
153:
154: }
155:
156: private static void handleFault(Element fault) {
157:
158: Element faultcode = fault.getFirstChildElement("faultcode");
159: Element faultstring = fault.getFirstChildElement("faultstring");
160: Element faultactor = fault.getFirstChildElement("faultactor");
161: Element detail = fault.getFirstChildElement("detail");
162:
163: String error = "Fault: \n";
164: if (faultcode != null) {
165: error += "Fault code: " + faultcode.getValue() + "\n";
166: }
167: if (faultstring != null) {
168: error += "Fault string: " + faultstring.getValue() + "\n";
169: }
170: if (faultactor != null) {
171: error += "Fault actor: " + faultactor.getValue() + "\n";
172: }
173: if (detail != null) {
174: error += "Details: " + detail.getValue() + "\n";
175: }
176:
177: }
178:
179: public static Document buildRequest(String index) {
180:
181: String SOAPNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
182: Element envelope = new Element("SOAP-ENV:Envelope",
183: SOAPNamespace);
184: Element body = new Element("SOAP-ENV:Body", SOAPNamespace);
185: Element calculateFibonacci = new Element("calculateFibonacci",
186: "http://namespaces.cafeconleche.org/xmljava/ch3/");
187: calculateFibonacci.appendChild(index);
188: calculateFibonacci.addNamespaceDeclaration("xsi",
189: "http://www.w3.org/2001/XMLSchema-instance");
190: Attribute type = new Attribute("type", "xsi:positiveInteger");
191: calculateFibonacci.addAttribute(type);
192:
193: envelope.appendChild(body);
194: body.appendChild(calculateFibonacci);
195:
196: Document doc = new Document(envelope);
197: return doc;
198:
199: }
200:
201: }
|