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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

With the creation of the request message completed, the code sends the message to the SAAJ<br />

coffee supplier. <strong>The</strong> message being sent is the SOAPMessage object msg, to which the elements<br />

created in the previous code snippets were added. <strong>The</strong> endpoint is the URI for the SAAJ coffee<br />

supplier, http://localhost:8080/saaj-coffee-supplier/getPriceList. <strong>The</strong><br />

SOAPConnection object con is used to send the message, and because it is no longer needed, it is<br />

closed.<br />

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

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

con.close();<br />

When the call method is executed, the Application Server executes the servlet<br />

PriceListServlet. This servlet creates and returns a SOAPMessage object whose content is the<br />

SAAJ supplier’s price list. (PriceListServlet is discussed in “Returning the Price List” on<br />

page 1032.) <strong>The</strong> Application Server knows to execute PriceListServlet because the given<br />

endpoint is mapped to that servlet.<br />

Extracting the Price List<br />

This section demonstrates (1) retrieving the price list that is contained in response, the<br />

SOAPMessage object returned by the method call, and (2) returning the price list as a<br />

PriceListBean.<br />

<strong>The</strong> code creates an empty Vector object that will hold the coffee-name and price elements<br />

that are extracted from response. <strong>The</strong>n the code uses response to access its SOAPBody object,<br />

which holds the message’s content.<br />

Vector list = new Vector();<br />

SOAPBody responseBody = response.getSOAPBody();<br />

<strong>The</strong> next step is to retrieve the SOAPBodyElement object. <strong>The</strong> method getChildElements<br />

returns an Iterator object that contains all the child elements of the element on which it is<br />

called, so in the following lines of code, it1 contains the SOAPBodyElement object bodyEl,<br />

which represents the price-list element.<br />

Iterator it1 = responseBody.getChildElements();<br />

while (it1.hasNext()) {<br />

SOAPBodyElement bodyEl = (SOAPBodyElement)it1.next();<br />

<strong>The</strong> Iterator object it2 holds the child elements of bodyEl, which represent coffee elements.<br />

Calling the method next on it2 retrieves the first coffee element in bodyEl. As long as it2 has<br />

another element, the method next will return the next coffee element.<br />

Iterator it2 = bodyEl.getChildElements();<br />

while (it2.hasNext()) {<br />

SOAPElement child2 = (SOAPElement)it2.next();<br />

SAAJ Coffee Supplier Service<br />

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

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

Saved successfully!

Ooh no, something went wrong!