Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modify a SOAP message...  RSS feed

Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="" xmlns:xsd="" xmlns:xsi="">
<ns1:testMethod soapenv:encodingStyle="" xmlns:ns1="LogTestService"/>
And I am trying to insert the following SOAPBodyElement into the above message:
<WOMBAT:GetLastTradePrice xmlns:WOMBAT="">
<ns1 rice xmlns:ns1=""/>
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.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 =
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", "");

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","");
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));


} catch (Exception e) {
writer.println("Exception in LOGHANDLER: " + e.getMessage());
throw AxisFault.makeFault(e);
} finally {
Any help is appreciated.
Thank you.
Always! Wait. Never. Shut up. Look at this tiny ad.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!