Wednesday, September 21, 2011

Obtaining HTTP Request context in Axis2 WebServices


        Recently I integrated REST (Representational State Transfer) and SOAP (Simple Object Access Protocol) WebServices (WS) into a single context. i.e both the service end points were mapped to single context path. The REST WS was a simple authentication servlet application and SOAP WS was a AAR (Axis Archive file) file generated via AXIS2 plugin and deployed with Apache AXIS2 support. Also some of the stateful information from REST service needed to be obtained in SOAP service. But how? SOAP WS is a simple class with some methods exposed as service using Axis framework. Then how can I obtain the details from the other services deployed under same context path.

I was thinking for a solution. I realized that if it is same context then the Servlet Request object can be shared across. So, if values are set in session then it can be transformed across the application with state maintained.

Now, how to get the Session object in a plain class of SOAP WS. After looking into Axis API and surfing across the google I got some implementation to get HTTPServletRequest object.

Here's the implementation :

import org.apache.axis2.context.MessageContext;



MessageContext context =
       MessageContext.getCurrentMessageContext();
HttpServletRequest req = (HttpServletRequest)
       context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
HttpSession session = req.getSession();


What is MessageContext object?

I am sharing the info obtained from Axis2 API
       MessageContext creates an object for every message, regardless of whether it is from the client or server, processed within Axis2. First, the transport-related information is fed into the object, followed by the object model. This message context is then passed into each handler that is registered to process this type of message. Since the Axis2 information processing model is stateless, the context hierarchy is the only place that can be used to store state. Additionally, if the context information is relevant to this particular message being processed, it should be stored within message context.

- Shafeeq Mohammed

3 comments:

Unknown said...

Thanks for the contribution of post and help on this subject.

data recovery disk

Richard Majece said...

As for me, this useful information will help you to write high quality essay. It was helpful for me and my friends

kiwis said...

Thanks for sharing that with us. Really appreicated.
filmplus apk