I found here a few threads on how to get an XML response from a JAX-WS client. In my case, the client is generated from WSDL through the Oracle JDeveloper product and is going to call the Document / Literal service endpoint that was written in .NET. I want to get an XML response from calling the FROM of the calling client, and not from the handler.
The closest topic I've seen in this issue is:
http://www.coderanch.com/t/453537/Web-Services/java/capture-SoapRequest-xml-SoapResponse-xml
I don’t think I want to generate a Dispatch call because the XML endpoint schema for the SOAP package is quite complicated and the automatic proxy makes the call trivial. If there is no other way to populate the generated bean (s), then call some method that just creates the XML and then I call the call?
private void storeSOAPMessageXml(SOAPMessageContext messageContext) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SOAPMessage soapMessage = messageContext.getMessage();
try {
soapMessage.writeTo(baos);
String responseXml = baos.toString();
log.debug("Response: " + responseXml );
PaymentGatewayXMLThreadLocal.set(responseXml);
} catch (SOAPException e) {
log.error("Unable to retrieve SOAP Response message.", e);
} catch (IOException e) {
log.error("Unable to retrieve SOAP Response message.", e);
}
}
My thought was to save the response to the call in ThreadLocal inside the handler, and then read it after the call. It is reasonable? Therefore, after the handler executes the above code in handleMessage and handleFault, the calling client code calls this method:
@Override
public String getSOAPResponseXML(Object clientstub) {
String returnValue = PaymentGatewayXMLThreadLocal.get();
return returnValue;
}
There seems to be another way. After reading jax-ws-handlers, I saw that the handler can enter an application scope variable. I changed the handler to do this:
private void storeSOAPMessageXml(SOAPMessageContext messageContext) {
String xml = getSOAPMessageXml(messageContext);
messageContext.put(SOAP_RESPONSE_XML, xml);
messageContext.setScope(SOAP_RESPONSE_XML, MessageContext.Scope.APPLICATION );
}
:
@Override
public String getSOAPResponseXML(Object clientstub) {
String returnValue = null;
BindingProvider bindingProvider = (BindingProvider) clientstub;
Map<String, Object> responseContext = bindingProvider.getResponseContext();
System.out.println("has key? " + responseContext.containsKey(JaxWsClientResponseXmlHandler.SOAP_RESPONSE_XML));
returnValue = (String) responseContext.get(JaxWsClientResponseXmlHandler.SOAP_RESPONSE_XML);
return returnValue;
}