Adding SOAP header to response messages
"Pietro Romanazzi" <[email protected]>
| Newsgroups | gmane.text.xml.axis.user |
|---|---|
| Message-ID | <[email protected]> |
Hello guys
I need to add a custom header to a response SOAP message.
The simple handler I wrote is:
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;
public class AddHeaderHandler extends AbstractHandler {
public AddHeaderHandler() {
}
public InvocationResponse invoke(MessageContext ctx) {
SOAPEnvelope env = ctx.getEnvelope();
System.out.println("Soap Envelope before header injection: "+env.toString());
SOAPFactory factory = (SOAPFactory) env.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://my.org/axis2", "myns");
SOAPHeader head = factory.createSOAPHeader(env);
SOAPHeaderBlock header = head.addHeaderBlock("greetings", ns);
header.setText("byebye");
System.out.println("Soap Envelope after header injection"+env.toString());
return InvocationResponse.CONTINUE;
}
}
This is a fragment of axis2.xml
<phaseOrder type="OutFlow">
<!-- user can add his own phases to this area -->
<phase name="soapmonitorPhase"/>
<phase name="OperationOutPhase">
<handler name="greetings" class="AddHeaderHandler" />
</phase>
<!--system predefined phase-->
<!--these phase will run irrespective of the service-->
<phase name="RMPhase"/>
<phase name="PolicyDetermination"/>
<phase name="MessageOut">
</phase>
<phase name="Security"/>
</phaseOrder>
in response I see a soap fault
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>SOAPEnvelope must contain a body element which is either first or second child element of the SOAPEnvelope.</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>
Debugging the code I see that the handler is started but, after the statement
SOAPHeader head = factory.createSOAPHeader(env);
If I watch the env variable I see
com.sun.jdi.InvocationException occurred invoking method (the toString I suppose).
It seems that the createSOAPHeader corrupts the env.
I tried to put the handler in the “MessageOut” phase but the problem is the same.
If I put the same handler in the “InFlow”, even if it doesn’t make any sense but just to try, it completes properly and the header is added prior the dispatching phase but obviously the response doesn’t contain the header.
Help, suggestions?
Thanks
Pietro