title: How to get the XML response body back from a JAX-WS client?
body: <p>I've found a read a number of threads on here about how to retrieve the XML response from a JAX-WS client.  In my case, the client is generated from the WSDL via Oracle's JDeveloper product and is going to invoke a Document/Literal service endpoint that was written in .NET.  What I want to do is obtain the XML response from the call FROM the calling client, not from inside a handler.</p>
  
  <p>The closest thread that I saw to this issue was:
  <a href="http://www.coderanch.com/t/453537/Web-Services/java/capture-SoapRequest-xml-SoapResponse-xml" rel="nofollow">http://www.coderanch.com/t/453537/Web-Services/java/capture-SoapRequest-xml-SoapResponse-xml</a></p>
  
  <p>I don't think I want to generate a Dispatch call because the endpoint's XML schema for the SOAP packet is rather complex, and the automatic proxy makes the call trivial.  Unless there is some other way to populate the generated bean(s) and then invoke some method that simply produces the XML and I then make the call?</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>My thought was to store the response to the call in a ThreadLocal inside the handler and then read it after the call.  Is that reasonable?  So after the handler does the above code in the handleMessage and handleFault, the client calling code invokes this method:</p>
  
  <pre><code>@Override    
  public String getSOAPResponseXML(Object clientstub) {
      String returnValue = PaymentGatewayXMLThreadLocal.get();
      return returnValue;
  } // getSOAPResponseXML
  </code></pre>
  
  <p>It appears there may be another way after all.  After reading <a href="https://jax-ws.java.net/nonav/2.2.1/docs/handlers.html" rel="nofollow">jax-ws-handlers</a>, I saw that the handler can introduce an Application scoped variable.  I changed the handler to do this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The client just reads it like this:</p>
  
  <pre><code>@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&lt;String, Object&gt; requestContext = bindingProvider.getRequestContext();
      // String returnValue = (String) requestContext.get(JaxWsClientResponseXmlHandler.SOAP_RESPONSE_XML);
  
      // this works!!
      Map&lt;String, Object&gt; 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
  </code></pre>