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.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; 38 39 52 public class FibonacciSOAPClient { 53 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"; 58 59 public static void main(String [] args) { 60 61 if (args.length == 0) { 62 System.out.println( 63 "Usage: java nu.xom.samples.FibonacciSOAPClient index serverURL" 64 ); 65 return; 66 } 67 68 String index = args[0]; 69 70 String server; 71 if (args.length <= 1) server = defaultServer; 72 else server = args[1]; 73 74 Document request = buildRequest(index); 75 76 try { 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); 84 85 OutputStream out = connection.getOutputStream(); 86 Serializer serializer = new Serializer(out, "US-ASCII"); 87 serializer.write(request); 88 serializer.flush(); 89 90 InputStream in = connection.getInputStream(); 91 92 Builder parser = new Builder(); 93 Document response = parser.build(in); 94 in.close(); 95 out.close(); 96 connection.disconnect(); 97 98 120 121 Element responseEnvelope = response.getRootElement(); 122 Element responseBody = responseEnvelope.getFirstChildElement("Body", 123 "http://schemas.xmlsoap.org/soap/envelope/"); 124 125 Element fault = responseBody.getFirstChildElement( 127 "Fault", "http://schemas.xmlsoap.org/soap/envelope/"); 128 if (fault == null) { 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()); 135 } 136 } 137 else { 138 handleFault(fault); 139 } 140 141 } 142 catch (ParsingException ex) { 143 System.err.println("Server sent malformed output"); 144 System.err.println(ex); 145 } 146 catch (NullPointerException ex) { 147 System.err.println( 148 "Server sent invalid output without the expected content." 149 ); 150 System.err.println(ex); 151 } 152 catch (IOException ex) { 153 System.err.println(ex); 154 ex.printStackTrace(); 155 } 156 157 } 158 159 private static void handleFault(Element fault) { 160 161 Element faultcode = fault.getFirstChildElement("faultcode"); 162 Element faultstring = fault.getFirstChildElement("faultstring"); 163 Element faultactor = fault.getFirstChildElement("faultactor"); 164 Element detail = fault.getFirstChildElement("detail"); 165 166 String error = "Fault: \n"; 167 if (faultcode != null) { 168 error += "Fault code: " + faultcode.getValue() + "\n"; 169 } 170 if (faultstring != null) { 171 error += "Fault string: " + faultstring.getValue() + "\n"; 172 } 173 if (faultactor != null) { 174 error += "Fault actor: " + faultactor.getValue() + "\n"; 175 } 176 if (detail != null) { 177 error += "Details: " + detail.getValue() + "\n"; 178 } 179 180 } 181 182 public static Document buildRequest(String index) { 183 184 String SOAPNamespace = "http://schemas.xmlsoap.org/soap/envelope/"; 185 Element envelope = new Element("SOAP-ENV:Envelope", 186 SOAPNamespace); 187 Element body = new Element("SOAP-ENV:Body", 188 SOAPNamespace); 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); 196 197 envelope.appendChild(body); 198 body.appendChild(calculateFibonacci); 199 200 Document doc = new Document(envelope); 201 return doc; 202 203 } 204 205 } | Popular Tags |