FileDocCategorySizeDatePackage
OrderProcessor.javaAPI DocExample9709Wed Sep 19 09:41:36 BST 2001javaxml2

OrderProcessor

public class OrderProcessor extends Object

OrderProcessor is a web services client, and uses SOAP-RPC to interact with web services. However, it is also a web service, and receives SOAP messages and uses them to then construct SOAP-RPC calls, as well as sending return messages to its own web service clients.

Fields Summary
private org.apache.soap.encoding.SOAPMappingRegistry
registry
Mapping for CD class
private org.apache.soap.encoding.soapenc.BeanSerializer
serializer
The serializer for the CD class
private org.apache.soap.rpc.Call
call
The RPC Call object
private Vector
params
Parameters for call
private org.apache.soap.rpc.Response
rpcResponse
Response from RPC call
private URL
rpcServerURL
The URL to connect to
Constructors Summary
Methods Summary
public voidinitialize()

This will set up initial default values.

        // Set up internal URL for SOAP-RPC
        try {
            rpcServerURL = 
                new URL("http://localhost:8080/soap/servlet/rpcrouter");
        } catch (MalformedURLException neverHappens) {
            // ignored
        }

        // Set up a SOAP mapping to translate CD objects
        registry = new SOAPMappingRegistry();
        serializer = new BeanSerializer();
        registry.mapTypes(Constants.NS_URI_SOAP_ENC,
            new QName("urn:cd-catalog-demo", "cd"),
            CD.class, serializer, serializer); 

        // Build a Call to the internal SOAP service
        call = new Call();        
        call.setSOAPMappingRegistry(registry);
        call.setTargetObjectURI("urn:cd-catalog");
        call.setMethodName("getCD");
        call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

        // Set up input
        params = new Vector();
    
public voidpurchaseOrder(org.apache.soap.Envelope env, org.apache.soap.rpc.SOAPContext req, org.apache.soap.rpc.SOAPContext res)

This method receives SOAP messages addressed to this web service, decodes those messages, and sends them to another web service via SOAP-RPC.

param
env the SOAP envelope carrying the SOAP message.
param
req the SOAP request variable.
param
res the SOAP response variable.
throws
IOException - when errors result in connecting to the SOAP-RPC service.
throws
MessagingException - when errors occur in sending and receiving SOAP messages.


        // Set up SOAP environment
        initialize();

        // Set up list of CDs successfully ordered
        List orderedCDs = new LinkedList();

        // Set up hashtable of failed orders
        Hashtable failedCDs = new Hashtable();

        // Get the purchaseOrder element - always the first body entry
        Vector bodyEntries = env.getBody().getBodyEntries();
        Element purchaseOrder = (Element)bodyEntries.iterator().next();

        // In a real application, do something with the buyer information
 
        // Get the CDs ordered
        Element order = 
            (Element)purchaseOrder.getElementsByTagName("order").item(0);
        NodeList cds = order.getElementsByTagName("cd");

        // Loop through each ordered CD from the PO request
        for (int i=0, len=cds.getLength(); i<len; i++) {
            Element cdElement = (Element)cds.item(i);
            String artist = cdElement.getAttribute("artist");
            String title = cdElement.getAttribute("title");

            // Set up input
            params.clear();
            params.addElement(new Parameter("title", String.class, title, null));
            call.setParams(params);

            try {  
                // Invoke the call
                rpcResponse = call.invoke(rpcServerURL, "");

                if (!rpcResponse.generatedFault()) {
                    Parameter returnValue = rpcResponse.getReturnValue();
                    CD cd = (CD)returnValue.getValue();

                    // See if the CD is available
                    if (cd == null) {
                        failedCDs.put(title, "Requested CD is not available.");
                        continue;
                    }
 
                    // Verify it's by the right artist
                    if (cd.getArtist().equalsIgnoreCase(artist)) {
                        // Add this CD to the successful orders
                        orderedCDs.add(cd); 
                    } else {
                        // Add this to the failed orders
                        failedCDs.put(title, "Incorrect artist for specified CD.");
                    }
                } else {
                    Fault fault = rpcResponse.getFault();
                    failedCDs.put(title, fault.getFaultString());
                }
            } catch (SOAPException e) {
                failedCDs.put(title, "SOAP Exception: " + e.getMessage());
            }
        }

        // At the end of the loop, return something useful to the client
        Document doc = new org.apache.xerces.dom.DocumentImpl();
        Element response = doc.createElement("response");
        Element orderedCDsElement = doc.createElement("orderedCDs");
        Element failedCDsElement = doc.createElement("failedCDs");
        response.appendChild(orderedCDsElement);
        response.appendChild(failedCDsElement);

        // Add the ordered CDs
        for (Iterator i = orderedCDs.iterator(); i.hasNext(); ) {
            CD orderedCD = (CD)i.next();
            Element cdElement = doc.createElement("orderedCD");
            cdElement.setAttribute("title", orderedCD.getTitle());
            cdElement.setAttribute("artist", orderedCD.getArtist());
            cdElement.setAttribute("label", orderedCD.getLabel());
            orderedCDsElement.appendChild(cdElement);
        }

        // Add the failed CDs
        Enumeration keys = failedCDs.keys();
        while (keys.hasMoreElements()) {
            String title = (String)keys.nextElement();
            String error = (String)failedCDs.get(title);
            Element failedElement = doc.createElement("failedCD");
            failedElement.setAttribute("title", title);
            failedElement.appendChild(doc.createTextNode(error));
            failedCDsElement.appendChild(failedElement);
        }

        // Set this as the content of the envelope
        bodyEntries.clear();
        bodyEntries.add(response);
        StringWriter writer = new StringWriter();
        env.marshall(writer, null);

        // Send the envelope back to the client
        res.setRootPart(writer.toString(), "text/xml");