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.

In the following code fragment, doPost calls the methods getHeaders and putHeaders, defined<br />

immediately after doPost, to read and write the headers in req. It then gets the content of req as<br />

a stream and passes the headers and the input stream to the method<br />

MessageFactory.createMessage. <strong>The</strong> result is that the SOAPMessage object msg contains the<br />

request for a price list. Note that in this case, msg does not have any headers because the message<br />

sent in PriceListRequest did not have any headers.<br />

public void doPost(HttpServletRequest req, HttpServletResponse resp)<br />

throws ServletException, IOException {<br />

try {<br />

// Get all the headers from the HTTP request<br />

MimeHeaders headers = getHeaders(req);<br />

// Get the body of the HTTP request<br />

InputStream is = req.getInputStream();<br />

// Now internalize the contents of the HTTP request<br />

// and create a SOAPMessage<br />

SOAPMessage msg = messageFactory.createMessage(headers, is);<br />

Next, the code declares the SOAPMessage object reply and populates it by calling the method<br />

onMessage.<br />

SOAPMessage reply = null;<br />

reply = onMessage(msg);<br />

If reply has anything in it, its contents are saved, the status of resp is set to OK, and the headers<br />

and content of reply are written to resp.Ifreply is empty, the status of resp is set to indicate<br />

that there is no content.<br />

if (reply != null) {<br />

/*<br />

* Need to call saveChanges because we’re<br />

* going to use the MimeHeaders to set HTTP<br />

* response information. <strong>The</strong>se MimeHeaders<br />

* are generated as part of the save.<br />

*/<br />

if (reply.saveRequired()) {<br />

reply.saveChanges();<br />

}<br />

resp.setStatus(HttpServletResponse.SC_OK);<br />

putHeaders(reply.getMimeHeaders(), resp);<br />

// Write out the message on the response stream<br />

logger.info("Reply message:");<br />

SAAJ Coffee Supplier Service<br />

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

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

Saved successfully!

Ooh no, something went wrong!