This example illustrates how to access Novell exteNd WSSDK services (or other Web services) using the SAAJ client API's. SAAJ is a low-level interface for creating SOAP messages. In this example, we will develop a SAAJ client for the familiar Hello World example.
Below is the SAAJ client, which builds a SOAP request and sends it to the Hello World Web service. On return, the SOAP response is extracted and the usual "Hello World" message is displayed:
package saaj_simple; import java.util.Iterator; import javax.xml.soap.*; import java.net.URL; /** A simple SAAJ client that invokes the Hello World web service. */ public class Client { public static void main(String[] args) throws Exception { | // get the endpoint where the 'Hello World' web service is deployed | URL service = new URL(args.length > 0 ? args[0] : | "http://localhost:9090/hello"); | | // set the MessageFactory System property | System.setProperty("javax.xml.soap.MessageFactory", | "com.sssw.jbroker.saaj.soap.MessageFactoryImpl"); | | // set the ConnectionFactory System property | System.setProperty("javax.xml.soap.SOAPConnectionFactory", | "com.sssw.jbroker.saaj.soap.SOAPConnectionFactoryImpl"); | | // construct a SOAP message | MessageFactory factory = (MessageFactory) MessageFactory.newInstance(); | SOAPMessage message = factory.createMessage(); | | // get the SOAP body | SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); | SOAPBody body = envelope.getBody(); | | // create the sayHello element | Name name = envelope.createName("sayHello", "ns1", "http://www.hello"); | SOAPElement child = body.addBodyElement(name); | child.addNamespaceDeclaration("ns1", "http://www.hello"); | child.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); | | // prepare the message to be sent | message.saveChanges(); | | // post SOAP message to URL and get the response | SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); | SOAPConnection connection = scf.createConnection(); | SOAPMessage reply = connection.call(message, service); | connection.close(); | | // extract and print response | envelope = reply.getSOAPPart().getEnvelope(); | body = envelope.getBody(); | Iterator iter = body.getChildElements(); | SOAPElement response = (SOAPElement) iter.next(); | iter = response.getChildElements(); | SOAPElement result = (SOAPElement) iter.next(); | System.out.println(result.getValue()); } }
As can be seen, using the SAAJ API's for SOAP messaging is more tedious than using stubs and skeletons generated by Novell exteNd WSSDK.
You can use the Novell exteNd WSSDK TCP tunnel tool to inspect what goes over the wire. What goes over the HTTP connection to the servlet container is the following SOAP message:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'> <SOAP-ENV:Body> <ns1:sayHello xmlns:ns1='http://www.hello' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </SOAP-ENV:Body> </SOAP-ENV:Envelope> |
Figure 1: The SOAP message going from client to server in Hello World.
And below is the SOAP response message coming back from the server:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/1999/XMLSchema' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'> <SOAP-ENV:Body> <ns1:sayHelloResponse SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns=''> xmlns:ns1='http://www.hello'> <result xsi:type='xsd:string'>Hello World!</result> </ns1:sayHelloResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> |
Figure 2: The SOAP message going from server back to client in Hello World.
Please refer to the README for detailed instructions on compiling, deploying and running the example.
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.