Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

The SOAP Messaging Framework

The first part of the SOAP specification is primarily concerned with defining how SOAP messages are structured and the rules processors must abide by when producing and consuming them. Let's look at a sample SOAP message, the inventory check request described in our earlier example:

NOTE

All the wire examples in this book have been obtained by using the tcpmon tool, which is included in the Axis distribution you can obtain with the example package from the Sams Web site. Tcpmon (short for TCP monitor) allows you to record the traffic to and from a particular TCP port, typically HTTP requests and responses. We'll go into detail about this utility in Chapter 5.

POST /axis/InventoryCheck.jws HTTP/1.0 Content-Type: application/soap+xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
 <doCheck soapenv:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
  <arg0 xsi:type="soapenc:string"
     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">947-TI</arg0>
  <arg1 xsi:type="soapenc:int"
     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">3</arg1>
 </doCheck>
 </soapenv:Body>
</soapenv:Envelope>

This is clearly an XML document (Chapter 2, "XML Primer," covered XML in detail), which has been sent via an HTTP POST. We've removed a few of the nonrelevant HTTP headers from the trace, but we left the content-type header, which indicates that this POST contains a SOAP message (note that this content-type would be different for SOAP 1.1—see the sidebar for details). We'll cover the HTTP-specific parts of SOAP interactions further a bit later in the chapter.

The root element is soapenv:Envelope, in the http://www.w3.org/2003/05/soap-envelope namespace, which surrounds a soapenv:Body containing application-specific content that represents the central purpose of the message. In this case we're asking for an inventory check, so the central purpose is the doCheck element. The Envelope element has a few useful namespace declarations on it, for the SOAP envelope namespace and the XML Schema data and instance namespaces.

SOAP 1.1 Difference: Identifying SOAP Content

The SOAP 1.1 envelope namespace is http://schemas.xmlsoap.org/soap/envelope/, whereas for SOAP 1.2 it has changed to http://www.w3.org/2003/05/soap-envelope. This namespace is used for defining the envelope elements and for versioning, which we will explain in more detail in the "Versioning in SOAP" section.

The content-type used when sending SOAP messages across HTTP connections has changed as well—it was text/xml for SOAP 1.1 but is now application/soap+xml for SOAP 1.2. This is a great improvement, since text/xml is a generic indicator for any type of XML content. The content type was so generic that machines had to use the presence of a custom HTTP header called SOAPAction: to tell that XML traffic was, in fact, SOAP (see the section on the HTTP binding for more). Now the standard MIME infrastructure handles this for us.

The doCheck element represents the remote procedure call to the inventory check service. We'll talk more about using SOAP for RPCs in a while; for now, notice that the name of the method we're invoking is the name of the element directly inside the soapenv:Body, and the arguments to the method (in this case, the SKU number and the quantity desired) are encoded inside the method element as arg0 and arg1. The real names for these parameters in Java are SKU and quantity; but due to the ad-hoc way we're calling this method, the client doesn't have any way of knowing that information, so it uses the generated names arg0 and arg1.

The response to this message, which comes back across in the HTTP response, looks like this:

Content-Type: application/soap+xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
  xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <doCheckResponse
    soapenv:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
  <rpc:result xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">return</rpc:result>
  <return xsi:type="xsd:boolean">true</return>
 </doCheckResponse>
 </soapenv:Body>
</soapenv:Envelope>

The response is also a SOAP envelope, and it contains an encoded representation of the result of the RPC call (in this case, the Boolean value true).

What good is having this envelope structure, when we could send our XML formats directly over a transport like HTTP without a wrapper? Good question; as we answer it, we'll examine some more details of the protocol.

Vertical Extensibility

Let's say you want your purchase order to be extensible. Perhaps you want to include security in the document someday, or you might want to enable a notarization service to associate a token with a particular purchase order, as a third-party guarantee that the PO was sent and contained particular items. How might you make that happen?

You could drop extensibility elements directly into your document before sending it. If we took the purchase order from the last chapter and added a notary token, it might look something like this:

<po id="43871" submitted="2004-01-05" customerId="73852">
 <notary:token xmlns:notary="http://notaries-r-us.com">
  XQ34Z-4G5
 </notary:token>
 <billTo>
  <company>The Skateboard Warehouse</company>
  ...
 </billTo>
 ...
</po>

To do things this way, and make it easy for your partners to use, you'd need to do two things. First, your schema would have to be explicitly extensible at any point in the structure where you might want to add functionality later (this can be accomplished in a number of ways, including the xsd:any/ schema construct); otherwise, documents containing extension elements wouldn't validate. Second, you would need to agree on rules by which those extensibility elements were to be processed—which ones are optional, which ones affect which parts of the document, and so on. Both of these requirements present challenges. Not all schemas have been designed for extensibility, and you may need to extend a document that follows a preexisting standard format that wasn't built that way. Also, processing rules might vary from document type to document type, so it would be challenging to have a uniform model with which to build a common processor. It would be nice to have a standardized framework for implementing arbitrary extensibility in a way that everyone could agree on.

It turns out that the SOAP envelope, in addition to containing a body (which must always be present), may also contain an optional Header element—and the SOAP Header structure gives us just what we want in an XML extensibility system. It's a convenient and well-defined place in which to put our extensibility elements. Headers are just XML elements that live inside the soapenv:Header/soapenv:Header tags in the envelope. The soapenv:Header always appears, incidentally, before the soapenv:Body if it's present. (Note that in the SOAP 1.2 spec, the extensibility elements are known as header blocks. However, the industry—and the rest of this book—colloquially refers to them simply as headers.)

Let's look at the extensibility example recast as a SOAP message with a header:

<soapenv:Envelope
  xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
 <soapenv:Header>
 <notary:token xmlns:notary="http://notaries-r-us.com">
  XQ34Z-4G5
 </notary:token>
 </soapenv:Header>
 <soapenv:Body>
 <PO>
  ...normal purchase order here...
 </PO>
 </soapenv:Body>
</soapenv:Envelope>

Since the SOAP envelope wraps around whatever XML content you want to send in the body (the PO, in this example), you can use the Header to insert extensions (the notary:token header) without modifying the central core of the message. This can be compared to a situation in real life where you want to send a document and some auxiliary information, but you don't want to mark up the document—so you put the document inside an envelope and then add another piece of paper or two describing your extra information.

Each individual header represents one piece of extensibility information that travels with your message. A lot of other protocols have this same basic concept—we're all familiar with the email model of headers and body. HTTP also contains headers, and both email and HTTP use the concept of extensible, user-defined headers. However, the headers in protocols like these are simple strings; since SOAP uses XML, you can encode much richer data structures for individual headers. Also, you can use XML's structure to make processing headers much more powerful and flexible than a basic string-based model.

Headers can contain any sort of data imaginable, but typically they're used for two purposes:

  • Extending the messaging infrastructure—Infrastructure headers are typically processed by middleware. The application doesn't see the headers, just their effects. They could be things like security credentials, correlation IDs for reliable messaging, transaction context identifiers, routing controls, or anything else that provides services to the application.

  • Defining orthogonal data—The second category of headers is application defined. These contain data that is orthogonal to the body of the message but is still destined for the application on the receiving side. An example might be extra data to accompany nonextensible schemas—if you wanted to add more customer data fields but couldn't change the billTo element, for instance.

Using headers to add functionality to messages is known as vertical extensibility, because the headers build on top of the message. A little later we'll discuss horizontal extensibility as well.

Now that you know the basics, we'll consider some of the additional framework that SOAP supplies for headers and how to use it. After that, we'll explain the SOAP processing model, which is the key to SOAP's scalability and expressive power.

The mustUnderstand Flag

Some extensions might use headers to carry data that's nice to know but not critical to the main purpose of the SOAP message. For instance, you might be invoking a "buy book" operation on a store's Web service. You receive a header in the response confirmation message that contains a list of other books the site thinks you might find interesting. If you know how to process that extension, then you might offer a UI to access those books. But if you don't, it doesn't matter—your original request was still processed successfully. On the other hand, suppose the request message of that same "buy book" operation contained private information (such as a credit card number). The sender might want to encrypt the XML in the SOAP body to prevent snooping. To make sure the other side knows what to do with the postencryption data inside the body, the sender inserts a header that describes how to decrypt the message. That header is important, and anyone trying to process the message without correctly processing the header and decrypting the body is going to run into trouble.

This is why we have the mustUnderstand g attribute, which is always in the SOAP envelope namespace. Here's what our notary header would look like with that attribute:

 <notary:token xmlns:notary="http://notaries-r-us.com"
   soapenv:mustUnderstand="true">
  XQ34Z-4G5
 </notary:token>

By marking things mustUnderstand (when we refer to headers "marked mustUnderstand," we mean having the soapenv:mustUnderstand attribute set to true), you're saying that the receiver must agree to all the terms of your extension specification or they can't process the message. If the mustUnderstand attribute is set to false or is missing, the header is defined as optional—in this case, processors not familiar with the extension can still safely process the message and ignore the optional header.

SOAP 1.1 Difference: mustUnderstand

In SOAP 1.2, the mustUnderstand attribute may have the values 0/false (false) or 1/true (true). In SOAP 1.1, despite the fact that XML allows true and false for Boolean values, the only legal mustUnderstand values are 0 and 1.

The mustUnderstand attribute is a key part of the SOAP processing model, since it allows you to build extensions that fundamentally change how a given message is processed in a way that is guaranteed to be interoperable. Interoperable here means that you can always know how to gracefully fail in the face of extensions that aren't understood.

SOAP Modules

When you implement a semantic using SOAP headers, you typically want other parties to use your extension, unless it's purely for internal use. As such, you typically write a specification that details all the constraints, rules, preconditions, and data formats of your extension. These specifications are known as SOAP modules. Modules are named with URIs so they can be referenced, versioned, and reasoned about. We'll talk more about module specifications when we get to the SOAP binding framework a bit later.

  • + Share This
  • 🔖 Save To Your Account