SOAP message handler for logging inbound SOAP results
I recently had the need to log the results of some calls to soap service my app was making. I attached the SimpleHandler to the xml.rpc.client.BasicService
public class SimpleHandler extends GenericHandler {
...
@Override
public boolean handleResponse(MessageContext context) {
try {
// get the soap header
SOAPMessageContext smc = (SOAPMessageContext) context;
SOAPMessage soapMessage = smc.getMessage();
// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
// Get reply content
Source sc = soapMessage.getSOAPPart().getContent();
// Set output transformation
ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
StreamResult result = new StreamResult(streamOut);
tf.transform(sc, result);
String strMessage = streamOut.toString();
log.info("SOAP MESSAGE:\n"+strMessage+"\n");
} catch (Exception e) {
log.error("Exception in SimpleHander!");
throw new JAXRPCException(e);
}
return true;
}
...
}
