在Web service中通常采用兩種公認(rèn)技術(shù)來管理session,一種是借助HTTP和HTTP cookies,另一種是用SOAP headers。Axis能幫你實(shí)現(xiàn)這兩種技術(shù)。
by Kevin Jones
在Web service中沒有一種管理session的標(biāo)準(zhǔn)方法,只有兩種公認(rèn)的技術(shù),一種是依靠HTTP和HTTP cookies,另一種,或許也是最重要的一種方法,就是用SOAP headers。Axis能幫助開發(fā)人員實(shí)現(xiàn)這兩種技術(shù)。
在Axis中缺省使用的是HTTP managed sessions。在一個(gè)服務(wù)器中這么做是十分容易的,因?yàn)榇蠖鄶?shù)對(duì)Axis Web service的管理是通過org.apache.axis.MessageContext的一個(gè)實(shí)例來完成的。在一個(gè)Axis Web service中你可以通過調(diào)用MessageContext類中的靜態(tài)方法來得到MessageContext的一個(gè)實(shí)例:
public class SessionService { public String echo(String in) { MessageContext mc = MessageContext.getCurrentContext(); |
MessageContext中有一個(gè)名為setMaintainSession的方法,調(diào)用它便可激活session。但在編寫(Axis 1.1 RC2)時(shí),session對(duì)象只有在被訪問時(shí)才能激活,如下列代碼所示:
public class SessionService { public String echo(String in) { MessageContext mc = MessageContext. getCurrentContext(); Session session = mc.getSession(); String name = (String)session.get("name"); return in; } } |
這樣會(huì)導(dǎo)致Axis架構(gòu)生成一個(gè)set-cookie header:
Set-Cookie: JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9; Path=/axissessions |
客戶端需要在Cookie header中返回這個(gè)Cookie來保持該session。為了使axis運(yùn)行狀態(tài)下的客戶端能夠?qū)崿F(xiàn)這一點(diǎn),就必須調(diào)用org.apache.axis.client.Service接口的setMaintainSession方法。該接口是由WSDL2Java生成工具所生成的Locator類實(shí)現(xiàn)的。調(diào)用該方法之后,Axis架構(gòu)會(huì)自動(dòng)將該cookie返回到服務(wù)器中:
public static void main(String[] args) { UseSessionsServiceLocator locator = new UseSessionsServiceLocator(); locator.setMaintainSession(true); |
header看起來就像這樣:
Cookie: JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9 |
通過HTTP傳輸cookie是沒有問題的,但如果客戶端或服務(wù)器不通過HTTP,或使用的是通過多個(gè)Web services傳入調(diào)用的multihop service,那么這種方法就不那么有效了。一種更好的方法是用SOAP headers來加載session id。
Axis架構(gòu)支持多個(gè)Handlers。通過在一個(gè)Web service請(qǐng)求過程中調(diào)用調(diào)棧(call stack),Handlers能夠被放置到很多地方,它可以和傳輸過程結(jié)合起來,或者和一個(gè)Web service一起使用。Handlers可以被插入其中來處理Web service請(qǐng)求中的請(qǐng)求和/或響應(yīng)語(yǔ)句。
Axis帶有一個(gè)名為org.apache.axis.handlers.SimpleSessionHandler的handler,它用于提供基于session管理的SOAP header。要使用這個(gè)簡(jiǎn)單的帶有Web service的session handler,你必須告知Axis架構(gòu)將該handler添加到handler鏈中。你可以通過將該handler信息添加到server-config.wsdd來實(shí)現(xiàn)這一點(diǎn);一個(gè)簡(jiǎn)單的處理方法是定義一個(gè)包含額外配置Web service所需的WSDD文件,然后用Axis部署工具來部署這個(gè)配置文件。
這個(gè)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>urn:kevinj:Sessions</namespace> <requestFlow> <handler type="session"/> </requestFlow> <responseFlow> <handler type="session"/> </responseFlow> <parameter name="className" value= "kevinj.UseSessions"/> <parameter name="allowedMethods" value="*"/> </service> </deployment> |
該handler是和service分開定義并引用的,雖然你可以在service內(nèi)部定義它。注意這個(gè)handler是同時(shí)為了請(qǐng)求和響應(yīng)而定義的;這就確保了這些headers能夠在請(qǐng)求中被讀取并添加到響應(yīng)中去。你可以用這個(gè)管理工具來部署它:
java -cp [classpath to axis bits here] / org.apache.axis.client.AdminClient / -lhttp://localhost/myservice/AxisServlet deploy.wsdd |
現(xiàn)在服務(wù)器就可以運(yùn)行了,在使用該handler時(shí)服務(wù)器無需處理任何事情;而headers能夠自動(dòng)被添加進(jìn)去,就像這樣:
<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> |
要使用這個(gè)header,Web service客戶端必須能夠讀取它并了解其語(yǔ)法;而Axis客戶端可以解決這個(gè)問題。
要?jiǎng)?chuàng)建一個(gè)使用這個(gè)簡(jiǎn)單session的Axis客戶端,你需要配置Axis客戶端框架來使用該handler。過程同服務(wù)器端很相似,但不是部署到服務(wù)器,而是在本地創(chuàng)建config文件。你可以通過運(yùn)行org.apache.axis.utils.Admin來實(shí)現(xiàn)這一點(diǎn)。運(yùn)行以下代碼:
org.apache.axis.utils.Admin client deploy.wsdd |
這樣就創(chuàng)建了一個(gè)client-config.wsdd文件,它同樣也包含handler代碼。
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns= "http://xml.apache.org/axis/wsdd/" xmlns:java= "http://xml.apache.org/axis/ wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.implementation" value= "org.apache.axis.attachments. AttachmentsImpl"/> <parameter name= "sendMultiRefs" value="true"/> <parameter name="sendXsiTypes" value= "true"/> <parameter name= "sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type= "java:org.apache.axis.handlers. SimpleSessionHandler"/> </requestFlow> <responseFlow> <handler type= "java:org.apache.axis.handlers. SimpleSessionHandler"/> </responseFlow> </globalConfiguration> <handler name="session" type= "java:org.apache.axis.handlers. SimpleSessionHandler"/> <service name="Sessions" provider= "java:RPC" style="wrapped" use="literal"> <requestFlow> <handler type="session"/> </requestFlow> <responseFlow> <handler type="session"/> </responseFlow> <parameter name="allowedMethods" value="*"/> <parameter name="className" value= "kevinj.UseSessions"/> <namespace>urn:kevinj:Sessions</namespace> </service> <transport name="java" pivot= "java:org.apache.axis.transport. java.JavaSender"/> <transport name="http" pivot= "java:org.apache.axis.transport. http.HTTPSender"/> <transport name="local" pivot= "java:org.apache.axis.transport. local.LocalSender"/> </deployment> |
為了使客戶端能夠利用這個(gè)handler,你必須將client-config.wsdd文件添加到客戶端的classpath中。然后由Axis框架代表客戶端來讀取并響應(yīng)這些headers。同樣,客戶端代碼無需處理任何事情便可以使用它了。
關(guān)于作者:
Kevin Jones是一名有著15年開發(fā)經(jīng)驗(yàn)的開發(fā)人員。最近四年來,他一直從事Java編程方面的研究和教學(xué),最近, 他主要研究HTTP和XML。Kevin住在U.K.,他為Developmentor工作,這是一家以United States 和Europe為基地,主要做Java和Microsoft平臺(tái)的技術(shù)培訓(xùn)的公司。Kevin的聯(lián)系方式是:kevinj@develop.com。