OrderProcessorpublic 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 | registryMapping for CD class | private org.apache.soap.encoding.soapenc.BeanSerializer | serializerThe serializer for the CD class | private org.apache.soap.rpc.Call | callThe RPC Call object | private Vector | paramsParameters for call | private org.apache.soap.rpc.Response | rpcResponseResponse from RPC call | private URL | rpcServerURLThe URL to connect to |
Methods Summary |
---|
public void | initialize()
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 void | purchaseOrder(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.
// 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");
|
|