|
Using the Java[tm] Platform APIs for XML Messaging (JAXM)
by Michelle Cope We want to hear from you! Please send us your FEEDBACK. The following article may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license.
Simple Object Access Protocol (SOAP) messages are used to exchange XML-encoded
information in a distributed environment. SOAP messages are often used to
implement a request/response messaging topology. For example, in a purchasing
application for computer hardware, a client can send a SOAP encoded message
to request the purchase of a keyboard to a SOAP HTTP server. The SOAP server
responds with a SOAP message. If the request was successful then the response
SOAP message contains an acknowledgement. Otherwise, it contains error information.
According to the SOAP 1.1 specification, errors or status code information
must be conveyed as SOAP faults in SOAP messages. SOAP faults are generated
in response to a request message that could not be handled. A SOAP message
is usually not processed for two main reasons: the SOAP message did not comply
with the SOAP specification or its application specific content was incomplete
or incorrectly formed.
Section 4.4: SOAP Faults of the SOAP 1.1 Specification
describes, in depth, the SOAP fault structure and the criteria for SOAP
fault generation in a SOAP message. It is advisable that the reader familiarize
themselves with this document before continuing. SOAP Messages with SOAP
faults can be constructed and handled using JAXM. This article will describe
how to use JAXM to send SOAP faults in SOAP messages over HTTP, with reference
to the purchase application described. <SOAP-ENV:Envelope
xlmns:SOAP-ENV="http://schema.xmlsoap.org/soap/envelope/"> public SOAPMessage onMessage(SOAPMessage msg){
The reply SOAP message now has a SOAPFault element, next the HTTP status code of 500 needs to be set. The setting of a HTTP status code is done by the utility class, javax.xml.messaging.JAXMServlet. JAXMServlet can be extended by any servlet component to receive SOAP messages. A servlet component that extends JAXMServlet must also implement either the javax.xml.messaging.ReqRespListener or the javax.xml.messaging.OnewayListener interface. The onMessage(SOAPMessage msg) method that both interfaces declare is invoked by the doPost() method of JAXMServlet to pass the SOAP message to the servlet component. The onMessage(SOAPMessage msg) declarations in the two interfaces differ: only the method in the ReqRespListener interface returns a SOAPMessage. A servlet component that extends JAXMSevlet handles a SOAP message according to the HTTP-SOAP binding specification. A SOAP message is encoded in an HTTP post request to a JAXMServlet object. The doPost(HttpServletRequest, HttpServletResponse) method of the JAXM Servlet extracts the encoded SOAP message from the HTTPServletRequest object; invokes the onMessage(SOAPMessage msg) method to handle the SOAP message, and encodes a response SOAP message, if any, on the HttpServletResponse request. By default JAXMServlet assumes that the response SOAP message is error free and sends an HTTP status code of the format 2xx e.g. 200. In order to change this behaviour the doPost()method of JAXMServlet is overridden. The following code extract is in the doPost() method from ReceivingServlet.java. ReceivingServlet extends JAXMServlet class, and implements the ReqRespListener interface. It is broken into three main sections: the extraction of the SOAP message from the HttpServletRequest object ; the handling of the SOAP message, and the decoding of the SOAP message onto the HttpServletResponse object. 1. The extraction of the SOAP message from the HttpServletRequest object. ...Conceptually, a SOAP message consists of two main parts: the payload of the SOAP message, and the set of MIME headers that wrap the payload. Both parts are mapped into the HTTP request object: the MIME headers are placed as HTTP request parameters, and the payload is placed in the body of the HTTP request. Both parts are extracted separately from the HTTPServletRequest object, but both are required to reconstruct the request SOAPMessage using the createMessage(MimeHeaders, InputStream) method of the javax.xml.soap.MessageFactory class. 2. The handling of the SOAP message. The onMessage(SOAPMessage msg) method of ReqRespListener is invoked, passing in the request SOAPMessage. The onMessage(SOAPMessage msg) implementation is that describe above. A SOAPFault element is added to the message. A reply SOAPMessage returned by onMessage(SOAPMessage msg) is then checked for the presence of a SOAPFault using the utility method, containsFault(SOAPMessage). If the SOAPMessage does include a soap fault, then the status code of the HTTPServletResponse object is set to 500, otherwise the response is set to 200. 3. The decoding and sending of the reply SOAP message. The response SOAP message is then broken into its payload and MIME headers which are then mapped onto the HTTP response body, and HTTP response parameters respectively. The response SOAP message is then sent back to the client who sent the erroneous request. The client will display an appropriate message indicating the success of its request. The code is relatively simple. It could easily be expanded to handle the different setting of different HTTP status codes and the handling of an object implementing the OnewayListener as well as the ReqRespListener interface. The entire code is packaged in the soapfaultExample.war file. It contains two main Java programming language files: SendingServlet.java , which sends the client's request to purchase a keyboard, and ReceivingServlet.java which acts as the SOAP server. It can be deployed on TOMCAT 4.0 using the latest Java[tm] platform XML Spring Pack release which has JAXM version 1.0.1-ea2. The Java platform XML Spring pack also includes instructions on how to use JAXM with TOMCAT. These instructions need to be followed if the code is to work. The code will not work with any prior JAXM version because the provided JAXM implementation could not handle a HTTP status code of 500. | |||||||||||||||||||||||||||||||||||||||