background image

Ordering Coffee

<< The SOAPElement Class | The addTextNode Method >>
<< The SOAPElement Class | The addTextNode Method >>

Ordering Coffee

Ordering Coffee
The other kind of message that the Coffee Break servers can send to the SAAJ supplier is an
order for coffee. This is done in the placeOrder method of OrderRequest, which follows the
DTD coffee-order.dtd.
Creating the Order
As with the client code for requesting a price list, the placeOrder method starts by creating a
SOAPConnection
object and a SOAPMessage object and accessing the message's SOAPBody object.
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection con = scf.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPBody body = msg.getSOAPBody();
Next, the code creates and adds XML elements to form the order. As is required, the first
element is a SOAPBodyElement, which in this case is coffee-order.
QName bodyName = new QName(
"http://sonata.coffeebreak.com",
"coffee-order", "PO");
SOAPBodyElement order = body.addBodyElement(bodyName);
The application then adds the next level of elements, the first of these being orderID. The value
given to orderID is extracted from the OrderBean object passed to the
OrderRequest
.placeOrder method.
QName orderIDName = new QName(
"orderID");
SOAPElement orderID = order.addChildElement(orderIDName);
orderID.addTextNode(orderBean.getId());
The next element, customer, has several child elements that give information about the
customer. This information is also extracted from the Customer component of OrderBean.
QName childName = new QName(
"customer");
SOAPElement customer = order.addChildElement(childName);
childName = new QName(
"last-name");
SOAPElement lastName = customer.addChildElement(childName);
lastName.addTextNode(orderBean.getCustomer().getLastName());
childName = new QName(
"first-name");
SOAPElement firstName = customer.addChildElement(childName);
firstName.addTextNode(orderBean.getCustomer().getFirstName());
SAAJ Coffee Supplier Service
Chapter 36 · The Coffee Break Application
1037