background image

Extracting the Price List

<< Sending the Request | The SOAPElement Class >>
<< Sending the Request | The SOAPElement Class >>

Extracting the Price List

With the creation of the request message completed, the code sends the message to the SAAJ
coffee supplier. The message being sent is the SOAPMessage object msg, to which the elements
created in the previous code snippets were added. The endpoint is the URI for the SAAJ coffee
supplier, http://localhost:8080/saaj-coffee-supplier/getPriceList. The
SOAPConnection
object con is used to send the message, and because it is no longer needed, it is
closed.
URL endpoint = new URL(url);
SOAPMessage response = con.call(msg, endpoint);
con.close();
When the call method is executed, the Application Server executes the servlet
PriceListServlet
. This servlet creates and returns a SOAPMessage object whose content is the
SAAJ supplier's price list. (PriceListServlet is discussed in
"Returning the Price List" on
page 1040
.) The Application Server knows to execute PriceListServlet because the given
endpoint is mapped to that servlet.
Extracting the Price List
This section demonstrates (1) retrieving the price list that is contained in response, the
SOAPMessage
object returned by the method call, and (2) returning the price list as a
PriceListBean
.
The code creates an empty Vector object that will hold the coffee-name and price elements
that are extracted from response. Then the code uses response to access its SOAPBody object,
which holds the message's content.
Vector<String> list = new Vector<String>();
SOAPBody responseBody = response.getSOAPBody();
The next step is to retrieve the SOAPBodyElement object. The method getChildElements
returns an Iterator object that contains all the child elements of the element on which it is
called, so in the following lines of code, it1 contains the SOAPBodyElement object bodyEl,
which represents the price-list element.
Iterator it1 = responseBody.getChildElements();
while (it1.hasNext()) {
SOAPBodyElement bodyEl = (SOAPBodyElement)it1.next();
The Iterator object it2 holds the child elements of bodyEl, which represent coffee elements.
Calling the method next on it2 retrieves the first coffee element in bodyEl. As long as it2 has
another element, the method next will return the next coffee element.
Iterator it2 = bodyEl.getChildElements();
while (it2.hasNext()) {
SOAPElement child2 = (SOAPElement)it2.next();
SAAJ Coffee Supplier Service
Chapter 36 · The Coffee Break Application
1035