Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Message Queue 3.5 SP1 Java Client Developer's Guide 

Chapter 6
Working With SOAP Messages

Using Message Queue, you can send JMS messages that contain a SOAP payload. This allows you to transport SOAP messages reliably and to publish SOAP messages to JMS subscribers. This chapter explains how you do the following:

This chapter begins with an overview of SOAP processing and describes the Java API for SOAP with attachments (JAXM). You need to know this information to process SOAP messages. The chapter concludes by explaining how you can create a JMS message that contains a SOAP message payload.

If you are familiar with the SOAP specification, you can skip the introductory section and start by reading SOAP Messaging in JAVA.

The remaining sections in this chapter cover the following topics:

What is SOAP?

SOAP, the Simple Object Access Protocol, is a protocol that allows the exchange of structured data between peers in a decentralized, distributed environment. The structure of the data being exchanged is specified by an XML scheme.

The fact that SOAP messages are encoded in XML makes SOAP messages portable, because XML is a portable, system-independent way of representing data. By representing data using XML, you can access data from legacy systems as well as share your data with other enterprises. The data integration offered by XML also makes this technology a natural for web-based computing such as web services. Firewalls can recognize SOAP packets based on their content type (text/xml-SOAP) and can filter messages based on information exposed in the SOAP message header.

The SOAP specification describes a set of conventions for exchanging XML messages. As such, it forms a natural foundation for web services that also need to exchange information encoded in XML. Although any two partners could define their own protocol for carrying on this exchange, having a standard such as SOAP allows developers to build the generic pieces that support this exchange. These pieces might be software that adds functionality to the basic SOAP exchange, or might be tools that administer SOAP messaging, or might even comprise parts of an operating system that supports SOAP processing. Once this support is put in place, other developers can focus on creating the web services themselves.

The SOAP protocol is fully described at This section restricts itself to discussing the reasons why you would use SOAP and to describing some basic concepts that will make it easier to work with the JAXM API.

SOAP and the JAVA for XML Messaging API

The JAVA API for XML messaging (JAXM) is a JAVA-based API that enforces compliance to the SOAP standard. When you use this API to assemble and disassemble SOAP messages, it ensures the construction of syntactically correct SOAP messages. JAXM also makes it possible to automate message processing when several applications need to handle different parts of a message before forwarding it to the next recipient.

Figure 6-1 shows the layers that can come into play in the implementation of SOAP messaging. This chapter focuses on the SOAP and language implementation layers.

Figure 6-1  SOAP Messaging Layers

Diagram showing the functional layers needed for SOAP messaging. Figure contents are explained in text.

The sections that follow describe each layer shown in the preceding figure in greater detail. The rest of this chapter focuses on the SOAP and language implementation layers.

The Transport Layer

Underlying any messaging system is the transport or wire protocol that governs the serialization of the message as it is sent across a wire and the interpretation of the message bits when it gets to the other side. Although SOAP messages can be sent using any number of protocols, the SOAP specification defines only the binding with HTTP. SOAP uses the HTTP request/response message model. It provides SOAP request parameters in an HTTP request and SOAP response parameters in an HTTP response. The HTTP binding has the advantage of allowing SOAP messages to go through firewalls.

The SOAP Layer

Above the transport layer is the SOAP layer. This layer, which is defined in the SOAP Specification, specifies the XML scheme used to identify the message parts: envelope, header, body, and attachments. All SOAP message parts and contents, except for the attachments, are written in XML. The following sample SOAP message shows how XML tags are used to define a SOAP message:





          <m:GetLastTradePrice xmlns:m="Some-URI">





The wire transport and SOAP layers are actually sufficient to do SOAP messaging. You could create an XML document that defines the message you want to send, and you could write HTTP commands to send the message from one side and to receive it on the other. In this case, the client is limited to sending synchronous messages to a specified URL. Unfortunately, the scope and reliability of this kind of messaging is severely restricted. To overcome these limitations, the provider and profile layers are added to SOAP messaging.

The Provider Layer

In Figure 6-1, the provider is shown as two pieces of functionality: a language implementation and delivery semantics.

A provider language implementation allows you to create XML messages that conform to SOAP, using API calls. For example, any implementation of JAXM, allows a Java client to define the SOAP message and all its parts as Java objects. The client would also use JAXM to create a connection and use it to send the message. Likewise, a web service written in Java could use the same (or another) implementation of the JAXM API to receive the message, to disassemble it, and to acknowledge its receipt.

Messaging Semantics

In addition to a language implementation, a SOAP provider can offer services that relate to message delivery. These could include reliability, persistence, security, and administrative control. These services will be provided for SOAP messaging by Message Queue in future releases.


Because SOAP providers must all construct and deconstruct messages as defined by the SOAP specification, clients and services using SOAP are interoperable. That is, as shown in Figure 6-2, the client and the service doing SOAP messaging do not need to be written in the same language nor do they need to use the same SOAP provider. It is only the packaging of the message that must be standard.

Figure 6-2  SOAP Interoperability

Diagram showing how a client using one SOAP provider sends a message to a client using another SOAP provider.

In order for a JAXM client or service to interoperate with a service or client using a different provider, the parties must agree on two things:

Profiles provide additional processing information, as described next.

The Profiles Layer

The final, profile, layer of SOAP messaging governs messaging semantics between business partners who use SOAP messaging with SOAP providers. A profile is an industry standard, such as “ebxml”, which defines additional rules for message processing. A provider can add profile information to the header of a message when its message factory creates the message. (The SOAP message header is the primary means of SOAP messaging extensibility.) Support for the ebxml profile will be added in future releases of Message Queue.

The SOAP Message

Having surveyed the SOAP messaging layers, let’s examine the SOAP message itself. Although the work of rendering a SOAP message in XML is taken care of by the JAXM libraries, you must still understand its structure in order to make the JAXM calls in the right order.

A SOAP message is an XML document that consists of a SOAP envelope, an optional SOAP header, and a SOAP body. The SOAP message header contains information that allows the message to be routed through one or more intermediate nodes before it reaches its final destination.

A SOAP message may also contain an attachment, which does not have to be in XML. For more information, see SOAP Packaging Models next.

A SOAP message is constructed like a nested matrioshka doll. When you use JAXM to assemble or disassemble a message, you need to make the API calls in the appropriate order to get to the message part that interests you. For example, in order to add content to the message, you need to get to the body part of the message. To do this you need to work through the nested layers: SOAP part, SOAP envelope, SOAP body, until you get to the SOAP body element that you will use to specify your data. For more information, see The SOAP Message Object.

SOAP Packaging Models

The SOAP specification describes two models of SOAP messages: one that is encoded entirely in XML and one that allows the sender to add an attachment containing non-XML data. You should look over the following two figures and note the parts of the SOAP message for each model. When you use JAXM to define SOAP messages and their parts, it will be helpful for you to be familiar with this information.

Figure 6-3 shows the SOAP model without attachments. This package includes a SOAP envelope, a header, and a body. The header is optional.

Figure 6-3  SOAP Message Without Attachments

Diagram showing body and header enclosed in an envelope, which is in a SOAP message package, which is in a communication protocol envelope.

When you construct a SOAP message using JAXM, you do not have to specify which model you’re following. If you add an attachment, a message like that shown in Figure 6-4 is constructed; if you don’t, a message like that shown in Figure 6-3 is constructed.

Figure 6-4 shows a SOAP Message with attachments. The attachment part can contain any kind of content: image files, plain text, and so on. The sender of a message can choose whether to create a SOAP message with attachments. The message receiver can also choose whether to consume an attachment.

A message that contains one or more attachments is enclosed in a MIME envelope that contains all the parts of the message. In JAXM, the MIME envelope is automatically produced whenever the client creates an attachment part. If you add an attachment to a message, you are responsible for specifying (in the MIME header) the type of data in the attachment.

Figure 6-4  SOAP Message with Attachments

Diagram showing SOAP part enclosed in a MIME envelope together with the attachment part, which contains the SOAP attachment.

SOAP Messaging in JAVA

The SOAP specification does not provide a programming model or even an API for the construction of SOAP messages; it simply defines the XML schema to be used in packaging a SOAP message.

JAXM is an application programming interface that can be implemented to support a programming model for SOAP messaging and to furnish Java objects that application or tool writers can use to construct, send, receive, and examine SOAP messages. JAXM defines two packages:

This chapter focuses on the javax.xml.soap package and how you use the objects and methods it defines

It also explains how you can use the JMS API and Message Queue to send and receive JMS messages that carry SOAP message payloads.

The SOAP Message Object

A SOAP Message Object is a tree of objects as shown in Figure 6-5. The classes or interfaces from which these objects are derived are all defined in the javax.xml.soap package.

Figure 6-5  SOAP Message Object

Diagram showing hierarchy of objects that make up a SOAP message object. Long description follows figure.[D]

As shown in the figure, the SOAPMessage object is a collection of objects divided in two parts: a SOAP part and an attachment part. The main thing to remember is that the attachment part can contain non-xml data.

The SOAP part of the message contains an envelope that contains a body (which can contain data or fault information) and an optional header. When you use JAXM to create a SOAP message, the SOAP part, envelope, and body are created for you: you need only create the body elements. To do that you need to get to the parent of the body element, the SOAP body.

In order to reach any object in the SOAPMessage tree, you must traverse the tree starting from the root, as shown in the following lines of code. For example, assuming the SOAPMessage is MyMsg, here are the calls you would have to make in order to get the SOAP body:

SOAPPart MyPart = MyMsg.getSOAPPart();

SOAPEnvelope MyEnv = MyPart.getEnvelope();

SOAPBody MyBody = envelope.getBody();

At this point, you can create a name for a body element (as described in Namespaces) and add the body element to the SOAPMessage.

For example, the following code line creates a name (a representation of an XML tag) for a body element:

Name bodyName = envelope.createName("Temperature");

The next code line adds the body element to the body:

SOAPBodyElement myTemp = MyBody.addBodyElement(bodyName);

Finally, this code line defines some data for the body element bodyName:


Inherited Methods

The elements of a SOAP message form a tree. Each node in that tree implements the Node interface and, starting at the envelope level, each node implements the SOAPElement interface as well. The resulting shared methods are described in Table 6-1.

Table 6-1  Inherited Methods 

Inherited From

Method Name



addAttribute(Name, String)

Add an attribute with the specified Name object and string value.



addChildElement(String, String)

addChildElement(String, String, String)

Create a new SOAPElement object, initialized with the given Name object, and add the new element.

(Use the Envelope.createName method to create a Name object.)


addNameSpaceDeclaration (String, String)

Add a namespace declaration with the specified prefix and URI.



Create a new Text object initialized with the given String and add it to this SOAPElement object.



Return an iterator over all the attribute names in this object.



Return the value of the specified attribute.



Return an iterator over all the immediate content of this element.



Return an iterator over all the child elements with the specified name.



Return the name of this object.



Return the encoding style for this object.



Return an iterator of namespace prefixes.



Return the URI of the namespace with the given prefix.



Remove the specified attribute.



Remove the namespace declaration that corresponds to the specified prefix.



Set the encoding style for this object to that specified by String.



Remove this Node object from the tree.



Return the parent element of this Node object.



Return the value of the immediate child of this Node object if a child exists and its value is text.



Notify the implementation that his Node object is no longer being used and is free for reuse.



Set the parent of this object to that specified by the SOAPElement parameter.


An XML namespace is a means of qualifying element and attribute names to disambiguate them from other names in the same document. This section provides a brief description of XML namespaces and how they are used in SOAP. For complete information, see

An explicit XML namespace declaration takes the following form


xmlns:prefix ="URI">

The declaration defines prefix as an alias for the specified URI. In the element myElement, you can use prefix with any element or attribute to specify that the element or attribute name belongs to the namespace specified by the URI.

The following is an example of a namespace declaration:

<SOAP-ENV:Envelope xmlns:SOAP-ENV=""

This declaration defines SOAP_ENV as an alias for the namespace

After defining the alias, you can use it as a prefix to any attribute or element in the Envelope element. In Code Example 6-1, the elements <Envelope> and <Body> and the attribute encodingStyle all belong to the SOAP namespace specified by the URI "".

Code Example 6-1  Explicit Namespace Declarations 








     The text of the header









Note that the URI that defines the namespace does not have to point to an actual location; its purpose is to disambiguate attribute and element names.

Pre-defined SOAP Namespaces

SOAP defines two namespaces:

When you use JAXM to construct or consume messages, you are responsible for setting or processing namespaces correctly and for discarding messages that have incorrect namespaces.

Using Namespaces when Creating a SOAP Name

When you create the body elements or header elements of a SOAP message, you must use the Name object to specify a well-formed name for the element. You obtain a Name object by calling the method SOAPEnvelope.createName.

When you call this method, you can pass a local name as a parameter or you can specify a local name, prefix, and URI. For example, the following line of code defines a name object bodyName.

Name bodyName = MyEnvelope.createName("TradePrice",

This would be equivalent to the namespace declaration:

<GetLTP:TradePrice xmlns:GetLTP= "">

The following code shows how you create a name and associate it with a SOAPBody element. Note the use and placement of the createName method.

SoapBody body = envelope.getBody();//get body from envelope

Name bodyName = envelope.createName("TradePrice", "GetLTP",

SOAPBodyElement gltp = body.addBodyElement(bodyName);

Parsing Name Objects

For any given Name object, you can use the following Name methods to parse the name:

Destination, Message Factory, and Connection Objects

SOAP messaging occurs when a SOAP message, produced by a message factory, is sent to an endpoint via a connection.

The following three sections describe endpoint, message factory, and connection objects in greater detail.


An endpoint identifies the final destination of a message. An endpoint is defined either by the Endpoint class (if you use a provider) or by the URLEndpoint class (if you don’t use a provider).)

Constructing an Endpoint

You can initialize an endpoint either by calling its constructor or by looking it up in a naming service. For information about creating administered objects for endpoints, see Using JAXM Administered Objects.

The following code uses a constructor to create a URLEndpoint:

myEndpoint = new URLEndpoint("http://somehost/myServlet");

Using the Endpoint to Address a Message

If you are using a provider, the Message Factory creating the message includes the endpoint specification in the message header.

If you do not use a provider, you can specify the endpoint as a parameter to the method, which you use to send a SOAP message.

Sending a Message to Multiple Endpoints

If you are using an administered object to define an endpoint, note that it is possible to associate that administered object with multiple URLs--each URL, is capable of processing incoming SOAP messages. The code sample below associates the endpoint whose lookup name is myEndpoint with two URLs: http://www.myServlet1/ and http://www.myServlet2/.

imqobjmgr add

  -t e

  -l "cn=myEndpoint"

  -o "imqSOAPEndpointList=http://www.myServlet1/

This syntax allows you to use a SOAP connection to publish a SOAP message to multiple endpoints. For additional information about the endpoint administered object, see Using JAXM Administered Objects.

Message Factory

You use a Message Factory to create a SOAP message.


To send a SOAP message using JAXM, you must obtain either a SOAPConnection or a ProviderConnection. You can also transport a SOAP message using Message Queue; for more information, see Integrating SOAP and Message Queue.

SOAP Connection

A SOAPConnection allows you to send messages directly to a remote party. You can obtain a SOAPConnection object simply by calling the static method SOAPConnectionFactory.newInstance(). Neither reliability nor security are guaranteed over this type of connection.

Provider Connection

A ProviderConnection, which you get from a ProviderConnectionFactory, creates a connection to a particular messaging provider. When you send a SOAP message using a provider, the message is forwarded to the provider, and then the provider is responsible for delivery to its final destination. The provider guarantees reliable, secure messaging. (Message Queue does not currently offer SOAP provider support.)

Using JAXM Administered Objects

Administered objects are objects that encapsulate provider-specific configuration and naming information. For endpoint objects, you have the choice either to instantiate such an object or to create an administered object and associate it with an endpoint object instance.

The main benefit of creating an endpoint through a JNDI lookup is to isolate endpoint URLs from the code, allowing the application to switch the destination without recompiling the code. A secondary benefit is provider independence.

Creating an administered object for a SOAP element is the same as creating an administered object in Message Queue: you use the Object Manager (imqobjmgr) utility to specify the lookup name of the object, its attributes, and its type.

Table 6-2 lists and describes the attributes and other information that you need to specify when you create an endpoint administered object. Remember to specify all attributes as strings.

Table 6-2  SOAP Administered Object Information 



-o “attribute=val

Use this option to specify three possible attributes for an endpoint administered object:

  • A URL list
  • -o “imqSOAPEndpointList = “url1 url2 ....urln

    The list may contain one or more space-separated URLs. If it contains more than one, the message is broadcast to all the URLs. Each URL should be associated with a servlet that can receive and process a SOAP message.

  • A name
  • -o “imqEndpointName=SomeName

    If you don’t specify a name, the name Untitled_Endpoint_Object is used by default.

  • A description
  • -o "imqEndpointDescription=my endpoints for broadcast"

    If you don’t specify a description, the default value "A description for the endpoint object" is supplied by default.

-l “cn=lookupName

Use this option to specify the lookup name of the endpoint.

-t type

Use this option to specify the object’s type. This is always e for an endpoint.

-i filename

Use this option to specify the name of an input file containing imqobjmgr commands. Such an input file is typically used to specify object store attributes.

-j “attribute=val

Use this option to specify object store attributes. You can also specify these in an input file. Use the -i option to specify the input file.

Code Example 6-2 shows how you use the imqobjmgr command to create an administered object for an endpoint and add it to an object store. The -i option specifies the name of an input file that defines object store attributes (-j option).

Code Example 6-2  Adding an Endpoint Administered Object 

imqobjmgr add

  -t e

  -l "cn=myEndpoint"

  -o "imqSOAPEndpointList=http://www.myServlet/

  -o "imqEndpointName=MyBroadcastEndpoint"

  -i MyObjStoreAttrs

Having created the administered object and added it to an object store, you can now use it when you want to use an endpoint in your JAXM application. In Code Example 6-3, you first create an initial context for the JNDI lookup and then you look up the desired object.

Code Example 6-3  Looking up an Endpoint Administered Object 

Hashtable env = new Hashtable();

env.put (Context.PROVIDER_URL,

Context ctx = new InitialContext(env);

Endpoint mySOAPEndpoint = (Endpoint)

You can also list, delete, and update administered objects. For additional information, please see the Message Queue Administration Guide.

SOAP Messaging Models and Examples

This section explains how you use JAXM to send and receive a SOAP message. It is also possible to construct a SOAP message using JAXM and to send it as the payload of a JMS message. For information, see Integrating SOAP and Message Queue.

JAXM supplies two models that you can use to do SOAP messaging: one uses the SOAPConnection object and the other uses the ProviderConnection object. Message Queue does not currently support the ProviderConnection object.

SOAP Messaging Programming Models

This section provides a brief summary of the programming models used in SOAP messaging using JAXM.

A SOAP message is sent to an endpoint by way of a connection. There are two types of connections: point-to-point connections (implemented by the SOAPConnection class) and provider connections (implemented by the ProviderConnection class).

Point-to-Point Connections

You use point-to-point connections to establish a request-reply messaging model. The request-reply model is illustrated in Figure 6-6.

Figure 6-6  Request-Reply Messaging

Diagram showing the client sending a message to an endpoint that receives the message, processes it, and then returns to the sender.

Using this model, the client does the following:

It is assumed that the client will ignore the SOAPMessage object returned by the call method because the only reason this object is returned is to unblock the client.

The JAXM service listening for a request-reply message uses a ReqRespListener object to receive messages.

For a detailed example of a client that does point-to-point messaging, see Writing a SOAP Client.

Provider Connections

You use a provider connection to implement one-way messaging. Figure 6-7 illustrates the one-way messaging model.

Figure 6-7  One-Way Messaging

Diagram showing SOAP message being sent by way of a messaging provider to a SOAP endpoint.

As opposed to the point-to-point model, the final destination for the message is written into the message header by the provider. (When the administrator configures the messaging provider, she can supply a list of Endpoint objects. When a client uses the provider to send messages, the provider sends the messages only to those parties represented by Endpoint objects in its messaging provider’s list.)

A message sent by means of a provider connection is always routed through an intermediate destination in the provider before it is forwarded to its final destination. The provider is also responsible for the reliability of the transmission and the privacy of the message.

Using this model, the client does the following:

The JAXM service listening for a one way message uses a OnewayListener object to receive messages asynchronously.

Working with Attachments

If a message contains any data that is not XML, you must add it to the message as an attachment. A message can have any number of attachment parts. Each attachment part can contain anything from plain text to image files.

To create an attachment, you must create a URL object that specifies the location of the file that you want to attach to the SOAP message. You must also create a data handler that will be used to interpret the data in the attachment. Finally, you need to add the attachment to the SOAP message.

To create and add an attachment part to the message, you need to use the JavaBeans Activation Framework (JAF) API. This API allows you to determine the type of an arbitrary piece of data, encapsulate access to it, discover the operations available on it, and activate a bean that can perform these operations. You must include the activation.jar library in your application code in order to work with the JavaBeans Activation Framework.

    To Create and Add an Attachment
  1. Create a URL object and initialize it to contain the location of the file that you want to attach to the SOAP message.
  2. URL url = new URL("");

  3. Create a data handler and initialize it with a default handler, passing the URL as the location of the data source for the handler.
  4. DataHandler dh = new DataHandler(url);

  5. Create an attachment part that is initialized with the data handler containing the URL for the image.
  6. AttachmentPart ap1 = message.createAttachmentPart(dh);

  7. Add the attachment part to the SOAP message.
  8. myMessage.addAttachmentPart(ap1);

After creating the attachment and adding it to the message, you can send the message in the usual way.

If you are using JMS to send the message, you can use the SOAPMessageIntoJMSMessage conversion utility to convert a SOAP message that has an attachment into a JMS message that you can send to a JMS queue or topic using Message Queue.

Exception and Fault Handling

A SOAP application can use two error reporting mechanisms: SOAP exceptions and SOAP faults:

Writing a SOAP Client

The following steps show the calls you have to make to write a SOAP client for point-to-point messaging.

  1. Get an instance of a SOAPConnectionFactory:
  2. SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance();

  3. Get a SOAP connection from the SOAPConnectionFactory object:
  4. SOAPConnection myCon = myFct.createConnection();

    The myCon object that is returned will be used to send the message.

  5. Get a MessageFactory object to create a message:
  6. MessageFactory myMsgFct = MessageFactory.newInstance();

  7. Use the message factory to create a message:
  8. SOAPMessage message = myMsgFct.createMessage();

    The message that is created has all the parts that are shown in Figure 6-8.

    Figure 6-8  SOAP Message Parts
    Diagram showing SOAP message with pre-initialized objects: part, envelope, header, and body.

    At this point, the message has no content. To add content to the message, you need to create a SOAP body element, define a name and content for it, and then add it to the SOAP body.

    Remember that to access any part of the message, you need to traverse the tree, calling a get method on the parent element to obtain the child. For example, to reach the SOAP body, you start by getting the SOAP part and SOAP envelope:

    SOAPPart mySPart = message.getSOAPPart();

    SOAPEnvelope myEnvp = mySPart.getEnvelope();

  9. Now, you can get the body element from the myEnvp object:
  10. SOAPBody body = myEnvp.getBody();

    The children that you will add to the body element define the content of the message. (You can add content to the SOAP header in the same way.)

  11. When you add an element to a SOAP body (or header), you must first create a name for it by calling the envelope.createName method. This method returns a Name object, which you must then pass as a parameter to the method that creates the body element (or the header element).
  12. Name bodyName = envelope.createName("GetLastTradePrice", "m",

    SOAPBodyElement gltp = body.addBodyElement(bodyName);

  13. Now create another body element to add to the gltp element:
  14. Name myContent = envelope.createName("symbol");

    SOAPElement mySymbol = gltp.addChildElement(myContent);

  15. And now you can define data for the body element mySymbol:
  16. mySymbol.addTextNode("SUNW");

    The resulting SOAP message object is equivalent to this XML scheme:

    <SOAP-ENV: Envelope



          <m:GetLastTradePrice xmlns:m="">




    </SOAP-ENV: Envelope>

  17. Every time you send a message or write to it, the message is automatically saved. However if you change a message you have received or one that you have already sent, this would be the point when you would need to update the message by saving all your changes. For example:
  18. message.saveChanges();

  19. Before you send the message, you must create a URLEndpoint object with the URL of the endpoint to which the message is to be sent. (If you use a profile that adds addressing information to the message header, you do not need to do this.)
  20. URLEndpoint endPt = new

  21. Now, you can send the message:
  22. SOAPMessage reply =, endPt);

    The reply message (reply) is received on the same connection.

  23. Finally, you need to close the SOAPConnection object when it is no longer needed:
  24. myCon.close();

Writing a SOAP Service

A SOAP service represents the final recipient of a SOAP message and should currently be implemented as a servlet. You can write your own servlet or you can extend the JAXMServlet class, which is furnished in the soap.messaging package for your convenience. This section describes the task of writing a SOAP service based on the JAXMServlet class.

Your servlet must implement either the ReqRespListener or OneWayListener interfaces. The difference between these two is that ReqRespListener requires that you return a reply.

Using either of these interfaces, you must implement a method called onMessage(SOAPMsg). JAXMservlet will call onMessage after receiving a message using the HTTP POST method, which saves you the work of implementing your own doPost() method to convert the incoming message into a SOAP message.

Code Example 6-4 shows the basic structure of a SOAP service that uses the JAXM servlet utility class.

Code Example 6-4  Skeleton Message Consumer 

public class MyServlet extends JAXMServlet implements


  public SOAPMessage onMessage(SOAP Message msg)

  { //Process message here



Code Example 6-5 shows a simple ping message service:

Code Example 6-5  A Simple Ping Message Service 

public class SOAPEchoServlet extends JAXMServlet
                    implements ReqRespListener{

  public SOAPMessage onMessage(SOAPMessage mySoapMessage) {

    return mySoapMessage



Table 6-3 describes the methods that the JAXM servlet uses. If you were to write your own servlet, you would need to provide methods that performed similar work. In extending JAXMServlet, you may need to override the Init method and the SetMessageFactory method; you must implement the onMessage method.

Table 6-3  JAXMServlet Methods 



void init

Passes the ServletConfig object to its parent’s constructor and creates a default messageFactory object.

If you want incoming messages to be constructed according to a certain profile, you must call the SetMessageFactory method and specify the profile it should use in constructing SOAP messages.

void doPost (HTTPRequest, HTTPResponse

Gets the body of the HTTP request and creates a SOAP message according to the default or specified MessageFactory profile.

Calls the onMessage() method of an appropriate listener, passing the SOAP message as a parameter.

It is recommended that you do not override this method.

void setMessageFactory (MessageFactory)

Sets the MessageFactory object. This is the object used to create the SOAP message that is passed to the onMessage method.

MimeHeaders getHeaders (HTTPRequest)

Returns a MimeHeaders object that contains the headers in the given HTTPRequest object.

void putHeaders (mimeHeaders, HTTPresponse)

Sets the given HTTPResponse object with the headers in the given MimeHeaders object


User-defined method that is called by the servlet when the SOAP message is received. Normally this method needs to disassemble the SOAP message passed to it and to send a reply back to the client (if the servlet implements the ReqRespListener interface.)

Disassembling Messages

The onMessage method needs to disassemble the SOAP message that is passed to it by the servlet and process its contents in an appropriate manner. If there are problems in the processing of the message, the service needs to create a SOAP fault object and send it back to the client as described in Handling SOAP Faults.

Processing the SOAP message may involve working with the headers as well as locating the body elements and dealing with their contents. The following code sample shows how you might disassemble a SOAP message in the body of your onMessage method. Basically, you need to use a Document Object Model (DOM) API to parse through the SOAP message.

See for more information about the DOM API.

Code Example 6-6  Processing a SOAP Message 


  SOAPEnvelope env = reply.getSOAPPart().getEnvelope();

  SOAPBody sb = env.getBody();

  // create Name object for XElement that we are searching for

  Name ElName = env.createName("XElement");

  //Get child elements with the name XElement

  Iterator it = sb.getChildElements( ElName );

  //Get the first matched child element.
  //We know there is only one.

  SOAPBodyElement sbe = (SOAPBodyElement);

  //Get the value for XElement

  MyValue =   sbe.getValue();


Handling Attachments

A SOAP message may have attachments. For sample code that shows you how to create and add an attachment, see Code Example 6-7. For sample code that shows you how to receive and process an attachment, see Code Example 6-8.

In handling attachments, you will need to use the Java Activation Framework API. See for more information.

Replying to Messages

In replying to messages, you are simply taking on the client role, now from the server side.

Handling SOAP Faults

Server-side code must use a SOAP fault object to handle errors that occur on the server side when unmarshalling the request, processing the message, or marshalling the response. The SOAPFault interface extends the SOAPBodyElement interface.

SOAP messages have a specific element and format for error reporting on the server side: a SOAP message body can include a SOAP fault element to report errors that happen during the processing of a request. Created on the server side and sent from the server back to the client, the SOAP message containing the SOAPFault object reports any unexpected behavior to the originator of the message.

Within a SOAP message object, the SOAP fault object is a child of the SOAP body, as shown in Figure 6-9. Detail and detail entry objects are only needed if one needs to report that the body of the received message was malformed or contained inappropriate data. In such a case, the detail entry object is used to describe the malformed data.

Figure 6-9  SOAP Fault Element

Diagram showing hierarchy from top to bottom for a message containing fault information: SOAP part, envelope, body, fault, detail, and detail entry.

The SOAP Fault element defines the following four sub-elements:

Predefined Fault Codes

The SOAP specification lists four predefined faultcode values. The namespace identifier for these is

Table 6-4  SOAP Faultcode Values 

Faultcode Name



The processing party found an invalid namespace for the SOAP envelope element; that is, the namespace of the SOAP envelope element was not


An immediate child element of the SOAP Header element was either not understood or not appropriately processed by the recipient. This element’s mustUnderstand attribute was set to 1 (true).


The message was incorrectly formed or did not contain the appropriate information. For example, the message did not have the proper authentication or payment information. The client should interpret this code to mean that the message must be changed before it is sent again.

If this is the code returned, the SOAPFault object should probably include a detailEntry object that provides additional information about the malformed message.


The message could not be processed for reasons that are not connected with its content. For example, one of the message handlers could not communicate with another message handler that was upstream and did not respond. Or, the database that the server needed to access is down. The client should interpret this error to mean that the transmission could succeed at a later point in time.

These standard fault codes represent classes of faults. You can extend these by appending a period to the code and adding an additional name. For example: you could define a Server.OutOfMemory code, a Server.Down code, etc.

Defining a SOAP Fault

Using JAXM you can specify the value for faultcode, faultstring, and faultactor using methods of the SOAPFault object. The following code creates a SOAP fault object and sets the faultcode, faultstring, and faultactor attributes:

SOAPFault fault;

reply = factory.createMessage();

envp = reply.getSOAPPart().getEnvelope(true);

someBody = envp.getBody();

fault = someBody.addFault():


fault.setFaultString("Some Server Error");



The server can return this object in its reply to an incoming SOAP message in case of a server error.

The next code sample shows how to define a detail and detail entry object. Note that you must create a name for the detail entry object.

SOAPFault fault = someBody.addFault();



fault.setFaultString ("Unkown error");

Detail myDetail = fault.addDetail();

detail.addDetailEntry(envelope.createName("125detail", "m",
    "Someuri")).addTextNode("the message cannot contain
    the string //");


Integrating SOAP and Message Queue

This section explains how you can send, receive, and process a JMS message that contains a SOAP payload.

Message Queue provides a utility to help you send and receive SOAP messages using the JMS API. With the support it provides, you can convert a SOAP message into a JMS message and take advantage of the reliable messaging service offered by Message Queue. You can then convert the message back into a SOAP message on the receiving side and use the JAXM API to process it.

To send, receive, and process a JMS message that contains a SOAP payload, you must do the following:

The following sections offer several use cases and code examples to illustrate this process.

Example 1: Deferring SOAP Processing

In the first example, illustrated in Figure 6-10, an incoming SOAP message is received by a servlet. After receiving the SOAP message, the servlet MyServlet uses the MessageTransformer utility to transform the message into a JMS message, and (reliably) forwards it to an application that receives it, turns it back into a SOAP message, and processes the contents of the SOAP message.

For information on how the servlet receives the SOAP message, see Writing a SOAP Service.

Figure 6-10  Deferring SOAP Processing

Diagram showing deferred SOAP processing. Figure content is described in text.

    To Transform the SOAP Message into a JMS Message and Send the JMS Message
  1. Instantiate a ConnectionFactory object and set its attribute values, for example:
  2. QueueConnectionFactory myQConnFact =
        new com.sun.messaging.QueueConnectionFactory();

  3. Use the ConnectionFactory object to create a Connection object.
  4. QueueConnection myQConn =

  5. Use the Connection object to create a Session object.
  6. QueueSession myQSess = myQConn.createQueueSession(false,

  7. Instantiate a Message Queue Destination administered object corresponding to a physical destination in the Message Queue message service. In this example, the administered object is mySOAPQueue and the physical destination to which it refers is myPSOAPQ.
  8. Queue mySOAPQueue = new com.sun.messaging.Queue("myPSOAPQ");

  9. Use the MessageTransformer utility, as shown, to transform the SOAP message into a JMS message. For example, given a SOAP message named MySOAPMsg,
  10. Message MyJMS = MessageTransformer.SOAPMessageIntoJMSMessage
                      (MySOAPMsg, MyQSess);

  11. Create a QueueSender message producer.
  12. This message producer, associated with mySOAPQueue, is used to send messages to the queue destination named myPSOAPQ.

    QueueSender myQueueSender = myQSess.createSender(mySOAPQueue);

  13. Send a message to the queue.
  14. myQueueSender.send(myJMS);

    To Receive the JMS Message, Transform it into a SOAP Message, and Process It
  1. Instantiate a ConnectionFactory object and set its attribute values.
  2. QueueConnectioFactory myQConnFact = new

  3. Use the ConnectionFactory object to create a Connection object.
  4. QueueConnection myQConn = myQConnFact.createQueueConnection();

  5. Use the Connection object to create one or more Session objects.
  6. QueueSession myRQSess = myQConn.createQueueSession(false,

  7. Instantiate a Destination object and set its name attribute.
  8. Queue myRQueue = new com.sun.messaging.Queue("mySOAPQ");

  9. Use a Session object and a Destination object to create any needed MessageConsumer objects.
  10. QueueReceiver myQueueReceiver =

  11. If needed, instantiate a MessageListener object and register it with a MessageConsumer object.
  12. Start the QueueConnection you created in Step 2. Messages for consumption by a client can only be delivered over a connection that has been started.
  13. myQConn.start();

  14. Receive a message from the queue
  15. The code below is an example of a synchronous consumption of messages.

    Message myJMS = myQueueReceiver.receive();

  16. Use the Message Transformer to convert the JMS message back to a SOAP message.
  17. SOAPMessage MySoap =
          (myJMS, MyMsgFactory);

    If you specify null for the MessageFactory argument, the default Message Factory is used to construct the SOAP Message.

  18. Disassemble the SOAP message in preparation for further processing. See The SOAP Message Object for information.

Example 2: Publishing SOAP Messages

In the next example, illustrated in Figure 6-11, an incoming SOAP message is received by a servlet. The servlet packages the SOAP message as a JMS message and (reliably) forwards it to a topic. Each application that subscribes to this topic, receives the JMS message, turns it back into a SOAP message, and processes its contents.

Figure 6-11  Publishing a SOAP Message

Diagram showing how a SOAP message is transformed into a JMS message, published to listeners, and reconverted into SOAP.

The code that accomplishes this is exactly the same as in the previous example, except that instead of sending the JMS message to a queue, you send it to a topic. For an example of publishing a SOAP message using Message Queue, see Code Example 6-7.

Code Samples

This section includes and describes two code samples: one that sends a JMS message with a SOAP payload, and another that receives the JMS/SOAP message and processes the SOAP message.

Code Example 6-7 illustrates the use of the JMS API, the JAXM API, and the JAF API to send a SOAP message with attachments as the payload to a JMS message. The code shown for the SendSOAPMessageWithJMS includes the following methods:

Code Example 6-8 illustrates the use of the JMS API, the JAXM API, and the DOM API to receive a SOAP message with attachments as the payload to a JMS message. The code shown for the ReceiveSOAPMessageWithJMS includes the following methods:

Previous      Contents      Index      Next     

Copyright 2004 Sun Microsystems, Inc. All rights reserved.