background image

The SOAPMessage Object

<< SAAJ Service | The MimeHeaders Object >>
<< SAAJ Service | The MimeHeaders Object >>

The SOAPMessage Object

In the following code fragment, doPost calls the methods getHeaders and putHeaders, defined
immediately after doPost, to read and write the headers in req. It then gets the content of req as
a stream and passes the headers and the input stream to the method
MessageFactory.createMessage
. The result is that the SOAPMessage object msg contains the
request for a price list. Note that in this case, msg does not have any headers because the message
sent in PriceListRequest did not have any headers.
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
// Get all the headers from the HTTP request
MimeHeaders headers = getHeaders(req);
// Get the body of the HTTP request
InputStream is = req.getInputStream();
// Now internalize the contents of the HTTP request
// and create a SOAPMessage
SOAPMessage msg = messageFactory.createMessage(headers, is);
Next, the code declares the SOAPMessage object reply and populates it by calling the method
onMessage
.
SOAPMessage reply = null;
reply = onMessage(msg);
If reply has anything in it, its contents are saved, the status of resp is set to OK, and the headers
and content of reply are written to resp. If reply is empty, the status of resp is set to indicate
that there is no content.
if (reply != null) {
/*
* Need to call saveChanges because we
're
* going to use the MimeHeaders to set HTTP
* response information. These MimeHeaders
* are generated as part of the save.
*/
if (reply.saveRequired()) {
reply.saveChanges();
}
resp.setStatus(HttpServletResponse.SC_OK);
putHeaders(reply.getMimeHeaders(), resp);
// Write out the message on the response stream
logger.info(
"Reply message:");
SAAJ Coffee Supplier Service
Chapter 36 · The Coffee Break Application
1041