KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > FibonacciXMLRPCClient


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30
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;
37
38
39 /**
40  * <p>
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.
45  * </p>
46  *
47  * @author Elliotte Rusty Harold
48  * @version 1.0
49  *
50  */

51 public class FibonacciXMLRPCClient {
52   
53   private static String JavaDoc defaultServer
54    = "http://www.elharo.com/fibonacci/XML-RPC";
55    
56   public static void main(String JavaDoc[] args) {
57
58     if (args.length == 0) {
59       System.out.println(
60        "Usage: java nu.xom.samples.FibonacciXMLRPCClient "
61        + " index serverURL"
62       );
63       return;
64     }
65     
66     String JavaDoc index = args[0];
67     
68     String JavaDoc server;
69     if (args.length <= 1) server = defaultServer;
70     else server = args[1];
71     
72     try {
73         URL JavaDoc u = new URL JavaDoc(server);
74         URLConnection JavaDoc uc = u.openConnection();
75         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) uc;
76         connection.setDoOutput(true);
77         connection.setDoInput(true);
78         connection.setRequestMethod("POST");
79         OutputStream JavaDoc out = connection.getOutputStream();
80       
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);
95
96         Serializer serializer = new Serializer(out, "US-ASCII");
97         serializer.write(doc);
98           
99         InputStream JavaDoc in = connection.getInputStream();
100           
101         Builder parser = new Builder();
102         Document response = parser.build(in);
103
104         in.close();
105         out.close();
106         connection.disconnect();
107
108         Element methodResponse = response.getRootElement();
109         Elements body = methodResponse.getChildElements();
110         if (body.size() != 1) {
111             System.err.println("XML-RPC format error");
112             return;
113         }
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());
121         }
122         else if (body.get(0).getQualifiedName().equals("fault")) {
123             handleFault(body.get(0));
124         }
125         else {
126             System.err.println("XML-RPC Format Error");
127             return;
128         }
129           
130     }
131     catch (ParsingException ex) {
132       System.err.println("Server sent malformed output");
133       System.err.println(ex);
134     }
135     catch (NullPointerException JavaDoc ex) {
136       System.err.println(
137         "Server sent invalid output without the expected content."
138       );
139       System.err.println(ex);
140     }
141     catch (IOException JavaDoc ex) {
142       System.err.println(ex);
143     }
144   
145   }
146
147     private static void handleFault(Element fault) {
148       
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);
154         String JavaDoc code = "";
155         String JavaDoc detail = "";
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();
163         }
164         else if (name2.getValue().equals("faultCode")) {
165             code = value2.getValue();
166             detail = value1.getValue();
167         }
168         else {
169             throw new RuntimeException JavaDoc("Incorrect fault message");
170         }
171         System.err.println("Fault: ");
172         System.err.println(" code: " + code);
173         System.err.println(" " + detail);
174     }
175
176 }
Popular Tags