FileDocCategorySizeDatePackage
PriceClient.javaAPI DocExample3611Tue Oct 09 11:03:40 BST 2001com.ecerami.soap

PriceClient

public class PriceClient extends Object
A Sample SOAP Client Retrieves Current Price for Specified Stockkeeping Unit (SKU) Program illustrates different fault handling techniques. usage: java PriceClient sku#

Fields Summary
Constructors Summary
Methods Summary
public doublegetPrice(java.lang.String sku)
getPrice Method

    Parameter skuParam;

    //  Create SOAP RPC Call Object
    Call call = new Call ();

    // Set Encoding Style to standard SOAP encoding
    call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

    // Set Object URI and Method Name
    call.setTargetObjectURI ("urn:examples:priceservice");
    call.setMethodName ("getPrice");

    //  Set Method Parameters
    Vector paramList = new Vector ();
    skuParam = new Parameter("sku", String.class,
      sku, Constants.NS_URI_SOAP_ENC);
    paramList.addElement (skuParam);

    call.setParams (paramList);

    //  Set the URL for the Web Service
    URL url = new URL ("http://localhost:8080/soap/servlet/rpcrouter");

    // Invoke the Service
    Response resp = call.invoke (url, "");

    // Check for Success
    // Note that a Fault will not trigger a SOAPException
    if (!resp.generatedFault()) {
      // Extract Return value
      Parameter result = resp.getReturnValue ();
      Double price = (Double) result.getValue();
      return price.doubleValue();
    }
    //  Check for Faults
    else {
        //  Extract Fault Code and throw new Exception
        Fault fault = resp.getFault();
        String faultString = fault.getFaultString();
        throw new ProductNotFoundException (faultString, fault);
    }
  
public static voidmain(java.lang.String[] args)
Static Main method

    String sku = args[0];
    System.out.println ("Price Checker:  SOAP Client");
    PriceClient priceClient = new PriceClient();
    try {
      double price = priceClient.getPrice(sku);
      System.out.println ("SKU:  "+sku+" -->  "+price);
    } catch (ProductNotFoundException e) {
      System.err.println (e);
      printFaultDetails (e.getFault());
    } catch (SOAPException e) {
      System.err.println (e);
    } catch (MalformedURLException e) {
      System.err.println (e);
    }
  
public static voidprintFaultDetails(org.apache.soap.Fault fault)
Extract and Print Fault Details

    // Extract Detail Entries
    Vector detailEntries = fault.getDetailEntries();
    if (detailEntries != null) {
      // Print each Detail Entry
     for (int i=0; i< detailEntries.size(); i++) {
        Element detail = (Element) detailEntries.elementAt(i);
        String name = detail.getNodeName();
        String value = DOMUtils.getChildCharacterData(detail);
        System.err.println (name);
        System.err.println (value);
      }
    }