soap-rpc
it defines a model for representing an rpc and an rpc response using the soap
infrasture.
what's really important is that soap defines a uniform model for
representing an rpc and its return valuee or values. the fundamental requirements for
an rpc all are that the body element contains the method name and the parameters and
that the parameters are accessible via accessors. in addition, soap has provisions for
encoding the mehtod signature ,header data, and the uri that represents the destination.
a remote procedure call
public static float sendSoapRPCMessage(String url,String isbc) throws Exception
{
org.apache.soap.rpc.Call call=new org.apche.soap.rpc.Call();
String encodingStyleURL=org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
call.setTargetObjectURI("urn:xmethods-BNPriceCheck");
call.setMethodName("getPrice");
Vector params=new Vector();
params.addElelment(new org.apache.soap.rpc.Parameter("isbn",String.class,isbn,null));
call.setParams(params);
org.apache.soap.rpc.Response resp=call.invoke(new java.net.URL(url),");
}
another simple example
the client:
import java.net.*;
import java.util.*;
public class CheckStock {
private static final String DEFAULT_HOST_URL
= "http://localhost:8080/soap/servlet/rpcrouter";
private static final String DEFAULT_ITEM = "Test";
private static final String URI = "urn:oreilly-jaws-samples";
//Member variables
private String m_hostURL;
private String m_item;
public CheckStock (String hostURL, String item) throws Exception
{
m_hostURL = hostURL;
m_item = item;
// print stuff to the console
...
}
public void checkStock( ) throws Exception {
//Build the call.
org.apache.soap.rpc.Call call = new org.apache.soap.rpc.Call ( );
//This service uses standard SOAP encoding
String encodingStyleURI = org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
//Set the target URI
call.setTargetObjectURI ("urn:stock-onhand");
//Set the method name to invoke
call.setMethodName ("getQty");
//Create the parameter objects
Vector params = new Vector ( );
params.addElement (new org.apache.soap.rpc.Parameter("item",
String.class, m_item, null));
//Set the parameters
call.setParams (params);
//Invoke the service
org.apache.soap.rpc.Response resp
= call.invoke ( new java.net.URL(m_hostURL),"urn:go-do-this");
//Check the response
if (resp != null) {
if (resp.generatedFault ( )) {
org.apache.soap.Fault fault = resp.getFault ( );
System.out.println ("Call failed due to a SOAP Fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
Java Web Services
62
System.out.println("Fault String = " + fault.getFaultString());
} else {
org.apache.soap.rpc.Parameter result = resp.getReturnValue( );
Integer intresult = (Integer) result.getValue( );
System.out.println ("The stock-on-hand quantity for this item
is: " + intresult );
}
}
}
/** Main program entry point. */
public static void main(String args[]) {
// Command line parsing
...
// Start the CheckStock client
try
{
CheckStock stockClient = new CheckStock(hostURL, item);
stockClient.checkStock( );
}catch(Exception e){
System.out.println(e.getMessage( ));
}
}
...
}
the service:
import java.net.*;
import java.util.*;
public class CheckStock {
private static final String DEFAULT_HOST_URL
= "http://localhost:8080/soap/servlet/rpcrouter";
private static final String DEFAULT_ITEM = "Test";
private static final String URI = "urn:oreilly-jaws-samples";
//Member variables
private String m_hostURL;
private String m_item;
public CheckStock (String hostURL, String item) throws Exception
{
m_hostURL = hostURL;
m_item = item;
// print stuff to the console
...
}
public void checkStock( ) throws Exception {
//Build the call.
org.apache.soap.rpc.Call call = new org.apache.soap.rpc.Call ( );
//This service uses standard SOAP encoding
String encodingStyleURI = org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
//Set the target URI
call.setTargetObjectURI ("urn:stock-onhand");
//Set the method name to invoke
call.setMethodName ("getQty");
//Create the parameter objects
Vector params = new Vector ( );
params.addElement (new org.apache.soap.rpc.Parameter("item",
String.class, m_item, null));
//Set the parameters
call.setParams (params);
//Invoke the service
org.apache.soap.rpc.Response resp
= call.invoke ( new java.net.URL(m_hostURL),"urn:go-do-this");
//Check the response
if (resp != null) {
if (resp.generatedFault ( )) {
org.apache.soap.Fault fault = resp.getFault ( );
System.out.println ("Call failed due to a SOAP Fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
Java Web Services
62
System.out.println("Fault String = " + fault.getFaultString());
} else {
org.apache.soap.rpc.Parameter result = resp.getReturnValue( );
Integer intresult = (Integer) result.getValue( );
System.out.println ("The stock-on-hand quantity for this item
is: " + intresult );
}
}
}
/** Main program entry point. */
public static void main(String args[]) {
// Command line parsing
...
// Start the CheckStock client
try
{
CheckStock stockClient = new CheckStock(hostURL, item);
stockClient.checkStock( );
}catch(Exception e){
System.out.println(e.getMessage( ));
}
}
...
}
the deployment descriptor:
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:stock-onhand">
<isd:provider type="java"
scope="Application"
methods="getQty">
<isd:java class="StockQuantity"/>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener
</isd:faultListener>
</isd:service>