You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by swastb <sw...@cognizant.com> on 2011/02/14 09:47:07 UTC

Problem with outbound soap message

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

-- 
View this message in context: http://cxf.547215.n5.nabble.com/Problem-with-outbound-soap-message-tp3384130p3384130.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Problem with outbound soap message

Posted by Daniel Kulp <dk...@apache.org>.
 
> 	@Override
> 	public void handleMessage(SoapMessage message) throws Fault {
> 
> 		System.out.println(message.getInterceptorChain());
> 		message.getInterceptorChain().add(new EndingInterceptor());
> 
> 	}

You need to make sure the getSOAPMessage(message) call is made there to setup 
the SAAJ stuff.

Dan



On Wednesday 16 February 2011 11:25:40 PM swastb wrote:
> Hi Daniel,
> 
> Thanks a lot for the reply. Its working really fine with DOM. But I tried
> the following as well but still getting error as
> 
> Feb 17, 2011 9:46:27 AM
> 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)
> 
> I tried the following code
> private static SAAJOutInterceptor  saajOut = new SAAJOutInterceptor ();
> 	public HelloOutboundInterceptor(){
> 		super(Phase.PRE_PROTOCOL);
> 		addAfter(SoapOutInterceptor.class.getName());
> 
> 	}
> 
> 	@Override
> 	public void handleMessage(SoapMessage message) throws Fault {
> 
> 		System.out.println(message.getInterceptorChain());
> 		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 {
> 
>         	try {
>         		SOAPMessage soapMessage = getSOAPMessage(message);
> 				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);
> 			}
>         }
>     }

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog
Talend - http://www.talend.com

Re: Problem with outbound soap message

Posted by swastb <sw...@cognizant.com>.
Hi Daniel,

Thanks a lot for the reply. Its working really fine with DOM. But I tried
the following as well but still getting error as 

Feb 17, 2011 9:46:27 AM
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)

I tried the following code 
private static SAAJOutInterceptor  saajOut = new SAAJOutInterceptor ();	
	public HelloOutboundInterceptor(){
		super(Phase.PRE_PROTOCOL);
		addAfter(SoapOutInterceptor.class.getName());

	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		
		System.out.println(message.getInterceptorChain()); 		
		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 { 
        	
        	try {
        		SOAPMessage soapMessage = getSOAPMessage(message);
				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);
			} 
        } 
    } 
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Problem-with-outbound-soap-message-tp3384130p3388875.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Problem with outbound soap message

Posted by Daniel Kulp <dk...@apache.org>.

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
dkulp@apache.org
http://dankulp.com/blog