Header elements and the AXIS toolkit
SOAP, the Simple Object Access Protocol, is an evolving W3C standard. Developed by IBM, Microsoft, DevelopMentor, and UserLand Software for the exchange of information over a network, SOAP stands at the point where three technologies — Remote Procedure Call (RPC), XML, and Web applications — are converging. The combination is new so developers are still learning how to deploy it most effectively. In most first-generation applications, SOAP is a replacement for another RPC standard such as RMI or CORBA. But as this tip illustrates, SOAP can offer more.
Intranet and Internet services
Who develops the clients and the servers is determined by whether they’re being deployed on an intranet or on the Internet. On an intranet, the developers for the client and server are either on the same team or are on teams that are closely related. This translates into very strong ties between the client and server.
When it comes to the Internet, things are different. The client and server are often developed by different companies altogether. As a result, they won’t have as many opportunities to synchronize their work. To make things worse, if the service is successful, multiple clients and servers will implement it.
As an example, consider the Google search engine (see Resources), a service that benefits many applications interfacing with it. For instance, I use an XML editor to write my articles. When I research a topic, I find it convenient to run searches directly from the editor. Likewise for e-mail — it’s common to search for a contact address on a corporate Web site. The search often starts with Google, so my address book also benefits from implementing the service. And what about a photo editor? It’s common to search for background information on a subject or to search for new techniques, so the photo editor could interface with Google. That’s three possible clients for the service already — and Google is not the only search engine, so you could have more than one server implementation as well.
Since the client and server are developed independently, the definition of the service interface is very important. The service interface (not unlike an API) is the basis for cooperation between the two teams. It specifies the roles of the client and the server — the methods and their parameters. The interface must be flexible, modular, and extensible.
To encourage modularity and extensibility, SOAP organizes its requests into two parts: the header and the body. The header is typically used for technical parameters of the request such as session management, authentication, and transaction, while the body contains the actual request.
Header elements in AXIS
For simplicity, I chose to process the header in the server itself. AXIS
To illustrate how to implement header extensions, I’ll use AXIS, the Apache SOAP toolkit. The sample service is trivial: It adds up two integers. The service interface has only one method, add(), which takes two integers as parameters.
Imagine that the server can prioritize requests: High priority requests will get a faster answer than low priority ones. How do you extend the service interface? While you could extend the add() method with a third priority parameter, it’s not a good idea, primarily because doing so breaks existing clients and servers. In addition, it pollutes the interface with a technical parameter. Not every server is able to honour the priority, and not every client has a notion of priority. A better solution is to move the technical parameter to the header, where it belongs.
Listing 1 is a SOAP server that recognizes a priority header element. The service uses the MessageContext object to retrieve an instance of a Message object that represents the request. Next, it searches for a priority element in the header. The service sleeps for more or less time, depending on the priority (sleeping emulates low priority requests in this example). Note that the service accepts requests with no priority. Finally it sets the processed flag so AXIS knows the element has been dealt with (this flag is essential if the element has a mustUnderstand attribute). The server is compatible with those clients that do not implement the priority mechanism.
Listing 1. A server processes a header extension
Listing 2 is the client. If a priority argument is in the command line, it creates a SOAPHeaderElement object and passes it to the Call object. Again, this client is compatible with servers that do not implement the priority mechanism.
Listing 2. Corresponding client
SOAP promotes the design of modular and extensible service interfaces through the header element. While this tip demonstrates how to build premium services (faster response) on top of a regular service interface, the technique also applies to other technical parameters.
• Participate in the discussion forum for Benoit Marchal’s Working XML column.
• Download the source code used in this article.
• Read “A lightweight XML client,” in which Benoit Marchal presents a lightweight client to interface with SOAP services (developerWorks, September 2003).
• Learn the basic concepts behind SOAP in Dan Gisolfi’s article “Web services architect” (developerWorks, April 2001).
• Catch “Myths and misunderstandings surrounding SOAP” as Frank Cohen debunks some of the myths surrounding SOAP (developerWorks, September 2001).
• Use SOAP to access the popular Google search engine with the Google Web API. It’s one example of a Web service that many applications can use.
• Check out the directory of publicly available SOAP services on XMethods.
• Download the AXIS toolkit used in this article from W3C Recommendation.
• Find out how you can become an IBM Certified Developer in XML and related technologies.