I am trying to modify either the SOAP Body elements or the Header elements on the server side. I have written a handler that gets the request message and modifies it before reaching the server. But I am not able to modify.
A snippet of the SOAP message is given below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:testMethod soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="LogTestService"/>
</soapenv:Body>
</soapenv:Envelope>
And I am trying to insert the following SOAPBodyElement into the above message:
<WOMBAT:GetLastTradePrice xmlns:WOMBAT="http://www.wombat.org/trader">
<ns1
rice xmlns:ns1="http://www.wombat.org/trade"/>
</WOMBAT:GetLastTradePrice>
But after I attach this element to the body, I cannot see this element in my modified SOAP Message.
I am using Axis 1.0 with Tomcat 4.0.6. Also, my code is below for quick reading.
package samples.userguide.example4;
import org.apache.axis.Handler;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.Message;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.message.MessageElement;
import org.apache.axis.encoding.Base64;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Element;
import javax.crypto.*;
import javax.xml.soap.*;
import java.util.*;
import java.io.*;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Date;
public class LogHandler extends BasicHandler {
public void invoke(MessageContext msgContext) throws AxisFault
{
/** Log an access each time we get invoked.
*/
String filename = (String)getOption("filename");
if ((filename == null) || (filename.equals("")))
throw new AxisFault("Server.NoLogFile",
"No log file configured for the LogHandler!",
null, null);
PrintWriter writer = null;
try {
Handler serviceHandler = msgContext.getService();
FileOutputStream fos = new FileOutputStream(filename, true);
writer = new PrintWriter(fos);
Integer numAccesses =
(Integer)serviceHandler.getOption("accesses");
if (numAccesses == null)
numAccesses = new Integer(0);
numAccesses = new Integer(numAccesses.intValue() + 1);
Date date = new Date();
String result = date + ": service " +
msgContext.getTargetService() +
" accessed " + numAccesses + " time(s).";
serviceHandler.setOption("accesses", numAccesses);
// get to the message
Message reqMessage = msgContext.getRequestMessage();
org.apache.axis.message.SOAPEnvelope env = (org.apache.axis.message.SOAPEnvelope)reqMessage.getSOAPEnvelope();
SOAPBody body = env.getBody();
Name name = env.createName("GetLastTradePrice", "WOMBAT", "http://www.wombat.org/trader");
SOAPElement bodyElement = body.addBodyElement(name);
Name nameEl = bodyElement.getElementName();
System.out.println("Local Name = " + nameEl.getLocalName());
System.out.println("Prefix = " + nameEl.getPrefix());
System.out.println("Qualified Name = " + nameEl.getQualifiedName());
System.out.println("URI = " + nameEl.getURI());
Name secRefList = env.createName("Price","WOMBAT","http://www.wombat.org/trade");
SOAPElement secRefListElem = bodyElement.addChildElement(secRefList);
Element bodyEl = ((org.apache.axis.message.SOAPBodyElement)bodyElement).getAsDOM();
writer.println("The SOAPBody Elem is: " + XMLUtils.ElementToString(bodyEl));
Element bodyWhole = ((org.apache.axis.message.SOAPBody)body).getAsDOM();
writer.println("\n\nThe SOAPBody is: " + XMLUtils.ElementToString(bodyWhole));
Element element = ((org.apache.axis.message.SOAPEnvelope)env).getAsDOM();
writer.println("\n\nThe SOAPENV after detaching SOAPBody Elem is: " + XMLUtils.ElementToString(element));
writer.println(result);
} catch (Exception e) {
writer.println("Exception in LOGHANDLER: " + e.getMessage());
e.printStackTrace();
throw AxisFault.makeFault(e);
} finally {
writer.close();
}
}
}
Any help is appreciated.
Thank you.