1
/* Copyright 2002, 2003 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
22
package nu.xom.samples;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import java.net.HttpURLConnection;
29
import java.net.URLConnection;
31
import nu.xom.Attribute;
32
import nu.xom.Builder;
33
import nu.xom.Document;
34
import nu.xom.Element;
35
import nu.xom.Elements;
36
import nu.xom.ParsingException;
37
import nu.xom.Serializer;
41
* Demonstrates communication with a SOAP
42
* server via the creation of an XML document,
43
* transmission of that document over the network,
44
* and reception and parsing of the server's response
48
* @author Elliotte Rusty Harold
52
public class FibonacciSOAPClient {
54
public final static String defaultServer
55
= "http://www.elharo.com/fibonacci/SOAP";
56
public final static String SOAP_ACTION
57
= "http://www.example.com/fibonacci";
59
public static void main(String[] args) {
61
if (args.length == 0) {
63
"Usage: java nu.xom.samples.FibonacciSOAPClient index serverURL"
68
String index = args[0];
71
if (args.length <= 1) server = defaultServer;
72
else server = args[1];
74
Document request = buildRequest(index);
77
URL u = new URL(server);
78
URLConnection uc = u.openConnection();
79
HttpURLConnection connection = (HttpURLConnection) uc;
80
connection.setDoOutput(true);
81
connection.setDoInput(true);
82
connection.setRequestMethod("POST");
83
connection.setRequestProperty("SOAPAction", SOAP_ACTION);
85
OutputStream out = connection.getOutputStream();
86
Serializer serializer = new Serializer(out, "US-ASCII");
87
serializer.write(request);
90
InputStream in = connection.getInputStream();
92
Builder parser = new Builder();
93
Document response = parser.build(in);
96
connection.disconnect();
98
/* This is the response we expect:
99
* <?xml version="1.0"?>
101
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" />
104
xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/">
105
<fibonacci index="1">1</fibonacci>
106
<fibonacci index="2">1</fibonacci>
107
<fibonacci index="3">2</fibonacci>
108
<fibonacci index="4">3</fibonacci>
109
<fibonacci index="5">5</fibonacci>
110
<fibonacci index="6">8</fibonacci>
111
<fibonacci index="7">13</fibonacci>
112
<fibonacci index="8">21</fibonacci>
113
<fibonacci index="9">34</fibonacci>
114
<fibonacci index="10">55</fibonacci>
121
Element responseEnvelope = response.getRootElement();
122
Element responseBody = responseEnvelope.getFirstChildElement("Body",
123
"http://schemas.xmlsoap.org/soap/envelope/");
126
Element fault = responseBody.getFirstChildElement(
127
"Fault", "http://schemas.xmlsoap.org/soap/envelope/");
128
if (fault == null) { // no fault
129
Element responseNumbers = responseBody.getFirstChildElement(
130
"Fibonacci_Numbers", "http://namespaces.cafeconleche.org/xmljava/ch3/");
131
Elements results = responseNumbers.getChildElements("fibonacci",
132
"http://namespaces.cafeconleche.org/xmljava/ch3/");
133
for (int i = 0; i < results.size(); i++) {
134
System.out.println(results.get(i).getValue());
142
catch (ParsingException ex) {
143
System.err.println("Server sent malformed output");
144
System.err.println(ex);
146
catch (NullPointerException ex) {
148
"Server sent invalid output without the expected content."
150
System.err.println(ex);
152
catch (IOException ex) {
153
System.err.println(ex);
154
ex.printStackTrace();
159
private static void handleFault(Element fault) {
161
Element faultcode = fault.getFirstChildElement("faultcode");
162
Element faultstring = fault.getFirstChildElement("faultstring");
163
Element faultactor = fault.getFirstChildElement("faultactor");
164
Element detail = fault.getFirstChildElement("detail");
166
String error = "Fault: \n";
167
if (faultcode != null) {
168
error += "Fault code: " + faultcode.getValue() + "\n";
170
if (faultstring != null) {
171
error += "Fault string: " + faultstring.getValue() + "\n";
173
if (faultactor != null) {
174
error += "Fault actor: " + faultactor.getValue() + "\n";
176
if (detail != null) {
177
error += "Details: " + detail.getValue() + "\n";
182
public static Document buildRequest(String index) {
184
String SOAPNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
185
Element envelope = new Element("SOAP-ENV:Envelope",
187
Element body = new Element("SOAP-ENV:Body",
189
Element calculateFibonacci = new Element("calculateFibonacci",
190
"http://namespaces.cafeconleche.org/xmljava/ch3/");
191
calculateFibonacci.appendChild(index);
192
calculateFibonacci.addNamespaceDeclaration("xsi",
193
"http://www.w3.org/2001/XMLSchema-instance");
194
Attribute type = new Attribute("type", "xsi:positiveInteger");
195
calculateFibonacci.addAttribute(type);
197
envelope.appendChild(body);
198
body.appendChild(calculateFibonacci);
200
Document doc = new Document(envelope);
b'\\ No newline at end of file'