Download
FAQ History |
API
Search Feedback |
Tutorial
This tutorial walks you through how to use the SAAJ API. First, it covers the basics of creating and sending a simple SOAP message. Then you will learn more details about adding content to messages, including how to create SOAP faults and attributes. Finally, you will learn how to send a message and retrieve the content of the response. After going through this tutorial, you will know how to perform the following tasks:
In the section Code Examples, you will see the code fragments from earlier parts of the tutorial in runnable applications, which you can test yourself. To see how the SAAJ API can be used in server code, see the SAAJ part of the case study (SAAJ Coffee Supplier Service), which shows an example of both the client and the server code for a Web service application.
A SAAJ client can send request-response messages to Web services that are implemented to do request-response messaging. This section demonstrates how you can do this.
Creating and Sending a Simple Message
This section covers the basics of creating and sending a simple message and retrieving the content of the response. It includes the following topics:
Creating a Message
The first step is to create a message using a
MessageFactory
object. The SAAJ API provides a default implementation of theMessageFactory
class, thus making it easy to get an instance. The following code fragment illustrates getting an instance of the default message factory and then using it to create a message.MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage();As is true of the
newInstance
method forSOAPConnectionFactory
, thenewInstance
method forMessageFactory
is static, so you invoke it by callingMessageFactory.newInstance
.Parts of a Message
A
SOAPMessage
object is required to have certain elements, and, as stated previously, the SAAJ API simplifies things for you by returning a newSOAPMessage
object that already contains these elements. Somessage
, which was created in the preceding line of code, automatically has the following:I. A
SOAPPart
object that containsA. A
SOAPEnvelope
object that contains1. An empty
SOAPHeader
object2. An empty
SOAPBody
objectThe
SOAPHeader
object is optional and can be deleted if it is not needed. However, if there is one, it must precede theSOAPBody
object. TheSOAPBody
object can hold either the content of the message or a fault message that contains status information or details about a problem with the message. The section Using SOAP Faults walks you through how to useSOAPFault
objects.Accessing Elements of a Message
The next step in creating a message is to access its parts so that content can be added. There are two ways to do this. The
SOAPMessage
objectmessage
, created in the preceding code fragment, is the place to start.The first way to access the parts of the message is to work your way through the structure of the message. The message contains a
SOAPPart
object, so you use thegetSOAPPart
method ofmessage
to retrieve it:Next you can use the
getEnvelope
method ofsoapPart
to retrieve theSOAPEnvelope
object that it contains.You can now use the
getHeader
andgetBody
methods ofenvelope
to retrieve its emptySOAPHeader
andSOAPBody
objects.The second way to access the parts of the message is to retrieve the message header and body directly, without retrieving the
SOAPPart
orSOAPEnvelope
. To do so, use thegetSOAPHeader
andgetSOAPBody
methods ofSOAPMessage
:This example of a SAAJ client does not use a SOAP header, so you can delete it. (You will see more about headers later.) Because all
SOAPElement
objects, includingSOAPHeader
objects, are derived from theNode
interface, you use the methodNode.detachNode
to deleteheader
.Adding Content to the Body
The
SOAPBody
object contains either content or a fault. To add content to the body, you normally create one or moreSOAPBodyElement
objects to hold the content. You can also add subelements to theSOAPBodyElement
objects by using theaddChildElement
method. For each element or child element, you add content by using theaddTextNode
method.When you create any new element, you also need to create an associated
Name
object so that it is uniquely identified. One way to createName
objects is by usingSOAPEnvelope
methods, so you can use theenvelope
variable from the earlier code fragment to create theName
object for your new element. Another way to createName
objects is to useSOAPFactory
methods, which are useful if you do not have access to theSOAPEnvelope
.
Note: The
SOAPFactory
class also lets you create XML elements when you are not creating an entire message or do not have access to a completeSOAPMessage
object. For example, JAX-RPC implementations often work with XML fragments rather than completeSOAPMessage
objects. Consequently, they do not have access to aSOAPEnvelope
object, and this makes using aSOAPFactory
object to createName
objects very useful. In addition to a method for creatingName
objects, theSOAPFactory
class provides methods for creatingDetail
objects and SOAP fragments. You will find an explanation ofDetail
objects in Overview of SOAP Faults and Creating and Populating a SOAPFault Object.
Name
objects associated withSOAPBodyElement
orSOAPHeaderElement
objects must be fully qualified; that is, they must be created with a local name, a prefix for the namespace being used, and a URI for the namespace. Specifying a namespace for an element makes clear which one is meant if more than one element has the same local name.The following code fragment retrieves the
SOAPBody
objectbody
frommessage
, uses aSOAPFactory
to create aName
object for the element to be added, and adds a newSOAPBodyElement
object tobody
.SOAPBody body = message.getSOAPBody(); SOAPFactory soapFactory = SOAPFactory.newInstance(); Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombat.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName);At this point,
body
contains aSOAPBodyElement
object identified by theName
objectbodyName
, but there is still no content inbodyElement
. Assuming that you want to get a quote for the stock of Sun Microsystems, Inc., you need to create a child element for the symbol using theaddChildElement
method. Then you need to give it the stock symbol using theaddTextNode
method. TheName
object for the newSOAPElement
objectsymbol
is initialized with only a local name because child elements inherit the prefix and URI from the parent element.Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW");You might recall that the headers and content in a
SOAPPart
object must be in XML format. The SAAJ API takes care of this for you, building the appropriate XML constructs automatically when you call methods such asaddBodyElement
,addChildElement
, andaddTextNode
. Note that you can call the methodaddTextNode
only on an element such asbodyElement
or any child elements that are added to it. You cannot calladdTextNode
on aSOAPHeader
orSOAPBody
object because they contain elements and not text.The content that you have just added to your
SOAPBody
object will look like the following when it is sent over the wire:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m="http://wombat.ztrade.com"> <symbol>SUNW</symbol> </m:GetLastTradePrice> </SOAP-ENV:Body> </SOAP-ENV:Envelope>Let's examine this XML excerpt line by line to see how it relates to your SAAJ code. Note that an XML parser does not care about indentations, but they are generally used to indicate element levels and thereby make it easier for a human reader to understand.
Here is the SAAJ code:
SOAPMessage message = messageFactory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody();Here is the XML it produces:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> . . . </SOAP-ENV:Body> </SOAP-ENV:Envelope>The outermost element in this XML example is the SOAP envelope element, indicated by
SOAP-ENV:Envelope
. Note thatEnvelope
is the name of the element, andSOAP-ENV
is the namespace prefix. The interfaceSOAPEnvelope
represents a SOAP envelope.The first line signals the beginning of the SOAP envelope element, and the last line signals the end of it; everything in between is part of the SOAP envelope. The second line is an example of an attribute for the SOAP envelope element. Because a SOAP envelope element always contains this attribute with this value, a
SOAPMessage
object comes with it automatically included.xmlns
stands for "XML namespace," and its value is the URI of the namespace associated withEnvelope
.The next line is an empty SOAP header. We could remove it by calling
header.detachNode
after thegetSOAPHeader
call.The next two lines mark the beginning and end of the SOAP body, represented in SAAJ by a
SOAPBody
object. The next step is to add content to the body.Here is the SAAJ code:
Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombat.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName);Here is the XML it produces:
These lines are what the
SOAPBodyElement
bodyElement
in your code represents.GetLastTradePrice
is its local name,m
is its namespace prefix, andhttp://wombat.ztrade.com
is its namespace URI.Here is the SAAJ code:
Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW")
;Here is the XML it produces:
The
String
"SUNW"
is the text node for the element<symbol>
. ThisString
object is the message content that your recipient, the stock quote service, receives.The following example shows how to add multiple
SOAPElement
objects and add text to each of them. The code first creates theSOAPBodyElement
objectpurchaseLineItems
, which has a fully qualified name associated with it. That is, theName
object for it has a local name, a namespace prefix, and a namespace URI. As you saw earlier, aSOAPBodyElement
object is required to have a fully qualified name, but child elements added to it, such asSOAPElement
objects, can haveName
objects with only the local name.SOAPBody body = soapFactory.getSOAPBody(); Name bodyName = soapFactory.createName("PurchaseLineItems", "PO", "http://sonata.fruitsgalore.com"); SOAPBodyElement purchaseLineItems = body.addBodyElement(bodyName); Name childName = soapFactory.createName("Order"); SOAPElement order = purchaseLineItems.addChildElement(childName); childName = soapFactory.createName("Product"); SOAPElement product = order.addChildElement(childName); product.addTextNode("Apple"); childName = soapFactory.createName("Price"); SOAPElement price = order.addChildElement(childName); price.addTextNode("1.56"); childName = soapFactory.createName("Order"); SOAPElement order2 = purchaseLineItems.addChildElement(childName); childName = soapFactory.createName("Product"); SOAPElement product2 = order2.addChildElement(childName); product2.addTextNode("Peach"); childName = soapFactory.createName("Price"); SOAPElement price2 = order2.addChildElement(childName); price2.addTextNode("1.48");The SAAJ code in the preceding example produces the following XML in the SOAP body:
<PO:PurchaseLineItems xmlns:PO="http://www.sonata.fruitsgalore/order"> <Order> <Product>Apple</Product> <Price>1.56</Price> </Order> <Order> <Product>Peach</Product> <Price>1.48</Price> </Order> </PO:PurchaseLineItems>Getting a SOAPConnection Object
The SAAJ API is focused primarily on reading and writing messages. After you have written a message, you can send it using various mechanisms (such as JMS or JAXM). The SAAJ API does, however, provide a simple mechanism for request-response messaging.
To send a message, a SAAJ client can use a
SOAPConnection
object. ASOAPConnection
object is a point-to-point connection, meaning that it goes directly from the sender to the destination (usually a URL) that the sender specifies.The first step is to obtain a
SOAPConnectionFactory
object that you can use to create your connection. The SAAJ API makes this easy by providing theSOAPConnectionFactory
class with a default implementation. You can get an instance of this implementation using the following line of code.Now you can use
soapConnectionFactory
to create aSOAPConnection
object.You will use
connection
to send the message that you created.Sending a Message
A SAAJ client calls the
SOAPConnection
methodcall
on aSOAPConnection
object to send a message. Thecall
method takes two arguments: the message being sent and the destination to which the message should go. This message is going to the stock quote service indicated by theURL
objectendpoint
.java.net.URL endpoint = new URL( "http://wombat.ztrade.com/quotes"); SOAPMessage response = connection.call(message, endpoint);The content of the message you sent is the stock symbol SUNW; the
SOAPMessage
objectresponse
should contain the last stock price for Sun Microsystems, which you will retrieve in the next section.A connection uses a fair amount of resources, so it is a good idea to close a connection as soon as you are finished using it.
Getting the Content of a Message
The initial steps for retrieving a message's content are the same as those for giving content to a message: Either you use the
Message
object to get theSOAPBody
object, or you access theSOAPBody
object through theSOAPPart
andSOAPEnvelope
objects.Then you access the
SOAPBody
object'sSOAPBodyElement
object, because that is the element to which content was added in the example. (In a later section you will see how to add content directly to theSOAPPart
object, in which case you would not need to access theSOAPBodyElement
object to add content or to retrieve it.)To get the content, which was added with the method
SOAPElement.addTextNode
, you call the methodNode.getValue
. Note thatgetValue
returns the value of the immediate child of the element that calls the method. Therefore, in the following code fragment, thegetValue
method is called onbodyElement
, the element on which theaddTextNode
method was called.To access
bodyElement
, you call thegetChildElements
method onsoapBody
. PassingbodyName
togetChildElements
returns ajava.util.Iterator
object that contains all the child elements identified by theName
objectbodyName
. You already know that there is only one, so calling thenext
method on it will return theSOAPBodyElement
you want. Note that theIterator.next
method returns a JavaObject
, so you need to cast theObject
it returns to aSOAPBodyElement
object before assigning it to the variablebodyElement
.SOAPBody soapBody = response.getSOAPBody(); java.util.Iterator iterator = soapBody.getChildElements(bodyName); SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("
The last price for SUNW is");
System.out.println(lastPrice);If more than one element had the name
bodyName
, you would have to use awhile
loop using theIterator.hasNext
method to make sure that you got all of them.while (iterator.hasNext()) { SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("
The last price for SUNW is");
System.out.println(lastPrice); }At this point, you have seen how to send a very basic request-response message and get the content from the response. The next sections provide more detail on adding content to messages.
Adding Content to the Header
To add content to the header, you create a
SOAPHeaderElement
object. As with all new elements, it must have an associatedName
object, which you can create using the message'sSOAPEnvelope
object or aSOAPFactory
object.For example, suppose you want to add a conformance claim header to the message to state that your message conforms to the WS-I Basic Profile. The following code fragment retrieves the
SOAPHeader
object frommessage
and adds a newSOAPHeaderElement
object to it. ThisSOAPHeaderElement
object contains the correct qualified name and attribute for a WS-I conformance claim header.SOAPHeader header = message.getSOAPHeader(); Name headerName = soapFactory.createName("Claim", "wsi", "http://ws-i.org/schemas/conformanceClaim/"); SOAPHeaderElement headerElement = header.addHeaderElement(headerName); headerElement.addAttribute(soapFactory.createName( "conformsTo"), "http://ws-i.org/profiles/basic1.0/");At this point,
header
contains theSOAPHeaderElement
objectheaderElement
identified by theName
objectheaderName
. Note that theaddHeaderElement
method both createsheaderElement
and adds it toheader
.A conformance claim header has no content. This code produces the following XML header:
<SOAP-ENV:Header> <wsi:Claim conformsTo="http://ws-i.org/profiles/basic1.0/" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"/> </SOAP-ENV:Header>For more information about creating SOAP messages that conform to WS-I, see the Messaging section of the
WS-I Basic Profile.
For a different kind of header, you might want to add content to
headerElement
. The following line of code uses the methodaddTextNode
to do this.Now you have the
SOAPHeader
objectheader
that contains aSOAPHeaderElement
object whose content is"order"
.Adding Content to the SOAPPart Object
If the content you want to send is in a file, SAAJ provides an easy way to add it directly to the
SOAPPart
object. This means that you do not access theSOAPBody
object and build the XML content yourself, as you did in the preceding section.To add a file directly to the
SOAPPart
object, you use ajavax.xml.transform.Source
object from JAXP (the Java API for XML Processing). There are three types ofSource
objects:SAXSource
,DOMSource
, andStreamSource
. AStreamSource
object holds an XML document in text form.SAXSource
andDOMSource
objects hold content along with the instructions for transforming the content into an XML document.The following code fragment uses the JAXP API to build a
DOMSource
object that is passed to theSOAPPart.setContent
method. The first three lines of code get aDocumentBuilderFactory
object and use it to create theDocumentBuilder
objectbuilder
. Because SOAP messages use namespaces, you should set theNamespaceAware
property for the factory to true. Thenbuilder
parses the content file to produce aDocument
object.DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document document = builder.parse("file:///music/order/soap.xml"); DOMSource domSource = new DOMSource(document);The following two lines of code access the
SOAPPart
object (using theSOAPMessage
objectmessage
) and set the newDocument
object as its content. TheSOAPPart.setContent
method not only sets content for theSOAPBody
object but also sets the appropriate header for theSOAPHeader
object.The XML file you use to set the content of the
SOAPPart
object must includeEnvelope
andBody
elements:<SOAP-ENV:Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> ... </SOAP-ENV:Body> </SOAP-ENV:Envelope>You will see other ways to add content to a message in the sections Adding a Document to the SOAP Body and Adding Attachments.
Adding a Document to the SOAP Body
In addition to setting the content of the entire SOAP message to that of a
DOMSource
object, you can add a DOM document directly to the body of the message. This capability means that you do not have to create ajavax.xml.transform.Source
object. After you parse the document, you can add it directly to the message body:Manipulating Message Content Using SAAJ or DOM APIs
Because SAAJ nodes and elements implement the DOM
Node
andElement
interfaces, you have many options for adding or changing message content:The first three of these cause no problems. After you have created a message, whether or not you have imported its content from another document, you can start adding or changing nodes using either SAAJ or DOM APIs.
But if you use DOM APIs and then switch to using SAAJ APIs to manipulate the document, any references to objects within the tree that were obtained using DOM APIs are no longer valid. If you must use SAAJ APIs after using DOM APIs, you should set all your DOM typed references to null, because they can become invalid. For more information about the exact cases in which references become invalid, see the SAAJ API documentation.
The basic rule is that you can continue manipulating the message content using SAAJ APIs as long as you want to, but after you start manipulating it using DOM, you should no longer use SAAJ APIs.
Adding Attachments
An
AttachmentPart
object can contain any type of content, including XML. And because the SOAP part can contain only XML content, you must use anAttachmentPart
object for any content that is not in XML format.Creating an AttachmentPart Object and Adding Content
The
SOAPMessage
object creates anAttachmentPart
object, and the message also must add the attachment to itself after content has been added. TheSOAPMessage
class has three methods for creating anAttachmentPart
object.The first method creates an attachment with no content. In this case, an
AttachmentPart
method is used later to add content to the attachment.You add content to
attachment
by using theAttachmentPart
methodsetContent
. This method takes two parameters: a JavaObject
for the content, and aString
object for the MIME content type that is used to encode the object. Content in theSOAPBody
part of a message automatically has aContent-Type
header with the value"text/xml"
because the content must be in XML. In contrast, the type of content in anAttachmentPart
object must be specified because it can be any type.Each
AttachmentPart
object has one or more MIME headers associated with it. When you specify a type to thesetContent
method, that type is used for the headerContent-Type
. Note thatContent-Type
is the only header that is required. You may set other optional headers, such asContent-Id
andContent-Location
. For convenience, SAAJ providesget
andset
methods for the headersContent-Type
,Content-Id
, andContent-Location
. These headers can be helpful in accessing a particular attachment when a message has multiple attachments. For example, to access the attachments that have particular headers, you can call theSOAPMessage
methodgetAttachments
and pass it aMIMEHeaders
object containing the MIME headers you are interested in.The following code fragment shows one of the ways to use the method
setContent
. The JavaObject
in the first parameter can be aString
, a stream, ajavax.xml.transform.Source
object, or ajavax.activation.DataHandler
object. The JavaObject
being added in the following code fragment is aString
, which is plain text, so the second argument must be"text/plain"
. The code also sets a content identifier, which can be used to identify thisAttachmentPart
object. After you have added content toattachment
, you must add it to theSOAPMessage
object, something that is done in the last line.String stringContent = "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439"; attachment.setContent(stringContent, "text/plain"); attachment.setContentId("update_address"); message.addAttachmentPart(attachment);The
attachment
variable now represents anAttachmentPart
object that contains the stringstringContent
and has a header that contains the string"text/plain"
. It also has aContent-Id
header with"update_address"
as its value. Andattachment
is now part ofmessage
.The other two
SOAPMessage.createAttachment
methods create anAttachmentPart
object complete with content. One is very similar to theAttachmentPart.setContent
method in that it takes the same parameters and does essentially the same thing. It takes a JavaObject
containing the content and aString
giving the content type. As withAttachmentPart.setContent
, theObject
can be aString
, a stream, ajavax.xml.transform.Source
object, or ajavax.activation.DataHandler
object.The other method for creating an
AttachmentPart
object with content takes aDataHandler
object, which is part of the JavaBeans Activation Framework (JAF). Using aDataHandler
object is fairly straightforward. First, you create ajava.net.URL
object for the file you want to add as content. Then you create aDataHandler
object initialized with theURL
object:URL url = new URL("http://greatproducts.com/gizmos/img.jpg"); DataHandler dataHandler = new DataHandler(url); AttachmentPart attachment = message.createAttachmentPart(dataHandler); attachment.setContentId("attached_image"); message.addAttachmentPart(attachment);You might note two things about this code fragment. First, it sets a header for
Content-ID
using the methodsetContentId
. This method takes aString
that can be whatever you like to identify the attachment. Second, unlike the other methods for setting content, this one does not take aString
forContent-Type
. This method takes care of setting theContent-Type
header for you, something that is possible because one of the things aDataHandler
object does is to determine the data type of the file it contains.Accessing an AttachmentPart Object
If you receive a message with attachments or want to change an attachment to a message you are building, you need to access the attachment. The
SOAPMessage
class provides two versions of thegetAttachments
method for retrieving itsAttachmentPart
objects. When it is given no argument, the methodSOAPMessage.getAttachments
returns ajava.util.Iterator
object over all theAttachmentPart
objects in a message. WhengetAttachments
is given aMimeHeaders
object, which is a list of MIME headers,getAttachments
returns an iterator over theAttachmentPart
objects that have a header that matches one of the headers in the list. The following code uses thegetAttachments
method that takes no arguments and thus retrieves all theAttachmentPart
objects in theSOAPMessage
objectmessage
. Then it prints the content ID, the content type, and the content of eachAttachmentPart
object.java.util.Iterator iterator = message.getAttachments(); while (iterator.hasNext()) { AttachmentPart attachment = (AttachmentPart)iterator.next(); String id = attachment.getContentId(); String type = attachment.getContentType(); System.out.print("Attachment " + id + " has content type " + type); if (type == "text/plain") { Object content = attachment.getContent(); System.out.println("Attachment " + "contains:\n" + content); } }Adding Attributes
An XML element can have one or more attributes that give information about that element. An attribute consists of a name for the attribute followed immediately by an equal sign (
=
) and its value.The
SOAPElement
interface provides methods for adding an attribute, for getting the value of an attribute, and for removing an attribute. For example, in the following code fragment, the attribute namedid
is added to theSOAPElement
objectperson
. Becauseperson
is aSOAPElement
object rather than aSOAPBodyElement
object orSOAPHeaderElement
object, it is legal for itsName
object to contain only a local name.These lines of code will generate the first line in the following XML fragment.
The following line of code retrieves the value of the attribute whose name is
id
.If you had added two or more attributes to
person
, the preceding line of code would have returned only the value for the attribute namedid
. If you wanted to retrieve the values for all the attributes forperson
, you would use the methodgetAllAttributes
, which returns an iterator over all the values. The following lines of code retrieve and print each value on a separate line until there are no more attribute values. Note that theIterator.next
method returns a JavaObject
, which is cast to aName
object so that it can be assigned to theName
objectattributeName
. (The examples in DOMExample.java and DOMSrcExample.java use code similar to this.)Iterator iterator = person.getAllAttributes(); while (iterator.hasNext()){ Name attributeName = (Name) iterator.next(); System.out.println("Attribute name is " + attributeName.getQualifiedName()); System.out.println("Attribute value is " + element.getAttributeValue(attributeName)); }The following line of code removes the attribute named
id
fromperson
. The variablesuccessful
will betrue
if the attribute was removed successfully.In this section you have seen how to add, retrieve, and remove attributes. This information is general in that it applies to any element. The next section discusses attributes that can be added only to header elements.
Header Attributes
Attributes that appear in a
SOAPHeaderElement
object determine how a recipient processes a message. You can think of header attributes as offering a way to extend a message, giving information about such things as authentication, transaction management, payment, and so on. A header attribute refines the meaning of the header, whereas the header refines the meaning of the message contained in the SOAP body.The SOAP 1.1 specification defines two attributes that can appear only in
SOAPHeaderElement
objects:actor
andmustUnderstand
. The next two sections discuss these attributes.See HeaderExample.java for an example that uses the code shown in this section.
The Actor Attribute
The
actor
attribute is optional, but if it is used, it must appear in aSOAPHeaderElement
object. Its purpose is to indicate the recipient of a header element. The default actor is the message's ultimate recipient; that is, if no actor attribute is supplied, the message goes directly to the ultimate recipient.An actor is an application that can both receive SOAP messages and forward them to the next actor. The ability to specify one or more actors as intermediate recipients makes it possible to route a message to multiple recipients and to supply header information that applies specifically to each of the recipients.
For example, suppose that a message is an incoming purchase order. Its
SOAPHeader
object might haveSOAPHeaderElement
objects with actor attributes that route the message to applications that function as the order desk, the shipping desk, the confirmation desk, and the billing department. Each of these applications will take the appropriate action, remove theSOAPHeaderElement
objects relevant to it, and send the message on to the next actor.
Note: Although the SAAJ API provides the API for adding these attributes, it does not supply the API for processing them. For example, the actor attribute requires that there be an implementation such as a messaging provider service to route the message from one actor to the next.
An actor is identified by its URI. For example, the following line of code, in which
orderHeader
is aSOAPHeaderElement
object, sets the actor to the given URI.Additional actors can be set in their own
SOAPHeaderElement
objects. The following code fragment first uses theSOAPMessage
objectmessage
to get itsSOAPHeader
objectheader
. Thenheader
creates fourSOAPHeaderElement
objects, each of which sets itsactor
attribute.SOAPHeader header = message.getSOAPHeader(); SOAPFactory soapFactory = SOAPFactory.newInstance(); String nameSpace = "ns"; String nameSpaceURI = "http://gizmos.com/NSURI"; Name order = soapFactory.createName("orderDesk", nameSpace, nameSpaceURI); SOAPHeaderElement orderHeader = header.addHeaderElement(order); orderHeader.setActor("http://gizmos.com/orders"); Name shipping = soapFactory.createName("shippingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement shippingHeader = header.addHeaderElement(shipping); shippingHeader.setActor("http://gizmos.com/shipping"); Name confirmation = soapFactory.createName("confirmationDesk", nameSpace, nameSpaceURI); SOAPHeaderElement confirmationHeader = header.addHeaderElement(confirmation); confirmationHeader.setActor( "http://gizmos.com/confirmations"); Name billing = soapFactory.createName("billingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement billingHeader = header.addHeaderElement(billing); billingHeader.setActor("http://gizmos.com/billing");The
SOAPHeader
interface provides two methods that return ajava.util.Iterator
object over all theSOAPHeaderElement
objects that have an actor that matches the specified actor. The first method,examineHeaderElements
, returns an iterator over all the elements that have the specified actor.The second method,
extractHeaderElements
, not only returns an iterator over all theSOAPHeaderElement
objects that have the specified actor attribute but also detaches them from theSOAPHeader
object. So, for example, after the order desk application did its work, it would callextractHeaderElements
to remove all of theSOAPHeaderElement
objects that applied to it.Each
SOAPHeaderElement
object can have only one actor attribute, but the same actor can be an attribute for multipleSOAPHeaderElement
objects.Two additional
SOAPHeader
methods--examineAllHeaderElements
andextractAllHeaderElements
--allow you to examine or extract all the header elements, whether or not they have an actor attribute. For example, you could use the following code to display the values of all the header elements:Iterator allHeaders = header.examineAllHeaderElements(); while (allHeaders.hasNext()) { SOAPHeaderElement headerElement = (SOAPHeaderElement)allHeaders.next(); Name headerName = headerElement.getElementName(); System.out.println("\nHeader name is " + headerName.getQualifiedName()); System.out.println("Actor is " + headerElement.getActor()); }The mustUnderstand Attribute
The other attribute that must be added only to a
SOAPHeaderElement
object ismustUnderstand
. This attribute says whether or not the recipient (indicated by theactor
attribute) is required to process a header entry. When the value of themustUnderstand
attribute istrue
, the actor must understand the semantics of the header entry and must process it correctly to those semantics. If the value isfalse
, processing the header entry is optional. ASOAPHeaderElement
object with nomustUnderstand
attribute is equivalent to one with amustUnderstand
attribute whose value isfalse
.The
mustUnderstand
attribute is used to call attention to the fact that the semantics in an element are different from the semantics in its parent or peer elements. This allows for robust evolution, ensuring that a change in semantics will not be silently ignored by those who may not fully understand it.If the actor for a header that has a
mustUnderstand
attribute set totrue
cannot process the header, it must send a SOAP fault back to the sender. (See Using SOAP Faults.) The actor must not change state or cause any side effects, so that, to an outside observer, it appears that the fault was sent before any header processing was done.The following code fragment creates a
SOAPHeader
object with aSOAPHeaderElement
object that has amustUnderstand
attribute.SOAPHeader header = message.getSOAPHeader(); Name name = soapFactory.createName("Transaction", "t", "http://gizmos.com/orders"); SOAPHeaderElement transaction = header.addHeaderElement(name); transaction.setMustUnderstand(true); transaction.addTextNode("5");This code produces the following XML:
<SOAP-ENV:Header> <t:Transaction xmlns:t="http://gizmos.com/orders" SOAP-ENV:mustUnderstand="1"> 5 </t:Transaction> </SOAP-ENV:Header>You can use the
getMustUnderstand
method to retrieve the value of themustUnderstand
attribute. For example, you could add the following to the code fragment at the end of the preceding section:Using SOAP Faults
In this section, you will see how to use the API for creating and accessing a SOAP fault element in an XML message.
Overview of SOAP Faults
If you send a message that was not successful for some reason, you may get back a response containing a SOAP fault element, which gives you status information, error information, or both. There can be only one SOAP fault element in a message, and it must be an entry in the SOAP body. Furthermore, if there is a SOAP fault element in the SOAP body, there can be no other elements in the SOAP body. This means that when you add a SOAP fault element, you have effectively completed the construction of the SOAP body.
A
SOAPFault
object, the representation of a SOAP fault element in the SAAJ API, is similar to anException
object in that it conveys information about a problem. However, aSOAPFault
object is quite different in that it is an element in a message'sSOAPBody
object rather than part of thetry
/catch
mechanism used forException
objects. Also, as part of theSOAPBody
object, which provides a simple means for sending mandatory information intended for the ultimate recipient, aSOAPFault
object only reports status or error information. It does not halt the execution of an application, as anException
object can.If you are a client using the SAAJ API and are sending point-to-point messages, the recipient of your message may add a
SOAPFault
object to the response to alert you to a problem. For example, if you sent an order with an incomplete address for where to send the order, the service receiving the order might put aSOAPFault
object in the return message telling you that part of the address was missing.Another example of who might send a SOAP fault is an intermediate recipient, or actor. As stated in the section Adding Attributes, an actor that cannot process a header that has a
mustUnderstand
attribute with a value oftrue
must return a SOAP fault to the sender.A
SOAPFault
object contains the following elements:
- A fault code: Always required. The fault code must be a fully qualified name: it must contain a prefix followed by a local name. The SOAP 1.1 specification defines a set of fault code local name values in section 4.4.1, which a developer can extend to cover other problems. The default fault code local names defined in the specification relate to the SAAJ API as follows:
VersionMismatch
: The namespace for aSOAPEnvelope
object was invalid.MustUnderstand
: An immediate child element of aSOAPHeader
object had itsmustUnderstand
attribute set totrue
, and the processing party did not understand the element or did not obey it.Client
: TheSOAPMessage
object was not formed correctly or did not contain the information needed to succeed.Server
: TheSOAPMessage
object could not be processed because of a processing error, not because of a problem with the message itself.- A fault string: Always required. A human-readable explanation of the fault.
- A fault actor: Required if the
SOAPHeader
object contains one or moreactor
attributes; optional if no actors are specified, meaning that the only actor is the ultimate destination. The fault actor, which is specified as a URI, identifies who caused the fault. For an explanation of what an actor is, see The Actor Attribute.- A Detail object: Required if the fault is an error related to the
SOAPBody
object. If, for example, the fault code isClient
, indicating that the message could not be processed because of a problem in theSOAPBody
object, theSOAPFault
object must contain aDetail
object that gives details about the problem. If aSOAPFault
object does not contain aDetail
object, it can be assumed that theSOAPBody
object was processed successfully.Creating and Populating a SOAPFault Object
You have seen how to add content to a
SOAPBody
object; this section walks you through adding aSOAPFault
object to aSOAPBody
object and then adding its constituent parts.As with adding content, the first step is to access the
SOAPBody
object.With the
SOAPBody
objectbody
in hand, you can use it to create aSOAPFault
object. The following line of code creates aSOAPFault
object and adds it tobody
.The
SOAPFault
interface provides convenience methods that create an element, add the new element to theSOAPFault
object, and add a text node, all in one operation. For example, in the following lines of code, the methodsetFaultCode
creates afaultcode
element, adds it tofault
, and adds aText
node with the value"SOAP-ENV:Server"
by specifying a default prefix and the namespace URI for a SOAP envelope.Name faultName = soapFactory.createName("Server", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultActor("http://gizmos.com/orders"); fault.setFaultString("Server not responding");The
SOAPFault
objectfault
, created in the preceding lines of code, indicates that the cause of the problem is an unavailable server and that the actor athttp://gizmos.com/orders
is having the problem. If the message were being routed only to its ultimate destination, there would have been no need to set a fault actor. Also note thatfault
does not have aDetail
object because it does not relate to theSOAPBody
object.The following code fragment creates a
SOAPFault
object that includes aDetail
object. Note that aSOAPFault
object can have only oneDetail
object, which is simply a container forDetailEntry
objects, but theDetail
object can have multipleDetailEntry
objects. TheDetail
object in the following lines of code has twoDetailEntry
objects added to it.SOAPFault fault = body.addFault(); Name faultName = soapFactory.createName("Client", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultString("Message does not have necessary info"); Detail detail = fault.addDetail(); Name entryName = soapFactory.createName("order", "PO", "http://gizmos.com/orders/"); DetailEntry entry = detail.addDetailEntry(entryName); entry.addTextNode("Quantity element does not have a value"); Name entryName2 = soapFactory.createName("confirmation", "PO", "http://gizmos.com/confirm"); DetailEntry entry2 = detail.addDetailEntry(entryName2); entry2.addTextNode("Incomplete address: no zip code");See SOAPFaultTest.java for an example that uses code like that shown in this section.
Retrieving Fault Information
Just as the
SOAPFault
interface provides convenience methods for adding information, it also provides convenience methods for retrieving that information. The following code fragment shows what you might write to retrieve fault information from a message you received. In the code fragment,newMessage
is theSOAPMessage
object that has been sent to you. Because aSOAPFault
object must be part of theSOAPBody
object, the first step is to access theSOAPBody
object. Then the code tests to see whether theSOAPBody
object contains aSOAPFault
object. If it does, the code retrieves theSOAPFault
object and uses it to retrieve its contents. The convenience methodsgetFaultCode
,getFaultString
, andgetFaultActor
make retrieving the values very easy.SOAPBody body = newMessage.getSOAPBody(); if ( body.hasFault() ) { SOAPFault newFault = body.getFault(); Name code = newFault.getFaultCodeAsName(); String string = newFault.getFaultString(); String actor = newFault.getFaultActor();Next the code prints the values it has just retrieved. Not all messages are required to have a fault actor, so the code tests to see whether there is one. Testing whether the variable
actor
isnull
works because the methodgetFaultActor
returnsnull
if a fault actor has not been set.System.out.println("SOAP fault contains: "); System.out.println(" Fault code = " + code.getQualifiedName()); System.out.println(" Fault string = " + string); if ( actor != null ) { System.out.println(" Fault actor = " + actor); }The final task is to retrieve the
Detail
object and get itsDetailEntry
objects. The code uses theSOAPFault
objectnewFault
to retrieve theDetail
objectnewDetail
, and then it usesnewDetail
to call the methodgetDetailEntries
. This method returns thejava.util.Iterator
objectentries
, which contains all theDetailEntry
objects innewDetail
. Not allSOAPFault
objects are required to have aDetail
object, so the code tests to see whethernewDetail
isnull
. If it is not, the code prints the values of theDetailEntry
objects as long as there are any.Detail newDetail = newFault.getDetail(); if ( newDetail != null) { Iterator entries = newDetail.getDetailEntries(); while ( entries.hasNext() ) { DetailEntry newEntry = (DetailEntry)entries.next(); String value = newEntry.getValue(); System.out.println(" Detail entry = " + value); } }In summary, you have seen how to add a
SOAPFault
object and its contents to a message as well as how to retrieve the contents. ASOAPFault
object, which is optional, is added to theSOAPBody
object to convey status or error information. It must always have a fault code and aString
explanation of the fault. ASOAPFault
object must indicate the actor that is the source of the fault only when there are multiple actors; otherwise, it is optional. Similarly, theSOAPFault
object must contain aDetail
object with one or moreDetailEntry
objects only when the contents of theSOAPBody
object could not be processed successfully.See SOAPFaultTest.java for an example that uses code like that shown in this section.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.