Listing 1
package message;
import org.apache.soap.*;
import org.apache.soap.rpc.SOAPContext;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;
import java.io.*;
public class MessageService {
public void process(
Envelope env,
SOAPContext requestContext,
SOAPContext responseContext) {
Document doc;
Node bodyNode;
Node bodyNodeParent = null;
try {
bodyNode = (Node)
env.getBody().getBodyEntries()
.elementAt(0);
Element request = (Element)
bodyNode.getFirstChild();
// make use of existing DOM object structure and
// just replace the body to return response
Node bodyNodeParent = bodyNode.getParentNode();
bodyNodeParent.removeChild(bodyNode);
// add the response element to the XML doc
doc = bodyNode.getOwnerDocument();
Node returnNode = doc.importNode(request, true);
bodyNodeParent.appendChild(returnNode);
// convert DOM structure to XML
OutputFormat format = new OutputFormat(doc);
StringWriter response = new StringWriter();
XMLSerializer serial = new XMLSerializer(response, format);
serial.asDOMSerializer();
serial.serialize((Element)
doc.getDocumentElement());
responseContext.setRootPart(response.toString(),"text/xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Listing 2
<root>
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="http://tempuri.org/message.MessageService"
type="message"
checkMustUnderstands="false">
<isd:provider type="java" scope="Application" methods="process">
<isd:java class="message.MessageService" static="false"/>
</isd:provider>
</isd:service>
</root>

Listing 3
package proxy.soap.message;
import java.net.*;
import java.util.*;
import org.w3c.dom.*;
import org.apache.soap.*;
import org.apache.soap.messaging.Message;
public class MessageServiceProxy {
protected Message soapMessage;
protected URL theURL = null;
public MessageServiceProxy() {
soapMessage = new Message();
soapMessage.setSOAPTransport(null);
}
public synchronized void setURL(String url) throws Exception {
theURL = new URL(url);
}
public void send(Element payload) throws SOAPException {
Element body = createBody(payload);
Envelope sendEnv = new Envelope();
Body sendBody = new Body();
Vector bodyEntries = new Vector();
bodyEntries.add(body);
sendBody.setBodyEntries(bodyEntries);
sendEnv.setBody(sendBody);
soapMessage.send(theURL, "", sendEnv);
}
public Element receive() throws SOAPException {
Element response = null;
Envelope responseEnv = soapMessage.receiveEnvelope();
Body responseBody = responseEnv.getBody();
response = (Element)
responseBody.getBodyEntries().firstElement();
return response;
}
protected Element createBody(Element payload) {
Element body = null;
try {
Document doc = payload.getOwnerDocument();
body = doc.createElement("process");
body.setAttribute("xmlns",
"http://tempuri.org/message.MessageService");
body.appendChild(payload);
} catch (Exception ex) {
ex.printStackTrace();
}
return body;
}
}

Listing 4
import message.*;
import proxy.soap.message.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
public class TestMes {
public static void main(String[] args) {
MessageServiceProxy proxy = new
MessageServiceProxy();
try {
DocumentImpl doc = new DocumentImpl();
Element inputEl = doc.createElement("test");
inputEl.setAttribute("myAtt", "good");
proxy.setURL("http://localhost:8080/MessageService/servlet/
messagerouter"); proxy.send(inputEl);
Element response = proxy.receive();
String result = response.getAttribute("myAtt");
System.out.println("The web service echoed the attribute;
the value is \""+ result + "\"");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Listing 5
POST /MessageService/servlet/messagerouter HTTP/1.0
Host: localhost:8080
Content-Type: text/xml; charset=utf-8
Content-Length: 367
SOAPAction: ""
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<process xmlns="http://tempuri.org/message.
MessageService">
<test myAtt="good"/>
</process>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Listing 6
HTTP/1.1 200 OK
Server: WebSphere Application Server/4.0
Content-Type: text/xml
Set-Cookie: JSESSIONID=0000BHWUPBKL2DOVAPKAOXTQWAQ:-1;Path=/
Cache-Control: no-cache="set-cookie,set-cookie2"
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Content-Length: 291
Content-Language: en
Connection: close
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<test myAtt="good"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>