2 Replies Latest reply on Feb 6, 2012 9:08 AM by Hordak Evil

    How to modify SOAP body in JBoss 6.1.0. final?

    Hordak Evil Newbie

      I'm trying to modify SOAP body for an outbound ws client SOAP message, from an application which is deployed on JBoss 6.1.0. Final.

      There is a registered HandlerResolver for that purpose, which has the following code:

       

       

      public boolean handleMessage(SOAPMessageContext messagecontext) {
              Boolean outbound = (Boolean) messagecontext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
              if (outbound) {
                  try {
                      SOAPMessage soapMessage = messagecontext.getMessage();
                      SOAPBody soapBody = soapMessage.getSOAPBody();
      
                      Node firstChild = soapBody.getFirstChild();
      
                      String timeStamp = getTimestamp();
                      String signature = getSignature(firstChild.getLocalName(), timeStamp, secretBytes);
      
                      SOAPFactory factory = SOAPFactory.newInstance();
                      SOAPElement signatureElement = factory.createElement("Signature");
                      System.out.println(signature);
                      signatureElement.addTextNode(signature);
      
                      SOAPElement timestampElement = factory.createElement("Timestamp");
                      timestampElement.addTextNode(timeStamp);
      
                      firstChild.appendChild(signatureElement);
                      firstChild.appendChild(timestampElement);                   
      
                  } catch(SOAPException se) {
                      throw new RuntimeException("SOAPException was thrown.", se);
                  }
              }
              return true;
          }
      

      However, it throws this exception:

       

       

      17:59:35,527 WARN  [org.apache.cxf.jaxws.handler.HandlerChainInvoker] HANDLER_RAISED_RUNTIME_EXCEPTION: org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
          at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source) [:1.6.0_27]
          at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source) [:1.6.0_27]
          at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source) [:1.6.0_27]
      
      ...
      ...
      17:59:35,594 ERROR [STDERR] WS exception
      
      17:59:35,594 ERROR [STDERR] javax.xml.ws.soap.SOAPFaultException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
      
      17:59:35,595 ERROR [STDERR]     at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:146)
      
      17:59:35,595 ERROR [STDERR]     at $Proxy301.itemSearch(Unknown Source)
      
      17:59:35,595 ERROR [STDERR]     at com.youandshoe.search.DoSearch.doSearch(DoSearch.java:127)
      
      17:59:35,595 ERROR [STDERR]     at com.youandshoe.search.DoSearch.doSearch(DoSearch.java:174)
      
      17:59:35,595 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
      17:59:35,595 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
      17:59:35,595 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
      17:59:35,596 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Unknown Source)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:72)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor$InvocationContext.proceed(InvocationContextInterceptor.java:127)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.weld.integration.ejb.interceptor.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:151)
      
      17:59:35,596 ERROR [STDERR]     at org.jboss.weld.integration.ejb.interceptor.Jsr299BindingsInterceptor.doAroundInvoke(Jsr299BindingsInterceptor.java:122)
      
      17:59:35,596 ERROR [STDERR]     at sun.reflect.GeneratedMethodAccessor374.invoke(Unknown Source)
      
      17:59:35,596 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
      17:59:35,596 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Unknown Source)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:80)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:71)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:62)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor$InvocationContext.proceed(InvocationContextInterceptor.java:127)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:46)
      
      17:59:35,597 ERROR [STDERR]     at org.jboss.weld.integration.deployer.metadata.WeldLifecycleInterceptor.doAroundInvoke(WeldLifecycleInterceptor.java:55)
      
      17:59:35,597 ERROR [STDERR]     at sun.reflect.GeneratedMethodAccessor373.invoke(Unknown Source)
      
      17:59:35,597 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      ...
      

      for this line:

       

       firstChild.appendChild(signatureElement);
      

       

      This code worked fine with JBoss 5.1.0.

       

      Any ideas?

      Thanks.

        • 1. Re: How to modify SOAP body in JBoss 6.1.0. final?
          Jim Ma Apprentice

          From the error stack trace, looks you are using jbossws-cxf stack to send message. Because jbossws-cxf uses different saaj implementation, soapFactory.newInstance().createElement() could create element with another document. Below is an example appendChild with the retrieved document from soapBody, it may help resolve your problem.  

           

          SOAPMessage soapMessage = messagecontext.getMessage();
          SOAPBody soapBody = soapMessage.getSOAPBody();
          Node firstChild = soapBody.getFirstChild();
          Document doc = firstChild.getOwnerDocument();
          Node node1 = doc.createElementNS("http://www.foo.org", "bar");
          node1.appendChild(doc.createTextNode("text"));
          firstChild.appendChild(node1); 
          soapMessage.saveChanges();
          
          • 2. Re: How to modify SOAP body in JBoss 6.1.0. final?
            Hordak Evil Newbie

            Thank you very much, Jim! Your answer solved my problem.

             

            I thought there could be a problem because of difference between Axis and cxf frameworks, because the above code worked fine in JBoss 5.1.0, which used the Axis framework, but

            had no idea where to dig

             

            Thanks once agan,

             

            Martin