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.

}<br />

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

SOAPElement price = lineItem.addChildElement(childName);<br />

price.addTextNode(lib.getPrice().toString());<br />

// total<br />

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

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

total.addTextNode(orderBean.getTotal().toString());<br />

With the order complete, the application sends the message to the endpoint<br />

http://localhost:8080/saaj-coffee-supplier/orderCoffee and closes the connection.<br />

URL endpoint = new URL(url);<br />

SOAPMessage reply = con.call(msg, endpoint);<br />

con.close();<br />

Because the given endpoint is mapped to ConfirmationServlet, the Application Server<br />

executes that servlet (discussed in “Returning the Order Confirmation” on page 1036) to create<br />

and return the SOAPMessage object reply.<br />

Retrieving the Order Confirmation<br />

<strong>The</strong> rest of the placeOrder method retrieves the information returned in reply. <strong>The</strong> client<br />

knows what elements are in it because they are specified in confirm.dtd. After accessing the<br />

SOAPBody object, the code retrieves the confirmation element and gets the text of the orderID<br />

and ship-date elements. Finally, it constructs and returns a ConfirmationBean with this<br />

information.<br />

SOAPBody sBody = reply.getSOAPBody();<br />

Iterator bodyIt = sBody.getChildElements();<br />

SOAPBodyElement sbEl = (SOAPBodyElement)bodyIt.next();<br />

Iterator bodyIt2 = sbEl.getChildElements();<br />

SOAPElement ID = (SOAPElement)bodyIt2.next();<br />

String id = ID.getValue();<br />

SOAPElement sDate = (SOAPElement)bodyIt2.next();<br />

String shippingDate = sDate.getValue();<br />

SAAJ Coffee Supplier Service<br />

SimpleDateFormat df = new SimpleDateFormat("<strong>EE</strong>E MMM dd HH:mm:ss z yyyy");<br />

Date date = df.parse(shippingDate);<br />

GregorianCalendar cal = new GregorianCalendar();<br />

cal.setTime(date);<br />

cb = new ConfirmationBean();<br />

cb.setOrderId(id);<br />

cb.setShippingDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(cal));<br />

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

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

Saved successfully!

Ooh no, something went wrong!