background image

Parts of a Message

<< Creating and Sending a Simple Message | Accessing Elements of a Message >>
<< Creating and Sending a Simple Message | Accessing Elements of a Message >>
C
REATING AND
S
ENDING A
S
IMPLE
M
ESSAGE
125
ing 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 for
SOAPConnectionFactory
, the
newInstance
method for
MessageFactory
is static, so you invoke it by calling
MessageFactory.newInstance
.
If you specify no arguments to the
newInstance
method, it creates a message
factory for SOAP 1.1 messages. To create a message factory that allows you to
create and process SOAP 1.2 messages, use the following method call:
MessageFactory factory =
MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
To create a message factory that can create either SOAP 1.1 or SOAP 1.2 mes-
sages, use the following method call:
MessageFactory factory =
MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL
);
This kind of factory enables you to process an incoming message that might be
of either type.
Parts of a Message
A
SOAPMessage
object is required to have certain elements, and, as stated previ-
ously, the SAAJ API simplifies things for you by returning a new
SOAPMessage
object that already contains these elements. When you call
createMessage
with
no arguments, the message that is created automatically has the following:
I. A
SOAPPart
object that contains
A. A
SOAPEnvelope
object that contains
1. An empty
SOAPHeader
object
2. An empty
SOAPBody
object
The
SOAPHeader
object is optional and can be deleted if it is not needed. How-
ever, if there is one, it must precede the
SOAPBody
object. The
SOAPBody
object
can hold either the content of the message or a fault message that contains status