The pattern of using the SAAJ interceptor like that really only works for the 
incoming side.   For the outgoing side, you would need to break the 
interceptor into two parts.   Something like:


public class HelloOutboundInterceptor extends AbstractSoapInterceptor{
     private SAAJOutInterceptor  saajOut = new SAAJOutInterceptor ();
     public HelloOutboundInterceptor(){
           super(Phase.PRE_PROTOCOL);
           addAfter(SAAJOutInterceptor.class.getName());
     }
     public void handleMessage(SoapMessage message) throws Fault { 
           //make sure the saaj model is setup
           getSOAPMessage(message);
           //add the ending interceptor to do the work
           message.getInterceptorChain().add(new EndingInterceptor());
     }

     static class EndingInterceptor extends AbstractSoapInterceptor {
          public EndingInterceptor() {
               super(Phase.PRE_PROTOCOL_ENDING);
               addBefore(SAAJOutEndingInterceptor.class.getName()); 
           }
          public void handleMessage(SoapMessage message) throws Fault { 
               //your logic here
          }
     }
}


THAT said, it may be better to just use an interceptor early in the chain 
(like USER_LOGICAL) to build up a DOM for your specific header and just do:


Header header = new Header(qname, dom);
message.getHeaders().add(header);

and CXF would automatically add the header to the output during writing 
without having to deal with the saaj tree.

Dan




On Monday 14 February 2011 3:47:07 AM swastb wrote:
> Hi,
> 
> I have written the following code for modifying the outbound soap message.
> I am using CXF 2.3.2 version.
> 
> public class HelloOutboundInterceptor extends AbstractSoapInterceptor{
> 
>       private SAAJOutInterceptor  saajOut = new SAAJOutInterceptor ();
>       public HelloOutboundInterceptor(){
>               super(Phase.PRE_PROTOCOL_ENDING);
>               addAfter(SAAJOutInterceptor.class.getName());
> 
>       }
> 
>       @Override
>       public void handleMessage(SoapMessage message) throws Fault {
> 
>               System.out.println(message.getInterceptorChain());
>               SOAPMessage soapMessage = getSOAPMessage(message);
> 
>               boolean isOutbound = message == 
message.getExchange().getOutMessage()
> 
>                               || message == 
> message.getExchange().getOutFaultMessage();
> 
>               System.out.println("isOutbound is "+isOutbound);
> 
>               if(isOutbound){
>                       try {
> 
>                               SOAPPart sp = soapMessage.getSOAPPart();
>                               SOAPEnvelope envelope = sp.getEnvelope();
>                               envelope.getHeader().detachNode();
>                               SOAPHeader header = envelope.addHeader();
>                               SOAPFactory soapFactory = 
> SOAPFactory.newInstance();
>                               Name headerName = 
> soapFactory.createName("Authentication",
>                                               "auth", 
> "http://sample.cxf.com/";);
>                               SOAPElement headerElement = 
header.addHeaderElement(headerName)
>                                               .addChildElement("TokenID");
>                               headerElement.addTextNode("token1234");
>                               System.out.println("Header is added 
> successfully");
>                               try {
>                                       soapMessage.writeTo(System.out);
>                               } catch (Exception e) {
>                                       System.out.println(e);
>                               }
>                       } catch (SOAPException e) {
>                               System.out.println(e);
>                       }
>               }
> 
>               System.out.println();
> 
>       }
> 
>       private SOAPMessage getSOAPMessage(SoapMessage smsg) {
>               SOAPMessage soapMessage = smsg.getContent(SOAPMessage.class);
>               if (soapMessage == null) {
>                       saajOut.handleMessage(smsg);
>                       soapMessage = smsg.getContent(SOAPMessage.class);
>               }
>               return soapMessage;
>       }
> 
> i am able to see the following soap message,
> <SOAP-ENV:Envelope
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";><S
> OAP-ENV:Header><auth:Authentication
> xmlns:auth="http://sample.cxf.com/";><TokenID
> 
> >token1234</TokenID></auth:Authentication></SOAP-ENV:Header><SOAP-ENV:Body/
> >></SO
> 
> AP-ENV:Envelope>
> but the client is not getting header in the soap message.I am seeing the
> following error in the tomcat logs
> 
> WARNING: Interceptor for
> {http://sample.cxf.com/}HelloWorldImplService#{http://s
> ample.cxf.com/}sayHi has thrown exception, unwinding now
> org.apache.cxf.binding.soap.SoapFault: Problem writing SAAJ model to stream
> Feb 14, 2011 2:06:35 PM
> org.apache.cxf.binding.soap.interceptor.Soap11FaultOutIn
> terceptor$Soap11FaultOutInterceptorInternal handleMessage
> WARNING: Error writing to XMLStreamWriter.
> org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to
> insert a
>  node where it is not permitted.
>         at
> com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.insertBefore(
> CoreDocumentImpl.java:381)
>         at
> com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.
> java:235)
> 
> I have followed the following link , but still it didnt solve my problem.
> Please help me.
> http://cxf.547215.n5.nabble.com/Problems-when-intercepting-an-outbound-SOAP
> -message-td548810.html

-- 
Daniel Kulp
[email protected]
http://dankulp.com/blog

Reply via email to