You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Da...@stpaul.com on 2003/10/31 02:24:28 UTC

headers and body

I'm trying to write a web service that will take the incoming SOAP Header
and Body elements and invoke another web service using the Header and Body
elements I previously gathered.  I looked for methods to add the header and
body elements to the envelope, but didn't find anything useful.  Do I have
to iterate through all the header and body children to get this
accomplished or is there an easier way?  Does anyone have sample code?

Thanks,

David



Re: headers and body

Posted by Stephen Gordon <st...@student.usyd.edu.au>.
Here's what I did with the body - I used JDOM to convert from SAX to DOM:

Imports:
-----------------SNIP--------------------
import org.apache.axis.Message;
import org.apache.axis.message.SOAPBodyElement;
import org.apache.axis.message.SOAPEnvelope;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-----------------SNIP--------------------

Then in invoke:

-----------------SNIP--------------------

SAXBuilder saxBuild = new SAXBuilder();
DOMOutputter domOut = new DOMOutputter();
Message msg = msgContext.getCurrentMessage();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos); baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Document msgJdomDoc = saxBuild.build(bais); bais.close();

// Custom code to modify Document goes in here...

Element msgDomElem = domOut.output(msgJdomDoc).getDocumentElement();
NodeList bodyNodes = msgDomElem.getElementsByTagNameNS(SOAP_ENV_URI, 
"Body");
if (bodyNodes.getLength() != 1) return;
NodeList bodyElemList = bodyNodes.item(0).getChildNodes();

SOAPEnvelope soapEnv = new SOAPEnvelope();
for (int i = 0; i < bodyElemList.getLength(); i++) {
	Node bodyElem = bodyElemList.item(i);
         if (bodyElem.getNodeType() == Node.ELEMENT_NODE) {
         	SOAPBodyElement soapBodyElem = new 
SOAPBodyElement((Element)bodyElem);
		soapEnv.addBodyElement(soapBodyElem);
	}
}

Message newMsg = new Message((Object)soapEnv);
msgContext.setCurrentMessage(newMsg);

-----------------SNIP--------------------

Hope this helps (there's a few exceptions you'll need to catch),

stephen

David.Hegg@stpaul.com wrote:

> I'm trying to write a web service that will take the incoming SOAP Header
> and Body elements and invoke another web service using the Header and Body
> elements I previously gathered.  I looked for methods to add the header and
> body elements to the envelope, but didn't find anything useful.  Do I have
> to iterate through all the header and body children to get this
> accomplished or is there an easier way?  Does anyone have sample code?
> 
> Thanks,
> 
> David
> 
> 
> 
>