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 />

}<br />

else {<br />

StringBuffer concat = new StringBuffer();<br />

inti=0;<br />

while (i < values.length) {<br />

if (i != 0) {<br />

concat.append(’,’);<br />

}<br />

concat.append(values[i++]);<br />

}<br />

res.setHeader(header.getName(), concat.toString());<br />

}<br />

<strong>The</strong> method onMessage is the application code for responding to the message sent by<br />

PriceListRequest and internalized into msg. It uses the static MessageFactory object<br />

messageFactory to create the SOAPMessage object message and then populates it with the<br />

supplier’s current coffee prices.<br />

<strong>The</strong> method doPost invokes onMessage and passes it msg. In this case, onMessage does not need<br />

to use msg because it simply creates a message containing the supplier’s price list. <strong>The</strong><br />

onMessage method in ConfirmationServlet (see “Returning the Order Confirmation” on<br />

page 1036), on the other hand, uses the message passed to it to get the order ID.<br />

public SOAPMessage onMessage(SOAPMessage msg) {<br />

SOAPMessage message = null;<br />

try {<br />

message = messageFactory.createMessage();<br />

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

QName bodyName =<br />

new QName("http://sonata.coffeebreak.com", "price-list", "PriceList");<br />

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

QName coffeeN = new QName("coffee");<br />

SOAPElement coffee = list.addChildElement(coffeeN);<br />

QName coffeeNm1 = new QName("coffee-name");<br />

SOAPElement coffeeName = coffee.addChildElement(coffeeNm1);<br />

coffeeName.addTextNode("Arabica");<br />

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

SOAPElement price1 = coffee.addChildElement(priceName1);<br />

price1.addTextNode("4.50");<br />

QName coffeeNm2 = new QName("coffee-name");<br />

SAAJ Coffee Supplier Service<br />

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

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

Saved successfully!

Ooh no, something went wrong!