How to return body of XML response from JAX-WS client?

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;
} // getSOAPResponseXML

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);
// YourPayXMLThreadLocal.set(xml);
// put it into the messageContext as well, but change scope
// default of handler Scope, and client can't read it from responsecontext!
messageContext.put(SOAP_RESPONSE_XML, xml);
messageContext.setScope(SOAP_RESPONSE_XML, MessageContext.Scope.APPLICATION );
} // storeSOAPMessageXml

:

@Override    
public String getSOAPResponseXML(Object clientstub) {
    String returnValue = null;
    // works (assuming a threadlocal is ok)
    // returnValue = YourPayXMLThreadLocal.get();

    BindingProvider bindingProvider = (BindingProvider) clientstub;
    // Thought this would work, but it doesn't - it returns null.        
    // Map<String, Object> requestContext = bindingProvider.getRequestContext();
    // String returnValue = (String) requestContext.get(JaxWsClientResponseXmlHandler.SOAP_RESPONSE_XML);

    // this works!!
    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;
} // getSOAPResponseXML
+1
2

,

-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true

- , . , ? :

((BindingProvider) port).getRequestContext().put("KEY", "VALUE");

:

String value = (String) messageContext.get("KEY");
0

, XML - (. JAXB). XML, , , WS. , , , , SOAP, XML , .

0

All Articles