Java Reference
In-Depth Information
If
hasFault()
returns false, the message's body provides the response value that
must be extracted. The following excerpt from the
toRoman()
method handles this
extraction task:
body = response.getSOAPBody();
bodyName = new QName("urn:Roman-IRoman", "IntToRomanRe-
sponse", "NS1");
Iterator iter = body.getChildElements(bodyName);
bodyElement = (SOAPBodyElement) iter.next();
iter = bodyElement.getChildElements(new QName("return"));
return ((SOAPElement) iter.next()).getValue();
Aftercalling
SOAPMessage
's
SOAPBody getSOAPBody()
conveniencemeth-
od to return the
SOAPBody
object describing the SOAP message's body, the excerpt
creates a
QName
object that identifies the qualified name for the
IntToRomanRe-
sponse
element. This object is then passed to
SOAPBody
's inherited
Iterator
getChildElements(QName qname)
method to return a
java.util.Iterator
instance that will be used to iterate over all
IntToRo-
manResponse
child elements of the
Body
element.
Becausethereisonlyonesuchchildelement,onlyasinglecallto
next()
ismadeto
returnthiselement,asa
SOAPBodyElement
instance.Thisinstanceisusedtoinvoke
getChildElements()
, but this time with the qualified name of the
return
ele-
ment.Thereturnediterator's
next()
methodiscalledtoextractthe
return
element
asa
SOAPElement
instance,and
getValue()
isinvokedonthisinstancetoreturn
the value of the
return
element, which happens to be
MMXI
.
Logging SOAP Messages with a JAX-WS Handler
The
RomanNumerals
application used
SOAPMessage
's
void
writeTo(OutputStream out)
methodtodumpSOAPmessagestothestandard
outputstream.Ifyouwanttoaccomplishthistaskinthecontextof
Listing11-7
'
s
Tem-
pVerterClient
application, you need to install a JAX-WS handler.
JAX-WSletsyouinstallachainofhandlersonawebserviceclass,aclientclass,or
bothtoperformcustomprocessingofrequestandresponsemessages.Forexample,you
mightuseahandlertoaddsecurityinformationtothemessageortologmessagedetails.
A
handler
is an instance of a class that ultimately implements the
javax.xml.ws.handler.Handler<C extends MessageContext>
inter-
face in terms of the following methods: