WebReference.com - Part 5 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (4/6)
[previous] [next] |
Professional Java Web Services
Invoking the Call
Now that the SOAP request is properly set up we can now [invoke] the call:
// Invoke the call
Response resp = null;
try {
URL url = new URL(RPCROUTER);
resp = call.invoke(url, "");
} catch (SOAPException e) {
out.println("Caught SOAPException (" +
e.getFaultCode() + "): " + e.getMessage());
}
The invoke() method is used to send the SOAP request to the SOAP service. The method
signature of the invoke() method is below:
public response invoke(java.net.URL url, java.lang.String SOAPActionURI)
The url parameter is used to specify the endpoint for the Apache SOAP runtime. The
SOAPActionURI is used to specify the action to be called at the specified URL.
The invoke() method has to be within a try...catch clause because it
throws a SOAPException. A SOAPException will be thrown if the Call
object is set up incorrectly, or if there are network problems, and so on. The invoke() method
returns a Response object, which represents the response to the call.
Interpreting the Response
In this section we will discuss how the response from a SOAP server should be handled.
Successful Response
If the response doesn't contain a fault, then the return value can be obtained by first calling
the getReturnValue() method, which puts the return value into a Parameter object.
The getValue() method of the Parameter object can then be used to obtain the
actual value and manipulate the value as needed. The value returned is the UID of the
resumé that was submitted. If the value is not null then the value is printed.
// Check the response
if (!resp.generatedFault()) { //Successful response
Parameter ret = resp.getReturnValue();
String value = (String) ret.getValue();
if (value != null) {
out.println(
"<font face='Arial Narrow' size='2'>The resumé was submitted successfully!" +
"The unique identifier(UID) generated by the system is <b>" + value +
"</b></font><br>");
} else {
out.println(
"<font face='Arial Narrow' size='2'>The resumé was not submitted successfully
:-(</font><br>");
out.println(
"<font face='Arial Narrow' size='2'> [<a href='../servlet/retrieve?uid="
+ value + "'>View Resumé</a>]" + "[<a href='../submit.html'>Submit Another Resumé</a>] [<a href='../index.html'>Home</a>]</font>");
}
}
Unsuccessful Response
If the response does contain a fault, we can obtain the fault from the Response object by
using the getFault() method, which returns a Fault object. The Fault
object contains a getFaultCode() and getFaultString() method, which can be used
to obtain the fault code and the fault string, respectively.
else { //Unsuccessful response
Fault fault = resp.getFault();
out.println("Generated fault: ");
out.println (" Fault Code = " + fault.getFaultCode());
out.println (" Fault String = " + fault.getFaultString());
}
[previous] [next] |
Created: June 17, 2002
Revised: June 17, 2002
URL: http://webreference.com/programming/java/webservices/chap3/5/4.html

Find a programming school near you