Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax Free Open Book

Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax

Previous Section Next Section

A Simple SOAP Client

Talking to a SOAP server that provides Fibonacci numbers is not significantly harder than talking to an XML-RPC server. You just have to adjust the syntax of your request to use SOAP instead of XML-RPC. Again, we'll talk to a Fibonacci generator I'll develop in later chapters, but you can preview that at http://www.elharo.com/fibonacci/SOAP. The body of each request document contains a calculateFibonacci element in the http://namespaces.cafeconleche.org/xmljava/ch3/ namespace. This element contains a single positive integer:

<?xml version="1.0"?> 
<SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <SOAP-ENV:Body>
    <calculateFibonacci
      xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/"
      type="xsi:positiveInteger">10</calculateFibonacci>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The server responds with a list of Fibonacci numbers using the vocabulary designed earlier in this chapter, enclosed in the usual SOAP response envelope. For example, following is the response to a request for the first ten Fibonacci numbers:

<?xml version="1.0"?> 
<SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" />
  <SOAP-ENV:Body>
    <Fibonacci_Numbers
      xmlns="http://namespaces.cafeconleche.org/xmljava/ch3/">
      <fibonacci index="1">1</fibonacci>
      <fibonacci index="2">1</fibonacci>
      <fibonacci index="3">2</fibonacci>
      <fibonacci index="4">3</fibonacci>
      <fibonacci index="5">5</fibonacci>
      <fibonacci index="6">8</fibonacci>
      <fibonacci index="7">13</fibonacci>
      <fibonacci index="8">21</fibonacci>
      <fibonacci index="9">34</fibonacci>
      <fibonacci index="10">55</fibonacci>
    </Fibonacci_Numbers>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Conceptually, the SOAP client is almost identical to Example 3.10. It reads one or more integers from the command lines, opens a URLConnection to the server, and POSTs each integer to the server wrapped inside a bunch of XML markup. It then receives a batch of XML markup in response, which it prints on System.out. The markup used has changed; however, the markup is still composed of strings. One string is more or less the same as the next as far as Java is concerned. Example 3.11 shows the completed co

Example 3.11 Connecting to a SOAP Server with URLConnection
import java.net.*;
import java.io.*;


public class FibonacciSOAPClient {

  public final static String DEFAULT_SERVER
   = "http://www.elharo.com/fibonacci/SOAP";
  public final static String SOAP_ACTION
   = "http://www.example.com/fibonacci";

  public static void main(String[] args) {

    if (args.length == 0) {
      System.out.println(
       "Usage: java FibonacciSOAPClient index URL");
      return;
    }
    String input = args[0];
    String server = DEFAULT_SERVER;
    if (args.length >= 2) server = args[1];

    try {
      URL u = new URL(server);
      URLConnection uc = u.openConnection();
      HttpURLConnection connection = (HttpURLConnection) uc;
      
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setRequestMethod("POST");
      connection.setRequestProperty("SOAPAction", SOAP_ACTION);

      OutputStream out = connection.getOutputStream();
      Writer wout = new OutputStreamWriter(out);

      wout.write("<?xml version='1.0'?>\r\n");
      wout.write("<SOAP-ENV:Envelope ");
      wout.write("xmlns:SOAP-ENV=");
      wout.write(
        "'http://schemas.xmlsoap.org/soap/envelope/' "
      );
      wout.write("xmlns:xsi=");
      wout.write(
        "'http://www.w3.org/2001/XMLSchema-instance'>\r\n");
      wout.write("  <SOAP-ENV:Body>\r\n");
      wout.write("    <calculateFibonacci ");
      wout.write(
    "xmlns='http://namespaces.cafeconleche.org/xmljava/ch3/'\r\n"
      );
      wout.write("    type='xsi:positiveInteger'>" + input
       + "</calculateFibonacci>\r\n");
      wout.write("  </SOAP-ENV:Body>\r\n");
      wout.write("</SOAP-ENV:Envelope>\r\n");

      wout.flush();
      wout.close();

      InputStream in = connection.getInputStream();
      int c;
      while ((c = in.read()) != -1) System.out.write(c);
      in.close();

    }
    catch (IOException e) {
      System.err.println(e);
    }
  
 } // end main

} // end FibonacciSOAPClient

The one new piece here is that SOAP 1.1 requires the client to set a SOAPAction field in the HTTP header. This is done with URLConnection's setRequestProperty() method. Some servers may dispatch the request based on this field without actually parsing the body of the request. This server doesn't pay any attention to the SOAPAction field, however, so I just set it to a reasonable default value. SOAP 1.2 will make SOAPAction optional.

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Praise for Elliotte Rusty Harold's 'Processing XML with Java™'
    List of Examples
    List of Figures
    Preface
    Part I: XML
    Chapter 1. XML for Data
    Chapter 2. XML Protocols: XML-RPC and SOAP
    Chapter 3. Writing XML with Java
    Fibonacci Numbers
    Writing XML
    Output Streams, Writers, and Encodings
    A Simple XML-RPC Client
    A Simple SOAP Client
    Servlets
    Summary
    Chapter 4. Converting Flat Files to XML
    Chapter 5. Reading XML
    Part II: SAX
    Part III: DOM
    Part IV: JDOM
    Part V: XPath/XSLT
    Part VI: Appendixes


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele