Low Level SOAP manipulation and JAX-WS invokation (SAAJ)

1. The Client Code

package com.vvirlan;

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class MessageFactoryExample {
	public static void main(String[] args) throws SOAPException, IOException {
		MessageFactoryExample ex = new MessageFactoryExample();
		ex.run();
	}
	private void run() throws SOAPException, IOException {
		MessageFactory factroy = MessageFactory.newInstance();
		SOAPMessage message = factroy.createMessage();
		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPHeader header = envelope.getHeader();
		SOAPBody body = envelope.getBody();
		//header.detachNode();
		QName bodyName = new QName("http://vvirlan.com/", "sayHello", "PRE");
		SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
		QName name = new QName("arg0");
		SOAPElement arg0 = bodyElement.addChildElement(name);
		arg0.addTextNode("Vlad");
		message.writeTo(System.out);
		System.out.println();
		SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
		SOAPConnection soapConnection = soapConnectionFactory.createConnection();
		URL endpoint = new URL("http://localhost:8080/JaxWsServiceExample/MyCalculatorService");
		SOAPMessage response = soapConnection.call(message, endpoint);
		response.writeTo(System.out);
soapConnection.close();
	}
}

2. The server (a very simple Dynamic Web App in eclipse running on Glassfish):

package com.vvirlan;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MyCalculator {
	@WebMethod
	public String sayHello(String msg) {
		return "Hi" + msg;
	}
}

3. Invokation results:
3.1. Request

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Header />
	<SOAP-ENV:Body>
		<PRE:sayHello xmlns:PRE="http://vvirlan.com/">
			<arg0>Vlad</arg0>
		</PRE:sayHello>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

3.2. Response

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns2:sayHelloResponse xmlns:ns2="http://vvirlan.com/">
			<return>HiVlad</return>
		</ns2:sayHelloResponse>
	</S:Body>
</S:Envelope>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s