10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Ordering Coffee<br />

<strong>The</strong> other kind of message that the Coffee Break servers can send to the SAAJ supplier is an<br />

order for coffee. This is done in the placeOrder method of OrderRequest, which follows the<br />

DTD coffee-order.dtd.<br />

Creating the Order<br />

As with the client code for requesting a price list, the placeOrder method starts by creating a<br />

SOAPConnection object and a SOAPMessage object and accessing the message’s SOAPBody object.<br />

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();<br />

SOAPConnection con = scf.createConnection();<br />

MessageFactory mf = MessageFactory.newInstance();<br />

SOAPMessage msg = mf.createMessage();<br />

SOAPBody body = msg.getSOAPBody();<br />

Next, the code creates and adds XML elements to form the order. As is required, the first<br />

element is a SOAPBodyElement, which in this case is coffee-order.<br />

QName bodyName = new QName("http://sonata.coffeebreak.com",<br />

"coffee-order", "PO");<br />

SOAPBodyElement order = body.addBodyElement(bodyName);<br />

<strong>The</strong> application then adds the next level of elements, the first of these being orderID. <strong>The</strong> value<br />

given to orderID is extracted from the OrderBean object passed to the<br />

OrderRequest.placeOrder method.<br />

QName orderIDName = new QName("orderID");<br />

SOAPElement orderID = order.addChildElement(orderIDName);<br />

orderID.addTextNode(orderBean.getId());<br />

<strong>The</strong> next element, customer, has several child elements that give information about the<br />

customer. This information is also extracted from the Customer component of OrderBean.<br />

QName childName = new QName("customer");<br />

SOAPElement customer = order.addChildElement(childName);<br />

childName = new QName("last-name");<br />

SOAPElement lastName = customer.addChildElement(childName);<br />

lastName.addTextNode(orderBean.getCustomer().getLastName());<br />

childName = new QName("first-name");<br />

SOAPElement firstName = customer.addChildElement(childName);<br />

firstName.addTextNode(orderBean.getCustomer().getFirstName());<br />

SAAJ Coffee Supplier Service<br />

Chapter 36 • <strong>The</strong> Coffee Break Application 1029

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!