September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Anatomy of a SOAP Client

  • March 29, 2001
  • By Piroz Mohseni
  • Send Email »
  • More Articles »

Simple Object Access Protocol (SOAP) opens a new chapter in interactions among applications especially when these applications reside in separate enterprises. Unlike CORBA, DCOM and RMI, SOAPs messaging structure is based on XML. Granted, this brings more overhead, but it also keeps things simple. The transport layer is not specified, but these days most SOAP implementations run on top of HTTP (again not the most efficient, but simple and straightforward). So think of SOAP as a way to send an XML message to a remote application, which causes a particular method to be invoked, and the result will be send to you also in XML.

There are many implementations of SOAP in various languages. My favorite is the Apache SOAP implementation ( http://xml.apache.org). Lets take a look at the typical steps a client application must take before invoking a SOAP call. First make sure you have the appropriate import statements. The org.apache.soap package includes the classes representing the actual SOAP message like Body, Header, and Envelope. The org.apache.soap.rpc package includes classes used for the messaging interaction like Call, Parameter, and Response.

The first thing you need to do is to create a new instance of the Call object. This object will be used to create and configure the message that is to be sent to the server. Call is a subclass of org.apache.rpc.RPCMessage.

Call call = new Call ();

Once you have an instance of Call, then you need to provide it with a unique URI representing the remove object that you want to invoke on the server. The service provider will furnish this URI. You will also specify the method that you wanted invoked on that remote object

call.setTargetObjectURI ("urn:wjs-stockquotes");
call.setMethodName ("getQuote");

Java objects need to be serialized and encoded before they can be exchanged between the client and the server. The receiving program needs to do the opposite by deserializing and decoding the message. The SOAPMappingRegistry extends the JavaXMLMappingRegistry and provides the methods for serialization and deserialization.

call.setSOAPMappingRegistry(new SOAPMappingRegistry());
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

Most methods invocations require some parameters. A Vector is usually used to assemble the parameters. We construct a new Parameter by providing its name, its data type, its value, and an encoding style URI, which is null here. We then use the setParams() method to add the vector to the Call object.

Vector params = new Vector ();
params.addElement (new Parameter("ticker", String.class, MSFT, null));
call.setParams (params);

We are now ready to make the SOAP call by using the invoke() method. The url parameter refers to the unique url of the service.

Response resp = call.invoke (url, );

Two things can happen as a result of the call. The SOAP call can either fail or succeed. We can check for failure by calling the generatedFault() method which returns a boolean. If there is an error condition, we get the instance of Fault and can then extract the error code and the error message. If the call was successful, then we can call the getReturnValue() method to retrieve the object returned by the remote method.

if (resp.generatedFault ()) {      Fault fault = resp.getFault ();      System.out.println ("  Fault Code   = " + fault.getFaultCode ());        System.out.println ("  Fault String = " + fault.getFaultString ());} else {      Parameter result = resp.getReturnValue ();      System.out.println (result.getValue ());}

SOAP opens a new dimension in enterprise application architecture. The simplicity and openness of the protocol gives architects a new tool to further integrate applications and provide seamless, one-stop web services for the enterprise and its clients and partners.

- Piroz Mohseni






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel