Listing 1 WSDL for the HTTP/SOAP binding
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="WSIFEJBAddressBookService"
targetNamespace=
"http://ejb.service.addressbook.wsifsamples.wsdl/
WSIFEJBAddressBookService/"
xmlns:tns=
"http://ejb.service.addressbook.wsifsamples.wsdl/
WSIFEJBAddressBookService/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:typens="http://types.addressbook.wsifsamples/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<!-- Tyles Definition -->
<types>
<schema attributeFormDefault="qualified"
elementFormDefault="unqualified"
targetNamespace="http://types.addressbook.wsifsamples/">
<complexType name="WSIFPhone">
<all>
<element name="exchange" nillable="true" type="xsd:string"/>
<element name="areaCode" type="xsd:int"/>
<element name="number" nillable="true" type="xsd:string"/>
</all>
</complexType>
<complexType name="WSIFAddress">
<all>
<element name="phoneNumber" nillable="true"
type="typens:WSIFPhone"/>
<element name="zip" type="xsd:int"/>
<element name="streetNum" type="xsd:int"/>
<element name="state" nillable="true" type="xsd:string"/>
<element name="streetName" nillable="true" type="xsd:string"/>
<element name="city" nillable="true" type="xsd:string"/>
</all>
</complexType>
</schema>
</types>
<!-- Message Definition -->
<message name="getAddressFromNameRequest">
<part name="name" type="xsd:string"/>
</message>
<message name="getAddressFromNameResponse">
<part name="result" type="typens:WSIFAddress"/>
</message>
<message name="addEntryRequest">
<part name="name" type="xsd:string"/>
<part name="address" type="typens:WSIFAddress"/>
</message>
<message name="faultMessage">
<part name="error" type="soap:fault"/>
</message>
<message name="addEntryResponse"/>
<!-- Port Definition -->
<portType name="WSIFEJBAddressBook">
<operation name="getAddressFromName" parameterOrder="name">
<input message="tns:getAddressFromNameRequest"
name="getAddressFromNameRequest"/>
<output message="tns:getAddressFromNameResponse"
name="getAddressFromNameResponse"/>
</operation>
<operation name="addEntry" parameterOrder="name address">
<input message="tns:addEntryRequest" name="addEntryRequest"/>
<output message="tns:addEntryResponse" name="addEntryResponse"/>
</operation>
</portType>
<!-- Binding Definition -->
<binding name="WSIFEJBAddressBookBinding" type="tns:WSIFEJBAddressBook">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getAddressFromName">
<soap:operation soapAction="" style="rpc"/>
<input name="getAddressFromNameRequest">
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace=
"http://tempuri.org/wsifsamples.addressbook.service.ejb.
WSIFEJBAddressBook" parts="name" use="encoded"/>
</input>
<output name="getAddressFromNameResponse">
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace=
"http://tempuri.org/wsifsamples.addressbook.service.ejb.
WSIFEJBAddressBook" parts="result" use="encoded"/>
</output>
</operation>
<operation name="addEntry">
<soap:operation soapAction="" style="rpc"/>
<input name="addEntryRequest">
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace=
"http://tempuri.org/wsifsamples.addressbook.service.ejb.
WSIFEJBAddressBook" parts="name address" use="encoded"/>
</input>
<output name="addEntryResponse">
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace=
"http://tempuri.org/wsifsamples.addressbook.service.ejb.
WSIFEJBAddressBook" use="encoded"/>
</output>
</operation>
</binding>
<!-- Service Definition -->
<service name="WSIFEJBAddressBookService">
<port binding="tns:WSIFEJBAddressBookBinding" name="WSIFEJBAddressBookPort">
<soap:address location=
"http://localhost:9080/WSIFAddressBookService/servlet/rpcrouter"/>
</port>
</service>
</definitions>
Listing 2 Basic WSIF client
package wsifsamples.addressbook.client;
import java.util.Iterator;
import java.util.Locale;
import java.io.*;
import org.apache.wsif.WSIFException;
import org.apache.wsif.WSIFConstants;
import org.apache.wsif.WSIFMessage;
import org.apache.wsif.WSIFOperation;
import org.apache.wsif.WSIFPort;
import org.apache.wsif.WSIFService;
import org.apache.wsif.WSIFServiceFactory;
import wsifsamples.addressbook.types.WSIFAddress;
import wsifsamples.addressbook.types.WSIFPhone;
import java.util.*;
/**
* This is a sample showing dynamic invocation of a web service using WSIF.
* The sample is based on a simple addressbook which allows names and addressses
* to be stored and retrieved.
*/
public class WSIFAddressBookClient {
public static void main(String[] args) {
String wl = "http://localhost:9080/WSIFAddressBookService/wsdl/wsifsamples/
addressbook/service/ejb/WSIFEJBAddressBookServiceC.wsdl";
try {
// The starting point for any dynamic invocation using wsif is a
// WSIFServiceFactory. We create ourselves one via the newInstance
// method.
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
System.out.println("\tWSIF factory created");
// Once we have a factory, we can use it to create a WSIFService object
// corresponding to the AddressBookService service in the wsdl file.
WSIFService service = factory.getService(
wsdlLocation, // location of the wsdl file
null, // service namespace
null, // service name
null, // port type namespace
null // portName
);
System.out.println("\tWSIF service created");
// Our WSDL file contains the definitions for two complexType
// elements within the schema element. We will now map these complexTypes
// to Java classes. These mappings are used by the Apache SOAP provider
service.mapType(
new javax.xml.namespace.QName(
"http://types.addressbook.wsifsamples/",
"WSIFAddress"),
Class.forName("wsifsamples.addressbook.types.WSIFAddress"));
service.mapType(
new javax.xml.namespace.QName(
"http://types.addressbook.wsifsamples/",
"WSIFPhone"),
Class.forName("wsifsamples.addressbook.types.WSIFPhone"));
System.out.println("\tWSIF types map created");
// We now have a WSIFService object. The next step is to create a WSIFPort
// object for the port we wish to use.
WSIFPort port = service.getPort();
System.out.println("\tWSIF port created");
// Once we have a WSIFPort,we can create an operation.
WSIFOperation operation =
port.createOperation("addEntry", "addEntryRequest", "addEntryResponse");
System.out.println ("\tWSIF operation created");
// Create messages to use in the execution of the operation. This should
// be done by invoking the createXXXXXMessage methods on the WSIFOperation.
WSIFMessage inputMessage = operation.createInputMessage();
WSIFMessage outputMessage = operation.createOutputMessage();
WSIFMessage faultMessage = operation.createFaultMessage();
// Create a name and address to add to the addressbook
String nameToAdd = "Jack";
WSIFAddress addressToAdd =
new WSIFAddress (1,
"The Waterfront",
"Some City",
"NY",
47907,
new WSIFPhone (765, "494", "4900"));
// Add the name and address to the input message
inputMessage.setObjectPart("name", nameToAdd);
inputMessage.setObjectPart("address", addressToAdd);
System.out.println ("\tWSIF messages created");
// Execute the operation, obtaining a flag to indicate its success
boolean operationSucceeded;
try{
operationSucceeded =
operation.executeRequestResponseOperation(
inputMessage,
outputMessage,
faultMessage);
if (operationSucceeded) {
System.out.println ("Successfully added name and address");
} else {
System.out.println("Failed to add name and address");
}
}
catch(Exception e){}
// Start from fresh
operation = null;
inputMessage = null;
outputMessage = null;
faultMessage = null;
// Lookup an address from the addressbook.
operation = port.createOperation("getAddressFromName");
// Create the messages
inputMessage = operation.createInputMessage();
outputMessage = operation.createOutputMessage();
faultMessage = operation.createFaultMessage();
// Set the name to find in the addressbook
String nameToLookup = "Jack";
inputMessage.setObjectPart("name", nameToLookup);
// Execute the operation
operationSucceeded =
operation.executeRequestResponseOperation(
inputMessage,
outputMessage,
faultMessage);
if (operationSucceeded) {
System.out.println ("Successfull lookup of name '"
+ nameToLookup + "' in addressbook");
// We can obtain the address that was found
WSIFAddress addressFound = (WSIFAddress)
outputMessage.getObjectPart("result");
System.out.println("The address found was:");
System.out.println(addressFound);
} else {
System.out.println("Failed to lookup name in addressbook");
}
} catch (Exception e) {
System.out.println("An exception occurred when running the sample:");
e.printStackTrace();
}
}
}