也许二年前甚至一年前,我们还对Ajax、WebService相当陌生。然而今天,Ajax与WebService已经充斥互联网了。如果一年前还有人对是否使用Ajax、WebService犹犹豫豫,,那么今天已经容不得你选择了。
在Domino平台上直到Domino7.0发布,才增加了WebService设计元素。不过那时对WebService大家都没什么兴趣。也难怪,在Domino平台上想搞点创新还真不是件容易的事。需要面对升级Domino平台的问题,内存泄漏的问题,与其他系统整合的问题等等。这些问题使开发者对Domino意见很大,甚至有部分人选择了放弃Domino平台。Domino真的不行了吗?这些问题不能克服吗?随着Domino8.5版本的发布,我们惊喜的看到了Domino自己的变化,也许凤凰涅槃就在Domino8.5?也未可知。
以上是一些废话,转入本文的正题:利用Ajax调用Domino的WebService。
利用JS调用WebService其实很简单,就是利用Js程序拼出符合SOAP规范的XML数据然后将此XML数据传给WebService就可以了。但是具体怎么做那?
先来看一段js代码。
senddata = '<?xml version="1.0" encoding="gb2312"?>';
senddata += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DefaultNamespace">';
senddata += '<soap:Body>';
senddata += '<urn:' + method + ' xmlns="' + _Namespace + '">';
senddata += '<TFileNum xsi:type="xsd:string">' + strfilenum + '</TFileNum>';
senddata += "</urn:" + method + ">";
senddata += "</soap:Body>";
senddata += "</soap:Envelope>"
这个senddata就是拼成的SOAP规范的webService调用接口参数。
具体的SOAP规范是怎么定义的请大家查看参考资料,在这里就不解释了。好了,符合SOAP规范的调用参数写好了,怎么调用WebService那?
var method = "convert";
var _Namespace = "http://linux.domino01.com.cn/"
var url = _Namespace + "/webservice.nsf/convert2meeting?openWebService"
callwebservice(senddata, url, _Namespace + method);
//调用webservciefunction
callwebservice(xmldata, url, SOAPACTION){
createXMLHttpRequest();
xmlHttpReq.open("POST", url, false);//同步调用
xmlHttpReq.onreadystatechange = showResult; //调用showResult方法
xmlHttpReq.SetRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpReq.SetRequestHeader("Content-Length", getlen(xmldata));
xmlHttpReq.SetRequestHeader("SOAPAction", SOAPACTION);
xmlHttpReq.Send(xmldata);
}
到此WebService总算是能调用了,可是好像差点什么是吧?返回的数据怎么使用啊?这个还真是麻烦,一般调用XFire或者Axis开源框架的 WebService会将返回参数打包在XML文档中返回,(犯了个小错误,我已经编辑掉了.)让我们看看Domino的WebService返回什么数 据吧。
当使用XmlHTTP对象将参数传给WebService后,WebService处理后返回给调用者一个参数。这个参数是也是XML格式的数据。
比如我们传给WebServcie的参数是“aaa”,webService什么操作也不做直接返回这个参数。返回的格式是什么那?看下面
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:convertResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:DefaultNamespace">
<convertReturn xsi:type="xsd:string">aaa</convertReturn>
</ns1:convertResponse>
</soapenv:Body>
</soapenv:Envelope>
如果你想根据返回的结果判断下一步的操作,请解析XML数据。
xmlresponse = xmlHttpReq.responseXML;
var convertreturn = xmlresponse.getElementsByTagName("convertReturn");
var returnvalue = convertreturn[0].text;
当然在这里只简单运用了字符串作为参数传递,没有讨论如何传递数组的复杂结构参数。此文的目的仅是让大家了解一下Ajax和WebService在Domino这个平台下的应用。如果谁有兴趣也可以研究一下传输复杂结构参数的实现方法。
好了,客户端的调用算是介绍完了,那么该看看我们的WebServcie是怎么写的了。我是使用的Java语言来写的WebService。
import lotus.domino.*;
import lotus.domino.types.*;
public class convert2meeting {
// 这是一个 Web Service 的模板实现类。它
// 会成为外部类(如果导入 WSDL 文档)。此 Web Service 的用户
// 可以调用此实现类中的任何公用方法。
//
// 要获得会话对象,请使用此代码:
Session s = WebServiceBase.getCurrentSession();
public String convert(String TFileNum){
try{
AgentContext agcontext = s.getAgentContext();
Database db = agcontext.getCurrentDatabase();
}catch(Exception e){
}
System.out.println(TFileNum);
return TFileNum;
}
}
具体的WebService的WSDL:
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="urn:DefaultNamespace" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:DefaultNamespace" xmlns:intf="urn:DefaultNamespace" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<message name="convertResponse">
<part name="convertReturn" type="xsd:string"/>
</message>
<message name="convertRequest">
<part name="TFileNum" type="xsd:string"/>
</message>
<portType name="convert2meeting">
<operation name="convert" parameterOrder="TFileNum">
<input message="impl:convertRequest" name="convertRequest"/>
<output message="impl:convertResponse" name="convertResponse"/>
</operation>
</portType>
<binding name="DominoSoapBinding" type="impl:convert2meeting">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="convert">
<wsdlsoap:operation soapAction="convert"/>
<input name="convertRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:DefaultNamespace" use="encoded"/>
</input>
<output name="convertResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:DefaultNamespace" use="encoded"/>
</output>
</operation>
</binding>
<service name="convert2meetingfoservice">
<port binding="impl:DominoSoapBinding" name="Domino">
<wsdlsoap:address location="http://localhost"/>
</port>
</service>
</definitions>
参考文献:
http://www.ibm.com/developerworks/cn/lotus/web-services/