background image

SOAPMessage Object

<< Retrieving Fault Information | Code Examples >>
<< Retrieving Fault Information | Code Examples >>

SOAPMessage Object

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.getFaultSubcodes.
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 null. If it is not, the code prints the values of the DetailEntry objects
as long as there are any.
Detail newDetail = newFault.getDetail();
if (newDetail != null) {
Iterator entries = newDetail.getDetailEntries();
while ( entries.hasNext() ) {
DetailEntry newEntry = (DetailEntry)entries.next();
String value = newEntry.getValue();
System.out.println(
"
Detail entry =
" + value);
}
}
SAAJ Tutorial
The Java EE 5 Tutorial · September 2007
612