Client Code

The revised client code is presented in Example 5-10. Most of the getProduct( ) method is the same as in the original ProductClient code, and therefore I have only included those pieces that illustrate the new fault-handling capability. First, if a fault is detected, the client code extracts the Fault object and embeds it into a ProductNotFoundException:

Fault fault = resp.getFault(  );
String faultString = fault.getFaultString(  );
throw new ProductNotFoundException (faultString, fault);

This enables us to examine the Fault object later. Second, the main( ) method includes a new printFaultDetails( ) method for printing out any fault details embedded in the Response object. Before we examine this method, however, we’ll test the code by sending a nonexistent SKU. Let’s try the SKU number Z358185. If you are using the TcpTunnelGui tool, you will see the following response from the SOAP server:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope 
                     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                     xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
                     xmlns:xsd="http://www.w3.org/1999/XMLSchema">
                     <SOAP-ENV:Body>
                        <SOAP-ENV:Fault>
                           <faultcode>SOAP-ENV:Server</faultcode>
                           <faultstring>
                              Exception from service object: SKU Not Found:  Z358185
                           </faultstring>
                           <faultactor>/soap/servlet/rpcrouter</faultactor>
                        </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

As you can see, ProductNotFoundException is not propagated directly back to the client. Rather, the rpcrouter ...

Get Web Services Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.