Site icon IT World Canada

How to become the integration guy

Talking Shop

I’m betting my career on the theme of integration. I want to be the guy who builds the tunnels between silos, sews the seams into the patchwork quilts or, more literally, architects the Web services that connect information systems.

In order to demonstrate the potential value of such a vocation, I’ve created a simple program that “consumes” a Web service, exchanging messages via SOAP.

SOAP stands for Simple Object Access Protocol. It is an XML-based message format that is designed to allow applications to communicate over HTTP. Request/Response details are encapsulated within a SOAP envelope, which is the top-level element in a SOAP message:

An application that has a SOAP interface is a Web service, and some great examples of this can be found at www.xmethods.com.

The example I chose to use in my application was Eliza ( www34.brinkster.com/4xws/eliza.asmx) – a chat simulator. Enter a text string and the Eliza Web service formulates a response and sends it back. I liked this example because it does a very good job of conveying the “black box” nature of consuming someone else’s Web service – you know what goes in and what comes out, but the processing in between is none of your concern.

The Web site for Eliza defines the following structure for the body of the SOAP message:

string

My program, coded in Java, takes input from the keyboard to populate the element. It then listens for the responding SOAP message, and prints the relevant piece of it to the console. It loops until the user has nothing more to say.

I split the SOAP message to send into two strings – one representing everything up to the opening tag and the other representing the closing tag and everything after:

String firstPart = “” +

” +

“”;

String secondPart = “”;

The complete SOAP message is a concatenation of the two halves with the keyboard input String inserted between:

String SOAPMessage = firstPart + kybdInputString + secondPart;

Now I need to send this message to the Web service. I’ll convert my message string to an array of bytes, create a connection to the service’s URL and transmit the byte array using HTTP POST.

byte[] messageBytes = SOAPMessage.getBytes();

URL url = new URL(“http://www34.brinkster.com/4xws/eliza.asmx”);

URLConnection conn = url.openConnection();

HttpURLConnection httpConn = (HttpURLConnection) conn;

httpConn.setRequestMethod( “POST” );

httpConn.setRequestProperty( “Content-Length”, String.valueOf(messageBytes.length));

httpConn.setRequestProperty(“Content-Type”,”text/xml; charset=utf-8″);

httpConn.setRequestProperty(“SOAPAction”,”http://xws.webservice.net/xws/CHAT”);

httpConn.setDoOutput(true);

httpConn.setDoInput(true);

OutputStream out = httpConn.getOutputStream();

out.write(messageBytes );

out.flush();

The message is sent. To listen for the response, we read from the HttpURLConnection’s InputStream:

BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

StringBuffer buf = new StringBuffer();

String inputLine;

while ((inputLine = in.readLine()) != null) buf.append(inputLine);

We don’t want to show the complete return SOAP message in the console – we just want to print the reponse string. It is nested within a element, so the following line parses and displays:

System.out.println(

buf.substring ( buf.indexOf (“”), buf.indexOf (“”) )

);

Here’s a sample of the program in action:

Say: hello

How do you do. Please state your problem.

Say: i need to finish this column!

What would it mean to you if you got to finish this column?

Say: i can go get some sleep for a change

Have fun chatting with Eliza, and feel free to e-mail me if you’d like a copy of the full Java source file.

Cooney works as a programmer/analyst for a major Canadian book publisher. He can be reached atrobert_cooney@hotmail.com.

Exit mobile version