background image

getSOAPBody() Method

<< Retrieving Fault Information | Code Examples >>
<< Retrieving Fault Information | Code Examples >>
150
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
The following code fragment shows what you might write to retrieve fault infor-
mation from a message you received. In the code fragment,
newMessage
is the
SOAPMessage
object that has been sent to you. Because a
SOAPFault
object must
be part of the
SOAPBody
object, the first step is to access the
SOAPBody
object.
Then the code tests to see whether the
SOAPBody
object contains a
SOAPFault
object. If it does, the code retrieves the
SOAPFault
object and uses it to retrieve
its contents. The convenience methods
getFaultCode
,
getFaultString
, and
getFaultActor
make retrieving the values very easy.
SOAPBody body = newMessage.getSOAPBody();
if ( body.hasFault() ) {
SOAPFault newFault = body.getFault();
QName code = newFault.getFaultCodeAsQName();
String string = newFault.getFaultString();
String actor = newFault.getFaultActor();
To retrieve subcodes from a SOAP 1.2 fault, call the method
newFault.get-
FaultSubcodes
.
Next the code prints the values it has just retrieved. Not all messages are required
to have a fault actor, so the code tests to see whether there is one. Testing
whether the variable
actor
is
null
works because the method
getFaultActor
returns
null
if a fault actor has not been set.
System.out.println("SOAP fault contains: ");
System.out.println(" Fault code = " +
code.toString());
System.out.println("
Local name = " + code.getLocalPart());
System.out.println(" Namespace prefix = " +
code.getPrefix() + ", bound to " +
code.getNamespaceURI());
System.out.println(" Fault string = " + string);
if ( actor != null ) {
System.out.println(" Fault actor = " + actor);
}
The final task is to retrieve the
Detail
object and get its
DetailEntry
objects.
The code uses the
SOAPFault
object
newFault
to retrieve the
Detail
object
newDetail
, and then it uses
newDetail
to call the method
getDetailEntries
.
This method returns the
java.util.Iterator
object
entries
, which contains
all the
DetailEntry
objects in
newDetail
. Not all
SOAPFault
objects are
required to have a
Detail
object, so the code tests to see whether
newDetail
is