FileDocCategorySizeDatePackage
ProductClient.javaAPI DocExample3794Tue Oct 09 11:03:40 BST 2001com.ecerami.soap

ProductClient

public class ProductClient 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:productservice");
    call.setMethodName ("getProduct");

    //  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 f = resp.getFault();
      String faultCode = f.getFaultCode();
      String faultString = f.getFaultString();
      System.err.println("Fault Occurred (details follow):");
      System.err.println("Fault Code:  "+faultCode);
      System.err.println("Fault String:  "+faultString);
      return null;
    }
  
public static voidmain(java.lang.String[] args)
Static Main method

    String sku = args[0];
    System.out.println ("Product Checker:  SOAP Client");
    ProductClient productClient = new ProductClient();
    try {
      ProductBean product = productClient.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 (SOAPException e) {
      System.err.println (e);
    } catch (MalformedURLException e) {
      System.err.println (e);
    }