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.Builder;
32
import nu.xom.Document;
33
import nu.xom.Element;
34
import nu.xom.Elements;
35
import nu.xom.ParsingException;
36
import nu.xom.Serializer;
41
* Demonstrates communication with an XML-RPC
42
* server via the creation of a simple document,
43
* transmission of that document over the network,
44
* and reception and parsing of the server's response.
47
* @author Elliotte Rusty Harold
51
public class FibonacciXMLRPCClient {
53
private static String defaultServer
54
= "http://www.elharo.com/fibonacci/XML-RPC";
56
public static void main(String[] args) {
58
if (args.length == 0) {
60
"Usage: java nu.xom.samples.FibonacciXMLRPCClient "
66
String index = args[0];
69
if (args.length <= 1) server = defaultServer;
70
else server = args[1];
73
URL u = new URL(server);
74
URLConnection uc = u.openConnection();
75
HttpURLConnection connection = (HttpURLConnection) uc;
76
connection.setDoOutput(true);
77
connection.setDoInput(true);
78
connection.setRequestMethod("POST");
79
OutputStream out = connection.getOutputStream();
81
Element methodCall = new Element("methodCall");
82
Element methodName = new Element("methodName");
83
methodName.appendChild("calculateFibonacci");
84
Element params = new Element("params");
85
Element param = new Element("param");
86
Element value = new Element("value");
87
Element data = new Element("int");
88
data.appendChild(index);
89
methodCall.appendChild(methodName);
90
methodCall.appendChild(params);
91
params.appendChild(param);
92
param.appendChild(value);
93
value.appendChild(data);
94
Document doc = new Document(methodCall);
96
Serializer serializer = new Serializer(out, "US-ASCII");
97
serializer.write(doc);
99
InputStream in = connection.getInputStream();
101
Builder parser = new Builder();
102
Document response = parser.build(in);
106
connection.disconnect();
108
Element methodResponse = response.getRootElement();
109
Elements body = methodResponse.getChildElements();
110
if (body.size() != 1) {
111
System.err.println("XML-RPC format error");
114
if (body.get(0).getQualifiedName().equals("params")) {
115
Element responseParam = body.get(0).getFirstChildElement("param");
116
Element responseValue
117
= responseParam.getFirstChildElement("value");
118
Element responseDouble
119
= responseValue.getFirstChildElement("double");
120
System.out.println(responseDouble.getValue());
122
else if (body.get(0).getQualifiedName().equals("fault")) {
123
handleFault(body.get(0));
126
System.err.println("XML-RPC Format Error");
131
catch (ParsingException ex) {
132
System.err.println("Server sent malformed output");
133
System.err.println(ex);
135
catch (NullPointerException ex) {
137
"Server sent invalid output without the expected content."
139
System.err.println(ex);
141
catch (IOException ex) {
142
System.err.println(ex);
147
private static void handleFault(Element fault) {
149
Element value = fault.getFirstChildElement("value");
150
Element struct = value.getFirstChildElement("struct");
151
Elements members = struct.getChildElements("member");
152
Element member1 = members.get(0);
153
Element member2 = members.get(1);
156
Element name1 = member1.getFirstChildElement("name");
157
Element value1 = member1.getFirstChildElement("value");
158
Element name2 = member2.getFirstChildElement("name");
159
Element value2 = member2.getFirstChildElement("value");
160
if (name1.getValue().equals("faultCode")) {
161
code = value1.getValue();
162
detail = value2.getValue();
164
else if (name2.getValue().equals("faultCode")) {
165
code = value2.getValue();
166
detail = value1.getValue();
169
throw new RuntimeException("Incorrect fault message");
171
System.err.println("Fault: ");
172
System.err.println(" code: " + code);
173
System.err.println(" " + detail);
b'\\ No newline at end of file'