|
Download
FAQ History |
|
API
Search Feedback |
Overview of SAAJ
This overview presents a high level view of how SAAJ messaging works and explains concepts in general terms. Its goal is to give you some terminology and a framework for the explanations and code examples that are presented in the tutorial section.
The overview looks at SAAJ from two perspectives:
Messages
SAAJ messages follow SOAP standards, which prescribe the format for messages and also specify some things that are required, optional, or not allowed. With the SAAJ API, you can create XML messages that conform to the SOAP 1.1 and WS-I Basic Profile 1.0 specifications simply by making Java API calls.
The Structure of an XML Document
Note: For more complete information on XML documents, see Chapters 5 and 6.
An XML document has a hierarchical structure with elements, subelements, subsubelements, and so on. You will notice that many of the SAAJ classes and interfaces represent XML elements in a SOAP message and have the word element or SOAP or both in their names.
An element is also referred to as a node. Accordingly, the SAAJ API has the interface
Node, which is the base class for all the classes and interfaces that represent XML elements in a SOAP message. There are also methods such asSOAPElement.addTextNode,Node.detachNode, andNode.getValue, which you will see how to use in the tutorial section.What Is in a Message?
The two main types of SOAP messages are those that have attachments and those that do not.
Messages with No Attachments
The following outline shows the very high level structure of a SOAP message with no attachments. Except for the SOAP header, all the parts listed are required to be in every SOAP message.
I. SOAP message
A. SOAP part
1. SOAP envelope
a. SOAP header (optional)
b. SOAP body
The SAAJ API provides the
SOAPMessageclass to represent a SOAP message, theSOAPPartclass to represent the SOAP part, theSOAPEnvelopeinterface to represent the SOAP envelope, and so on. Figure 13-1 illustrates the structure of a SOAP message with no attachments.When you create a new
SOAPMessageobject, it will automatically have the parts that are required to be in a SOAP message. In other words, a newSOAPMessageobject has aSOAPPartobject that contains aSOAPEnvelopeobject. TheSOAPEnvelopeobject in turn automatically contains an emptySOAPHeaderobject followed by an emptySOAPBodyobject. If you do not need theSOAPHeaderobject, which is optional, you can delete it. The rationale for having it automatically included is that more often than not you will need it, so it is more convenient to have it provided.The
SOAPHeaderobject may contain one or more headers with information about the sending and receiving parties. TheSOAPBodyobject, which always follows theSOAPHeaderobject if there is one, provides a simple way to send information intended for the ultimate recipient. For example, if there is aSOAPFaultobject (see Using SOAP Faults), it must be in theSOAPBodyobject.
![]()
Figure 13-1
SOAPMessageObject with No AttachmentsMessages with Attachments
A SOAP message may include one or more attachment parts in addition to the SOAP part. The SOAP part may contain only XML content; as a result, if any of the content of a message is not in XML format, it must occur in an attachment part. So, if for example, you want your message to contain a binary file, your message must have an attachment part for it. Note that an attachment part can contain any kind of content, so it can contain data in XML format as well. Figure 13-2 shows the high-level structure of a SOAP message that has two attachments.
![]()
Figure 13-2
SOAPMessageObject with TwoAttachmentPartObjectsThe SAAJ API provides the
AttachmentPartclass to represent the attachment part of a SOAP message. ASOAPMessageobject automatically has aSOAPPartobject and its required subelements, but becauseAttachmentPartobjects are optional, you have to create and add them yourself. The tutorial section will walk you through creating and populating messages with and without attachment parts.If a
SOAPMessageobject has one or more attachments, eachAttachmentPartobject must have a MIME header to indicate the type of data it contains. It may also have additional MIME headers to identify it or to give its location, which are optional but can be useful when there are multiple attachments. When aSOAPMessageobject has one or moreAttachmentPartobjects, itsSOAPPartobject may or may not contain message content.SAAJ and DOM
At SAAJ 1.2, the SAAJ APIs extend their counterparts in the
org.w3c.dompackage:Moreover, the
SOAPPartof aSOAPMessageis also a DOM Level 2Document, and can be manipulated as such by applications, tools and libraries that use DOM. See Chapter 8 for details about DOM. See Adding Content to the SOAPPart Object and Adding a Document to the SOAP Body for details on how to use DOM documents with the SAAJ API.Connections
All SOAP messages are sent and received over a connection. With the SAAJ API, the connection is represented by a
SOAPConnectionobject, which goes from the sender directly to its destination. This kind of connection is called a point-to-point connection because it goes from one endpoint to another endpoint. Messages sent using the SAAJ API are called request-response messages. They are sent over aSOAPConnectionobject with the methodcall, which sends a message (a request) and then blocks until it receives the reply (a response).SOAPConnection Objects
The following code fragment creates the
SOAPConnectionobjectconnection, and then, after creating and populating the message, usesconnectionto send the message. As stated previously, all messages sent over aSOAPConnectionobject are sent with the methodcall, which both sends the message and blocks until it receives the response. Thus, the return value for the methodcallis theSOAPMessageobject that is the response to the message that was sent. The parameterrequestis the message being sent;endpointrepresents where it is being sent.SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection();. . .// create a request message and give it content java.net.URL endpoint = new URL("http://fabulous.com/gizmo/order"); SOAPMessage response = connection.call(request, endpoint);Note that the second argument to the method
call, which identifies where the message is being sent, can be aStringobject or aURLobject. Thus, the last two lines of code from the preceding example could also have been the following:String endpoint = "http://fabulous.com/gizmo/order"; SOAPMessage response = connection.call(request, endpoint);A Web service implemented for request-response messaging must return a response to any message it receives. The response is a
SOAPMessageobject, just as the request is aSOAPMessageobject. When the request message is an update, the response is an acknowledgement that the update was received. Such an acknowledgement implies that the update was successful. Some messages may not require any response at all. The service that gets such a message is still required to send back a response because one is needed to unblock thecallmethod. In this case, the response is not related to the content of the message; it is simply a message to unblock thecallmethod.Now that you have some background on SOAP messages and SOAP connections, in the next section you will see how to use the SAAJ API.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.