background image

Retrieving the Order Confirmation

<< The addTextNode Method | SAAJ Service >>
<< The addTextNode Method | SAAJ Service >>

Retrieving the Order Confirmation

childName = new QName(
"price");
SOAPElement price = lineItem.addChildElement(childName);
price.addTextNode(lib.getPrice().toString());
}
// total
childName = new QName(
"total");
SOAPElement total = order.addChildElement(childName);
total.addTextNode(orderBean.getTotal().toString());
With the order complete, the application sends the message to the endpoint
http://localhost:8080/saaj-coffee-supplier/orderCoffee
and closes the connection.
URL endpoint = new URL(url);
SOAPMessage reply = con.call(msg, endpoint);
con.close();
Because the given endpoint is mapped to ConfirmationServlet, the Application Server
executes that servlet (discussed in
"Returning the Order Confirmation" on page 1044
) to create
and return the SOAPMessage object reply.
Retrieving the Order Confirmation
The rest of the placeOrder method retrieves the information returned in reply. The client
knows what elements are in it because they are specified in confirm.dtd. After accessing the
SOAPBody
object, the code retrieves the confirmation element and gets the text of the orderID
and ship-date elements. Finally, it constructs and returns a ConfirmationBean with this
information.
SOAPBody sBody = reply.getSOAPBody();
Iterator bodyIt = sBody.getChildElements();
SOAPBodyElement sbEl = (SOAPBodyElement)bodyIt.next();
Iterator bodyIt2 = sbEl.getChildElements();
SOAPElement ID = (SOAPElement)bodyIt2.next();
String id = ID.getValue();
SOAPElement sDate = (SOAPElement)bodyIt2.next();
String shippingDate = sDate.getValue();
SimpleDateFormat df = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy");
Date date = df.parse(shippingDate);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cb = new ConfirmationBean();
cb.setOrderId(id);
cb.setShippingDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(cal));
SAAJ Coffee Supplier Service
Chapter 36 · The Coffee Break Application
1039