background image

Creating and Populating a SOAPFault Object

<< Overview of SOAP Faults | The SOAP 1.2 code >>
<< Overview of SOAP Faults | The SOAP 1.2 code >>
U
SING
SOAP F
AULTS
147
tion.) Table 5­1 on page 149 lists and describes the default fault code local
names defined in the specifications.
A SOAP 1.2 fault code can optionally have a hierarchy of one or more
subcodes.
· A fault string: Always required. A human-readable explanation of the
fault.
· A fault actor: Required if the
SOAPHeader
object contains one or more
actor
attributes; optional if no actors are specified, meaning that the only
actor is the ultimate destination. The fault actor, which is specified as a
URI, identifies who caused the fault. For an explanation of what an actor
is, see The Actor Attribute, page 141.
· A Detail object: Required if the fault is an error related to the
SOAPBody
object. If, for example, the fault code is
Client
, indicating that the mes-
sage could not be processed because of a problem in the
SOAPBody
object,
the
SOAPFault
object must contain a
Detail
object that gives details about
the problem. If a
SOAPFault
object does not contain a
Detail
object, it can
be assumed that the
SOAPBody
object was processed successfully.
Creating and Populating a SOAPFault Object
You have seen how to add content to a
SOAPBody
object; this section walks you
through adding a
SOAPFault
object to a
SOAPBody
object and then adding its
constituent parts.
As with adding content, the first step is to access the
SOAPBody
object.
SOAPBody body = message.getSOAPBody();
With the
SOAPBody
object
body
in hand, you can use it to create a
SOAPFault
object. The following line of code creates a
SOAPFault
object and adds it to
body
.
SOAPFault fault = body.addFault();
The
SOAPFault
interface provides convenience methods that create an element,
add the new element to the
SOAPFault
object, and add a text node, all in one
operation. For example, in the following lines of SOAP 1.1 code, the method
setFaultCode
creates a
faultcode
element, adds it to
fault
, and adds a
Text