Title: HELP: Modify Message Body in Handler
I have come to the conclusion that it is simply not possible to change the body content (not the header) of a message from within a handler. For whatever reason the changes made are not reflected in the message after it leaves the handler. I have also talked to several other developers on these boards who had the same problem and they were never able to find a solution. The closest I was able to come to a solution is the code below:
 
            javax.xml.soap.SOAPMessage soapMessage = msgContext.getMessage();
            javax.xml.soap.SOAPPart sp = soapMessage.getSOAPPart();
            javax.xml.soap.SOAPEnvelope se = sp.getEnvelope();
           
            se.getBody().detachNode();
            javax.xml.soap.SOAPBody nsb = se.addBody();
           
            javax.xml.soap.Name name = se.createName("test_element");
            javax.xml.soap.SOAPBodyElement sbe = nsb.addBodyElement(name);
           
            javax.xml.soap.Name name2 = se.createName("version");
            javax.xml.soap.SOAPElement sel = sbe.addChildElement(name2);
            sel.addTextNode("1.0");
This did change the body by removing it and putting another in its place. But then Axis threw an exception later when it tried to deserialize the message saying that the context was not set (MessageElement.getValueAsType()).
 
I'll keep looking at the source code to see if I can find the problem, but it is looking pretty bleak at the moment.
 
Tripper
-----Original Message-----
From: Tripper McCarthy [mailto:[EMAIL PROTECTED]
Sent: Monday, June 23, 2003 4:34 PM
To: '[EMAIL PROTECTED]'
Subject: HELP: Modify Message Body in Handler

I have been trying to modify the content of the message body in one of my handlers before the message reaches the actual web service. I have read through a number of message from the mailing list, and tried just about everything I can think of. But my message body is unchanged after leaving my handler. Below is my current code. Does anyone have any suggestions?

    public void invoke(MessageContext msgContext) throws AxisFault {
            // Retrieve the soap body out of the message
            Message message = msgContext.getRequestMessage();
            org.apache.axis.message.SOAPEnvelope se = message.getSOAPEnvelope();
            org.apache.axis.message.SOAPBody sb = (org.apache.axis.message.SOAPBody)se.getBody();

            // Generic test
            MessageElement mElement = new MessageElement("","body_test");
            mElement.addTextNode("body test");
            sb.addChild(mElement);
           
            message.saveChanges();
            System.out.println("handler 1 = " + sb.toString());
}

The final println statement shows the exact message I started off with, without my changes.

Thanks

Tripper McCarthy
[EMAIL PROTECTED]

Reply via email to