Skip to main content

Manipulate Web services messages using the service integration bus, Part 1: JAX-RPC handlers and the SIBus

JAX-RPC handlers and the SIBus

Chris Whyley (whyleyc@uk.ibm.com), Software Test Engineer, IBM Hursley Software Laboratory
Author photo
Chris Whyley is a technical leader in Web services testing at the IBM Hursley Software Laboratory in the UK, where he helps to put Service Integration Bus technologies through their paces. Chris has worked with a wide range of Web services technologies across several releases of WebSphere Application Server, and is co-author of the WebSphere Version 6 Web Services Handbook. You can contact Chris at whyleyc@uk.ibm.com.

Summary:  Get an overview of the powerful Web services message manipulation features of the Service Integration Bus (SIBus). Part 1 of this two-part series offers a brief introduction to the SIBus, looks at the options for intercepting and handling Web services messages transporting across the SIBus, and discusses the advantages and disadvantages of the two main approaches to dealing with them. Detailing the first approach, Chris Whyley shows how to code and deploy JAX-RPC handlers to usefully work with the messages target Web services send and receive.

View more content in this series

Date:  31 May 2005
Level:  Intermediate
Activity:  930 views

Introduction to the Service Integration Bus

The Service Integration Bus (SIBus) is a key part of IBM® WebSphere® Application Server V6.0 (Application Server), allowing users to take advantage of a host of messaging and Web services options. Incorporating the functionality of the Web Services Gateway, the SIBus builds upon the asynchronous messaging features of WebSphere and allows users to manage Web services through administrative panels and commands. Existing Web services can be exposed through the SIBus, which you can set up to act as a proxy, allowing users to expose Web services at new destinations on the SIBus, and manipulate incoming and outgoing message content (see Figure 1 below). By decoupling the link between service requestors and providers, the SIBus gives users tighter control over the services they manage, allowing:

  • Secure exposure of Web services hidden behind a company firewall
  • Protocol transformation of incoming messages before they are passed to target services (for example, from SOAP/HTTP to SOAP/JMS)
  • The ability to inspect and then route incoming messages to different services
  • Straightforward management of message handlers
  • Advanced message options for asynchronous communication (such as prioritized message delivery and message persistence)

Figure 1. High-level overview of the SIBus
Figure 1. High-level overview of the SIBus

Why access a message?

Users might want to access a Web services message passing through the SIBus for many reasons. At the most basic level, message inspection is useful for logging purposes, as system administrators might want to keep a record of a particular transaction passing through an application server -- for auditability or simply for building up a record of the most regularly used Web services. In more advanced configurations, users might want to access messages in order to inspect authentication information (when using WS-Security), or to deal with transactions (when WS-Transaction is involved).

Message inspection is only one aspect of this processing though. One of the most powerful features of message handlers is their ability to manipulate and augment message content and message header information. Using the options available in the SIBus, you can route messages to different targets, change parts and operation names, and transform whole messages from one format to another. On top of this, you can also construct handler chains to further increase control over message flow and content.


Manipulating messages: Handlers or mediations?

Users have two options when using the SIBus to manipulate Web services messages: JAX-RPC handlers and mediations. Each has a unique set of advantages and disadvantages. This section examines the two approaches before attempting to unravel when and why you might want to use each of them (for a summary, see Table 1 below).

JAX-RPC handlers

JAX-RPC handlers have become increasingly popular within the J2EE community as the de-facto way of manipulating in-transit Web services message content. They are well defined, straightforward to deploy, use easily recognizable deployment descriptors, and allow users fine-grained control over most aspects of SOAP messages, whether over JMS or HTTP. You can find the latest formal specification for JAX-RPC handlers in JSR 921 (see Resources). This specification describes how you can implement handlers in either the client or server environment. Regardless of where you place them though, the characteristics of JAX-RPC handlers are the same -- They are independent of particular transports, capable of modifying certain types of SOAP body and header message content, and can process both inbound or outbound requests, giving users the ability to examine or modify a SOAP message before it is passed to the ultimate receiver. Multiple handlers can be chained together in JAX-RPC handler lists, which allow fine-grained control over the order in which they are executed. While handlers are often easy to use and can be dynamically deployed into the SIBus, they are not able to modify every aspect of the SOAP message -- operation information, part names, and sequences, for instance, are all off limits.

Mediations

Mediations are a new concept in WebSphere V6.0, and are designed to perform a similar function to JAX-RPC handlers. They can access and modify in-flight messages within Application Server. Unlike JAX-RPC handlers, however, they also have the ability to examine non-SOAP content, such as SDO or JMS messages, giving users greater freedom over the types of message they can access. Mediations can also take control over message delivery and provide powerful options for re-routing messages to alternative destinations, or clone messages and spray them out to multiple destinations for further processing. You can also use mediations to perform complex transformations on messages to change them from one format to another. In short, although mediations are limited to running within the server container, they provide a host of options for managing messages, and should be considered as a serious alternative to JAX-RPC handlers when you need more powerful manipulation, or when you are operating in an environment with mixed styles of messages. Part 2 of this article discusses mediations in detail.

Table 1. Comparing capabilities of SIBus message handlers

Handler typeBenefitsLimitations
JAX-RPC handler - Existing handler code can be easily deployed into the bus
- Conforms to JSR specifications
- Transport independent
- Can run in the client or server container
- Processing restricted to SOAP messages
- Cannot modify the target of a request
- Cannot modify operation info
- Cannot modify message part types or sequence
Mediation - Can access JMS and SDO messages as well as SOAP
- May transform a message from one format to another
- Can distribute messages to multiple destinations in a bus
- Can modify routing details to direct message to different target
- Can only run in the server container

Writing JAX-RPC handlers

You can find abundant literature on writing useful JAX-RPC handlers, and many good code examples are available for reuse from within the Java™ community. Some of the best practices for coding handlers include:

  • Try to write discreet function in handlers. (This enables them to be more easily combined together using handler lists.)
  • Write function that might be useful to both your client and server processes.
  • Follow the specifications to avoid compatibility problems.

The example below shows how to write a simple handler to log details of the incoming SOAP message to standard output. Such a handler can be extremely useful for helping to debug problems within a Web services application. Poorly formed SOAP messages can sometimes be difficult to spot, can be caused by a number of factors, and can hamper application development and performance. You can copy code for this handler from Listing 1 below, or download it from the Resources section at the end of this article.
Note: As a prerequisite for using this handler with a Web service in the SIBus, ensure that you have completed the steps in the developerWorks article, Create a simple HTTP Web services gateway service with WebSphere Application Server V6 , to create a simple service. Download the associated code from the article, or from the "More downloads" section below.


Listing 1. Sample JAX-RPC handler to print out a SOAP message
// Compile with j2ee.jar and xml.jar
package com.ibm.developerworks;

import java.util.Iterator;
import javax.xml.rpc.handler.*;
import javax.xml.rpc.handler.soap.*;
import javax.xml.soap.*;

public class DevWorksHandler1 implements javax.xml.rpc.handler.Handler {	
	
	public void init(HandlerInfo config) {
		System.out.println("****>> DevWorksHandler1: init()");
	}
	
	public boolean handleRequest(MessageContext context) {
		System.out.println("****>> DevWorksHandler1: handleRequest()");
		
		try {			
			// Get existing MessageContext and from it the SOAP Envelope	
			SOAPMessageContext smc = (SOAPMessageContext)context;
			SOAPMessage msg = smc.getMessage();
			SOAPPart sp = msg.getSOAPPart();			
			SOAPEnvelope se = sp.getEnvelope();
			
			// Get child elements of the SOAP Body
			Iterator iter = se.getChildElements();
			
			// Print out all the elements
			while (iter.hasNext()) {
				  SOAPElement sel = (SOAPElement) iter.next();
				  System.out.println(sel);			  
			}

		}
		catch (Exception e) {
			System.out.println("****>> DevWorksHandler1: Exception raised");
		 	e.printStackTrace();
		}
      		
		return true;
	}
	
	public void destroy() {
		System.out.println("****>> DevWorksHandler1: destroy()");
	}
	
	public javax.xml.namespace.QName[] getHeaders() {
		System.out.println("****>> DevWorksHandler1: getHeaders()");
		return null;
	}
	
	public boolean handleResponse(MessageContext context) {
		System.out.println("****>> DevWorksHandler1: handleResponse()");
		return true;
	}
	
	public boolean handleFault(MessageContext context) {
		System.out.println("****>> DevWorksHandler1: handleFault()");
		return true;
	}

}


Deploying JAX-RPC handlers to the SIBus

To make any JAX-RPC handler available for use in the SIBus, you must first ensure that the class is available and loaded into your application server. The simplest way to do this is to deploy a .jar file containing the handler(s) you wish to use into the WebSphere "classes" directory. By restarting the application server process, any handlers you have included in this directory will be picked up by the server and ready to use.

After doing this, you are then in a position to define the handler in the SIBus. You can do this from the Application Server administrative console (see Figure 2 below):

  1. In the navigation pane select Service integration --> Web services --> JAX-RPC Handlers.
  2. From the main window, click New.
  3. Fill in the details for your handler. The name and description are user-defined, but the class name must match the full class name (with packages) of the handler you wish to deploy. In this case use:
    Name : DevWorksHandler1
    Class Name : com.ibm.developerworks.DevWorksHandler1
  4. Click OK to save your new configuration.

Figure 2. Defining a JAX-RPC handler in the SIBus
Figure 2. Defining a JAX-RPC handler in the SIBus

Using JAX-RPC handler lists

After you deploy your handler into the SIBus, you then need to add it to a JAX-RPC handler list -- the order in which handlers appear in these lists determines the order in which they are executed. This feature allows users to dynamically compose functions in the SIBus and ensure they have full control over the message path through their JAX-RPC handlers. In this example, even with only one handler, you will still add it to a list so that you can later associate this list with a service and allow the option of adding more handlers to the list in the future.

To add your handler to a list using the administrative console (see Figure 3 below):

  1. In the navigation pane select Service integration --> Web services --> JAX-RPC Handler Lists.
  2. From the main window, click New.
  3. Fill in the details for your handler. The name and description are user-defined. In this case use:
    Name : DevWorksHandlerList
  4. Click on DevWorksHandler1 followed by the Add button to place it into the list.
  5. If you were working with more than one handler, you could use the Up and Down buttons to determine the order in which they are executed.
  6. Click OK to save your new configuration.

Figure 3. Defining a JAX-RPC handler list in the SIBus
Figure 3. Defining a JAX-RPC handler list in the SIBus

Associating JAX-RPC handler lists with services

Now that you have defined your JAX-RPC handler list, any service defined in the SIBus can use it. Typically, you would want to associate the list with an inbound or outbound service, two of the main administrative building blocks for exposing Web services in the SIBus. Binding the handler list to your bus service gives you the power to examine and manipulate in-flight SOAP messages before they reach their final destination (for more detail on setting up and using the SIBus please see the various articles and books in Resources). The example below assumes that you have followed the instructions in the recent developerWorks article, "Create a Simple HTTP Web Services Gateway Service with WebSphere Application Server V6" (see Resources).

To add your new handler list to a service using the administrative console (see Figure 4 below):

  1. In the navigation pane select Service integration --> Buses --> DeveloperWorksBus --> Inbound Services --> DeveloperWorksGatewayService --> Inbound Ports --> SOAPHTTPChannel1InboundPort.
  2. From the main window, select the DevWorksHandlerList from the JAX-RPC handler drop-down list.
  3. Click OK to save your new configuration.

Figure 4. Adding a JAX-RPC handler list to a service
Figure 4. Adding a JAX-RPC handler list to a service

Testing your JAX-RPC Handler

Assuming you have followed the steps so far, you should now be able to test your newly configured JAX-RPC handler list by invoking the associated gateway service, using the sample client provided in the related developerWorks article. On sending a string to the gateway service, the JAX-RPC handler will be invoked and produce the following message in the server SystemOut.log file:


Listing 2. JAX-RPC handler output

****>> DevWorksHandler1: init()
****>> DevWorksHandler1: handleRequest()
<soapenv:Header
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<soapenv:Body
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <p124:setReturnString xmlns:p124="http://service.string">
       <arg_0_1>CJW-TestString</arg_0_1>
     </p124:setReturnString>
</soapenv:Body>
StringService Receives and Returns: CJW-TestString
****>> DevWorksHandler1: handleResponse()


Summary

This article introduced the two main approaches to manipulating in-flight Web services messages in the SIBus within WebSphere -- JAX-RPC handlers and mediations -- and the advantages and disadvantages of both approaches. The example showed how to code and deploy a JAX-RPC handler into the SIBus to inspect and output the contents of an incoming SOAP message. Such a handler helps debug problems with Web services applications, or simply acts as a 'heartbeat' to verify that requests are being received. Part 2 of this article will cover in detail the use of mediations to manipulate Web services messages in the SIBus.



Downloads

DescriptionNameSizeDownload method
Sample JAX-RPC Handler used in this articlews-mmsib1code.zip3 KB HTTP
Sample service and clientDevWorks_Gateway_Sample.zip1.6 KB FTP

Information about download methods


Resources

About the author

Author photo

Chris Whyley is a technical leader in Web services testing at the IBM Hursley Software Laboratory in the UK, where he helps to put Service Integration Bus technologies through their paces. Chris has worked with a wide range of Web services technologies across several releases of WebSphere Application Server, and is co-author of the WebSphere Version 6 Web Services Handbook. You can contact Chris at whyleyc@uk.ibm.com.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=84178
ArticleTitle=Manipulate Web services messages using the service integration bus, Part 1: JAX-RPC handlers and the SIBus
publish-date=05312005
author1-email=whyleyc@uk.ibm.com
author1-email-cc=flanders@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers