Sunday, March 11, 2007

Axis, SOAP and Stateful Session

Axis supports two ways of managing sessions: SOAP header and HTTP cookie. Here we explain how to manage session with SOAP header.
Axis comes a handler called org.apache.axis.handlers.SimpleSessionHandler that provides SOAP header based session management. To use the simple session handler with the Web service, you have to tell the Axis framework to add the handler into the handler chain. To do this you can define a WSDD file that contains the extra configuration that your Web service will use and then deploy the configuration by using the Axis deployment tool.
The WSDD file should be like this (e.g. deploy.wsdd):

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<handler name="session" type="java:org.apache.axis.handlers.SimpleSessionHandler"/>
<service name="Sessions" provider="java:RPC" style="wrapped">
<namespace>your name space</namespace>
<requestFlow>
<handler type="session"/>
</requestFlow>
<responseFlow>
<handler type="session"/>
</responseFlow>
<parameter name="className" value="your class file to be stateful"/>
<parameter name="allowedMethods" value="*"/>
</service>

</deployment>
"your name space" is the name of namespace which you like to use for your service. It can be something like: http://localhost:8080/axis/services/flight, and "your class file to be stateful" is the name of class which you like to be stateful. This also can be something like: ticket.Flight.
After creating the WSDD file, first you should copy the .class file of your serive into Axis subdirectory under Tomcat installation path (e.g. <tomcap path>/webapps/axis/WEB-INF). You can do this task in two ways: copy the class file directly to classes subdirectory of Axis, or create a jar file of your class and then copy it to lib subdirectory of Axis.
After copying the class file or jar file into Axis, you can deploy your WSDD as follow:

java org.apache.axis.client.AdminClient deploy.wsdd

Notice before calling this command, add Axis libs to your CLASSPATH and also start your Tomcat.
Then if you write a test client to call your service, the reply from service should have a SOAP header similar to:

<soapenv:Header>
<ns1:sessionID soapenv:actor="" soapenv:mustUnderstand="0" xsi:type="xsd:long"
xmlns:ns1="http://xml.apache.org/axis/session">
-1919645576528915916
</ns1:sessionID>
</soapenv:Header>

Source: JavaPro

No comments: