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.