FileDocCategorySizeDatePackage
ProductClient2.javaAPI DocExample4423Tue Oct 09 11:03:40 BST 2001com.ecerami.soap

ProductClient2

public class ProductClient2 extends Object
A Sample SOAP Client Retrieves Product Info for Specified Stockkeeping Unit (SKU) usage: java ProductClient sku#

Fields Summary
Constructors Summary
Methods Summary
public ProductBeangetProduct(java.lang.String sku)
getProduct 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:productservice2");
    call.setMethodName ("getProductInfo");

    //  Add JavaXML Mapping for Product Bean
    //  First, Create a Bean Serializer
    BeanSerializer bSerializer = new BeanSerializer();

    //  Second, Get the current SOAPMappingRegistry
    //  This object is pre-registered with basic mappings
    SOAPMappingRegistry registry = new SOAPMappingRegistry();

    //  Third, Create an new Qualified Namespace for Product Bean
    QName qname = new QName ("urn:examples", "product");

    //  Fourth, Register new mapping for ProductBean
    registry.mapTypes (Constants.NS_URI_SOAP_ENC, qname,
      com.ecerami.soap.ProductBean.class, bSerializer, bSerializer);

    //  Fifth, Set MappingRegistry for the Call object
    call.setSOAPMappingRegistry(registry);

    //  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, null);

    // Check for Success
    if (!resp.generatedFault()) {
      // Extract Return value
      Parameter result = resp.getReturnValue ();
      ProductBean product = (ProductBean) result.getValue();
      return product;
    }
    //  Check for Faults
    else {
      //  Extract Fault Code and String
      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 ("Product Checker:  SOAP Client");
    ProductClient2 productClient2 = new ProductClient2();
    try {
      ProductBean product = productClient2.getProduct (sku);
      System.out.println ("SKU:  "+product.getSKU());
      System.out.println ("Name:  "+product.getName());
      System.out.println ("Description:  "+product.getDescription());
      System.out.println ("Price:  "+product.getPrice());
    } 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(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);
      }
    }