Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Please show the code you're using. You say you're properly importing the element - but without seeing the code, we can't tell whether or not that's true, and it's the obvious thing that could be going wrong. –  Jon Skeet May 29 '13 at 20:50
    
I can do that but if I wasn't importing it properly how would removing the extra webservice call allow the import to work? –  dsutherland May 30 '13 at 13:21
    
@JonSkeet - I've added some code to show the import occuring. Again though, I don't know how that would make a difference as removing the webservice call allows the import to occur without a problem. It's very strange. –  dsutherland May 30 '13 at 13:29
    
@JonSkeet - Furthermore, if I move the webservice call back to the first action in the function, the handler fails to even retrieve the SecurityHeaderElement with the same error. –  dsutherland May 30 '13 at 13:43
    
Is getSecurityHeaderElement your own method? If so, what does it do? If not, where is it from? While I can't see why the other web service call would cause a problem either, the more we know the more we may be able to help you. –  Jon Skeet May 30 '13 at 17:09

1 Answer 1

up vote 1 down vote accepted

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);
      }
}   
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.