FileDocCategorySizeDatePackage
CDAdder.javaAPI DocExample5696Wed Sep 19 09:29:42 BST 2001javaxml2

CDAdder

public class CDAdder extends Object

CDAdder is a web services client. It connects to the cd-catalog-demo service and adds a new CD.

Fields Summary
Constructors Summary
Methods Summary
public voidadd(java.net.URL url, java.lang.String title, java.lang.String artist, java.lang.String label)

This will connect to the provided URL and attempt to use the cd-catalog-demo web service to add a new {@link CD}, using the supplied information about the CD.

param
url URL where web service is running.
param
title the title of the CD to add.
param
artist the artist of the CD to add.
param
label the label of the CD to add.
throws
SOAPException - when errors in accessing the web service occur.


        System.out.println("Adding CD titled '" + title + "' by '" +
            artist + "', on the label " + label);

        CD cd = new CD(title, artist, label);

        // Map this type so SOAP can use it
        SOAPMappingRegistry registry = new SOAPMappingRegistry();
        BeanSerializer serializer = new BeanSerializer();
        registry.mapTypes(Constants.NS_URI_SOAP_ENC,
            new QName("urn:cd-catalog-demo", "cd"),
            CD.class, serializer, serializer);   

        // Build the Call object
        Call call = new Call();
        call.setSOAPMappingRegistry(registry);
        call.setTargetObjectURI("urn:cd-catalog");
        call.setMethodName("addCD");
        call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

        // Set up parameters
        Vector params = new Vector();
        params.addElement(new Parameter("cd", CD.class, cd, null));
        call.setParams(params);

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

        if (!response.generatedFault()) {
            System.out.println("Successful CD Addition.");
        } else {
            Fault fault = response.getFault();
            System.out.println("Error encountered: " + fault.getFaultString());

            Vector entries = fault.getDetailEntries();
            for (Iterator i = entries.iterator(); i.hasNext(); ) {
                org.w3c.dom.Element entry = (org.w3c.dom.Element)i.next();
                System.out.println(entry.getFirstChild().getNodeValue());
            }
        }
    
public static voidmain(java.lang.String[] args)

Provide a static entry point.

        if (args.length != 4) {
            System.out.println("Usage: java javaxml2.CDAdder [SOAP server URL] " +
                "\"[CD Title]\" \"[Artist Name]\" \"[CD Label]\"");
            return;
        }

        try {
            // URL for SOAP server to connect to
            URL url = new URL(args[0]);

            // Get values for new CD
            String title = args[1];
            String artist = args[2];
            String label = args[3];

            // Add the CD
            CDAdder adder = new CDAdder();
            adder.add(url, title, artist, label);
        } catch (Exception e) {
            e.printStackTrace();
        }