1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.HttpURLConnection ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 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 51 public class FibonacciXMLRPCClient { 52 53 private static String defaultServer 54 = "http://www.elharo.com/fibonacci/XML-RPC"; 55 56 public static void main(String [] 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 index = args[0]; 67 68 String server; 69 if (args.length <= 1) server = defaultServer; 70 else server = args[1]; 71 72 try { 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(); 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 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 ex) { 136 System.err.println( 137 "Server sent invalid output without the expected content." 138 ); 139 System.err.println(ex); 140 } 141 catch (IOException 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 code = ""; 155 String 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 ("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 |