javasoapjax-wswebservice-clientsoaphandler

WRONG_DOCUMENT_ERR when attaching Element to SOAP Header


Currently I get the following error when I am attempt to attach an org.w3c.dom.Element to an existing SOAPHeader in a javax.xml.ws.handler.soap.SOAPHandler during an outgoing client side message:

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it

This problem only occurs if I make a seperate jax-ws client call to another webservice from within the handleMessage() function. To answer some questions, I am properly importing and cloning the Element object when attempting to attach it and can successfully do so as long as I don't make a subsequent webservice call as I stated above. Both my client side call and webservice are running on JBoss EAP 5.1. Thoughts? Suggestions? Example usage has been depicted below:

public boolean handleMessage(SOAPMessageContext ctx) {
    Boolean outbound = (Boolean) msgContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if(!outbound)
    {
       SOAPPart document = ctx.getMessage().getSOAPPart();          
       SOAPHeaderElement wsse = getSecurityHeaderElement(document.getEnvelope());

       //Extra Webservice call
       Service service=Service.create(wsdlUrl,qname);
       WebserviceInterface ws=service.getPort(WebserviceInterface.class);
       ws.helloWorld();
       //End of other webservice call

       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder docBuilder= factory.newDocumentBuilder();
       Document doc=docBuilder.newDocument();
       //Insert appending nodes here
       Element xmlElement=doc.getDocumentElement();

       Node node = document.importNode(xmlElement.cloneNode(true),true);    
       wsse.appendChild(node);
    }
}   

What baffles me most is that this other webservice call should have 0 effect on the originating webservices SOAPHeader, but again if I remove the webservice call the problem goes away.


Solution

  • So after further analysis, it turns out calling a webservice from within the handleMessage() method isn't the issue; however, specifically instantiating a new instance of any webservice during the handleMessage() phase is what causes the problem. Still not sure why this is an issue (guessing it's a bug with APACHE XERCES or JBoss 5.1) but here is an example of how it can work.

    private WebserviceInterface ws;
    
    //Assuming this method is only called when not handling a message
    public void init()
    {
         Service service=Service.create(wsdlUrl,qname);
         ws=service.getPort(WebserviceInterface.class);
    }
    public boolean handleMessage(SOAPMessageContext ctx)
    {
         Boolean outbound = (Boolean)msgContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
         if(!outbound)
         {
              SOAPPart document = ctx.getMessage().getSOAPPart();          
              SOAPHeaderElement wsse = getSecurityHeaderElement(document.getEnvelope());
    
              //Extra Webservice call
              Element xmlElement=ws.helloWorld();
    
              Node node = document.importNode(xmlElement.cloneNode(true),true);    
              wsse.appendChild(node);
          }
    }