Previous section   Next section

3.2 SAAJ Programming

Whereas JAX-RPC is concerned with allowing a client program to make a remote procedure call to a web service without exposing the underlying XML-based protocols that are used, SAAJ is a much lower-level API that is entirely concerned with the messages exchanged between the web service and its clients. Furthermore, while JAX-RPC applications look, for the most part, like ordinary Java applications making local method calls, an application that uses SAAJ needs to construct SOAP messages piece by piece and extract information from response messages. Using SAAJ requires much more work on the part of the developer than JAX-RPC, so why would you bother to use it? Here are some of the circumstances in which you might want to use SAAJ and its close relative JAXM instead of JAX-RPC:

SAAJ and JAXM provide a complete solution for XML-based messaging. The major differences between SAAJ and JAXM are as follows:

The classes and interfaces that provide the SAAJ and JAXM APIs reside in different packages, and, to emphasize the fact that SAAJ does not require JAXM, they are distributed in separate JAR files, as shown in Table 3-1.

Table 3-1. Packaging of the SAAJ and JAXM APIs

API

Package

JAR file

SAAJ

javax.xml.soap

saaj-api.jar (API)

saaj-ri.jar (reference implementation)

JAXM

javax.xml.messaging

jaxm-api.jar (API)

jaxm-runtime.jar (reference implementation)

In the case of J2EE 1.4, JAXM is not supported and the SAAJ classes appear in lib\j2ee.jar, which contains almost all of the packages in the reference implementation.

Although we have used the terms "client" and "server" to describe the participants in a SAAJ message exchange, the programming model for both SAAJ and JAXM does not make a strong distinction between these two roles, because most of the API is concerned with the details of handling the messages rather than actually sending and receiving them. In fact, SAAJ messaging represents more of a peer-to-peer model in which it might be more appropriate to use the terms "sender" and "receiver" instead of "client" and "server." In this chapter, however, all of the examples use SAAJ and are therefore limited to synchronous request/reply exchanges. For the sake of clarity, I will continue to use the term "client" to refer to the initiator of a service request, which will always be a freestanding J2SE application, and I will use "server" to mean the entity that receives and replies to the request.

For the JWSDP, to compile client applications that use SAAJ, your CLASSPATH needs to include only the saaj-api.jar file. However, the CLASSPATH required to run a client application is much larger, consisting of the following JAR files, which can be found in various subdirectories of the JWSDP installation:

saaj-api.jar

saaj-ri.jar

activation.jar

commons-logging.jar

dom4j.jar

mail.jar

jaxp-api.jar

dom.jar

sax.jar

xalan.jar

xercesImpl.jar

xsltc.jar

To run a SAAJ client application with J2EE 1.4, your CLASSPATH needs to include lib\j2ee.jar, together with the following four files from the endorsed directory:

dom.jar

sax.jar

xalan.jar

xercesImpl.jar

Aside from the handling of SOAP messages, SAAJ includes the ability to synchronously send a completed message to a given destination using HTTP as the underlying transport mechanism[1] and receive the reply (see Section 3.3.1, later in this chapter), but no specific provision is made for receiving a SOAP request in the server role. Servers are expected to reside in web containers or application servers, and need to make their own arrangements to receive SOAP messages. The JAXM specification includes a servlet (javax.xml.messaging.JAXMServlet) that can be used as the basis for a web container-based message receiver, but service providers are not required to use it. The example source code in this chapter uses a servlet that is very similar to JAXMServlet, so that we can demonstrate SAAJ without introducing a dependency on JAXM. This servlet is described later in this chapter in Section 3.3.2.

[1] Support of HTTP as the transport mechanism for SOAP messages is mandatory for all SAAJ implementations. Vendors are free to provide other transport mechanisms, but are not required to do so.


  Previous section   Next section