javax.xml.messaging
abstract public class: JAXMServlet [javadoc |
source]
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
javax.xml.messaging.JAXMServlet
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
The superclass for components that
live in a servlet container that receives JAXM messages.
A
JAXMServlet
object is notified of a message's arrival
using the HTTP-SOAP binding.
The JAXMServlet
class is a support/utility class and is
provided purely as a convenience. It is not a mandatory component, and
there is no requirement that it be implemented or extended.
Note that when a component that receives messages extends
JAXMServlet
, it also needs to implement either a
ReqRespListener
object or a
OnewayListener
object,
depending on whether the component is written for a request-response
style of interaction or for a one-way (asynchronous) style of interaction.
Field Summary |
---|
protected MessageFactory | msgFactory | The MessageFactory object that will be used internally
to create the SOAPMessage object to be passed to the
method onMessage . This new message will contain the data
from the message that was posted to the servlet. Using the
MessageFactory object that is the value for this field
to create the new message ensures that the correct profile is used. |
Methods from javax.servlet.http.HttpServlet: |
---|
class$, doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service |
Methods from javax.servlet.GenericServlet: |
---|
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log, service |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from javax.xml.messaging.JAXMServlet Detail: |
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 a HTTP request and
// create a SOAPMessage
SOAPMessage msg = msgFactory.createMessage(headers, is);
SOAPMessage reply = null;
// There are no replies in case of an OnewayListener.
if (this instanceof ReqRespListener)
reply = ((ReqRespListener) this).onMessage(msg);
else if (this instanceof OnewayListener)
((OnewayListener) this).onMessage(msg);
else {
String lstnrMsg = AdministeredObject.cr.getKString(AdministeredObject.cr.X_NO_JAXMSERVLET_LISTENER,
this.getClass().getName());
throw new ServletException(lstnrMsg);
//throw new ServletException("JAXM component: "
//+this.getClass().getName()+" also has to"+
//" implement ReqRespListener or OnewayListener");
}
if (reply != null) {
// Need to saveChanges 'cos 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.
OutputStream os = resp.getOutputStream();
reply.writeTo(os);
os.flush();
} else
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
catch(Exception ex) {
String postMsg = AdministeredObject.cr.getKString(AdministeredObject.cr.X_JAXM_POST_FAILED);
throw new ServletException(postMsg + "\n" + ex.getMessage());
//throw new ServletException("JAXM POST failed "+ex.getMessage());
}
}
Internalizes the given HttpServletRequest object
and writes the reply to the given HttpServletResponse
object.
Note that the value for the msgFactory field will be used to
internalize the message. This ensures that the message
factory for the correct profile is used. |
protected static MimeHeaders getHeaders(HttpServletRequest req) {
Enumeration enm = req.getHeaderNames();
MimeHeaders headers = new MimeHeaders();
while (enm.hasMoreElements()) {
String headerName = (String)enm.nextElement();
String headerValue = req.getHeader(headerName);
StringTokenizer values = new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens())
headers.addHeader(headerName, values.nextToken().trim());
}
return headers;
}
Returns a MimeHeaders object that contains the headers
in the given HttpServletRequest object. |
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
try {
// Initialize it to the default.
msgFactory = MessageFactory.newInstance();
} catch (SOAPException ex) {
String fctryMsg = AdministeredObject.cr.getKString(AdministeredObject.cr.X_MESSAGEFACTORY_ERROR);
throw new ServletException(fctryMsg + "\n" + ex.getMessage());
//throw new ServletException("Unable to create message factory"+ex.getMessage());
}
}
Initializes this JAXMServlet object using the given
ServletConfig object and initializing the
msgFactory field with a default
MessageFactory object. |
protected static void putHeaders(MimeHeaders headers,
HttpServletResponse res) {
Iterator it = headers.getAllHeaders();
while (it.hasNext()) {
MimeHeader header = (MimeHeader)it.next();
String[] values = headers.getHeader(header.getName());
if (values.length == 1)
res.setHeader(header.getName(), header.getValue());
else
{
StringBuffer concat = new StringBuffer();
int i = 0;
while (i < values.length) {
if (i != 0)
concat.append(',');
concat.append(values[i++]);
}
res.setHeader(header.getName(),
concat.toString());
}
}
}
Sets the given HttpServletResponse object with the
headers in the given MimeHeaders object. |
public void setMessageFactory(MessageFactory msgFactory) {
this.msgFactory = msgFactory;
}
Sets this JAXMServlet object's msgFactory
field with the given MessageFactory object.
A MessageFactory object for a particular profile needs to
be set before a message is received in order for the message to be
successfully internalized. |