WEB アプリケーション用に DOMINO をプログラミングする
次のコードは、Domino App.nsf データベースの「WebService」Web エージェント用の LotusScript コードです。
Sub Initialize
'Http://servername.com/Database+Name.nsf/agentName?OpenAgent
Dim s As New notessession Dim doc As NotesDocument Set doc = s.DocumentContext
'...Get SOAPin...
次のコード行は、DocumentContext オブジェクトの「Post」の結果として SOAP メッセージが格納される [Request_content] フィールドの内容に SOAPin 変数を設定します。
詳細は、「Web エージェントでの LotusScript と Java」を参照してください。
SOAPin= doc.GetItemValue("Request_content")(0)
次のコードは、SOAP メッセージをマニュアルで解析し、次の情報を抽出します。
On Error Resume Next bodyPos= Instr(1,SOAPin,|<SOAP-ENV:Body>|)+15 methodPos= Instr(bodyPos,SOAPin,|:|)+1 methodEnd=Instr(methodPos,SOAPin,| |) MethodName = Mid(SOAPin,methodPos,(methodEnd-methodPos)) nameSpacePos= Instr(methodEnd,SOAPin,|uri:|)+4
nameSpaceEnd=Instr(nameSpacePos,SOAPin,|"|) NameSpace=Mid(SOAPin,nameSpacePos,(nameSpaceEnd-nameSpacePos)) argPos=Instr(nameSpaceEnd,SOAPin,|>|)+1 argPos2=Instr(argPos, SoapIn, |>|)+1 argEnd=Instr(argPos2,SOAPin,|</|) argValue =Mid(SOAPin,argPos2,(argEnd-ArgPos2))
次のコードは、名前領域、メソッド、および引数を、SOAP 要求から、Web エージェントによって呼び出されたスクリプトライブラリ、関数、およびパラメータにそれぞれマップします。
LSlib = NameSpace Parameter = argValue MyFunction = MethodName Library= |"| & LSlib & |"| Arg= |("| & Parameter & |")|
次のコードは、CallString をスクリプトライブラリへの呼び出しと同じに設定し、戻り値を response 変数にキャプチャします。
CallString = |Use | & Library & | response = | & MyFunction & Arg
指定のスクリプトライブラリを実行する CallString を実行します。
Execute CallString
結果は次のようになります。argValue="1. this is the first quote"
次のコードは、response と MethodName の各変数を含む SOAP 応答を作成し、strTmp 変数に保存します。
strTmp = |<?xml version="1.0" encoding="UTF-8" standalone="no"?>| &_ |<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">| & _ |<SOAP-ENV:Body>| & _ |<m:| & MethodName & "Response" & | xmlns:m="| & NameSpace & |" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">| & _ |<Answer xsi:type="xsd:string">| & response & |</Answer>| & _ |</m:| & MethodName & |Response>| & _ |</SOAP-ENV:Body>| & _ |</SOAP-ENV:Envelope>|
エージェントの最初の Print ステートメントで、エージェントのコンテンツタイプが XML であることを指定する必要があります。デフォルトで、Domino は Web エージェントのコンテンツを HTML に変換します。
Print "Content-Type:text/xml"
次のステートメントは、strTmp に格納された SOAP 応答を要求元に返します。コンソールには出力しません。
Print strTmp
'Print |<?xml version="1.0" encoding="UTF-8" standalone="no"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAPSDK1:GetQuoteResponse xmlns:SOAPSDK1="uri:Domino"><Answer>4. I don't understand the Question.Try again.</Answer></SOAPSDK1:GetQuoteResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>|
End Sub
Web エージェントによって呼び出されるスクリプトライブラリを作成する LotusScript スクリプトライブラリで SOAP 要求によって呼び出される LotusScript コードをカプセル化します。
1 SOAP 要求で指定された名前領域と同じ名前で LotusScript スクリプトライブラリを作成します。
次に標準の LotusScript 関数を示します。これは「Domino」スクリプトライブラリの「GetQuote」関数です。このコードは、フォームの [Answer] フィールドに格納されている引用リストから引用をアクセスします。異なる引用を持つ複数の文書が「Domino App」データベースに格納されています。この関数は、最後に取り出した引用が続けて取り出されることがないようにしながら、ランダムに引用を取り出します。
Function GetQuote(aString As String) As String
'Return incoming string & "From LotusScript Lib function" 'GetQuote= aString$ & ".From LotusScript Lib function " & Time Dim s As New NotesSession Dim db As NotesDatabase Dim docCollection As NotesDocumentCollection Dim doc As NotesDocument Dim count As Integer Dim index As Integer Dim QuoteIndex As Integer Set db = s.CurrentDatabase Set docCollection = db.AllDocuments count = docCollection.count Randomize index = Int((count * Rnd) + 1) While aString = index index = Int((count * Rnd) + 1) Wend Set doc = docCollection.GetNthDocument(index) GetQuote = index & "." & doc.GetItemValue("Answer")(0) '& | | & Time End Function
この Web エージェントとスクリプトライブラリを使って、リモートの要求元は、SOAP としてフォーマットされた要求を送信し、Domino server でタスクを実行し、SOAP としてフォーマットされた回答を受け取れます。これで、Web サービスが提供されます。
WSDL ファイルを作成する WSDL ファイルは、潜在的なサービス消費者への Web サービスを記述して、Web サービスが置かれている場所 (URL)、サービスの名前領域、ユーザーが呼び出せるメソッドの名前、および要求されたパラメータについての詳細を指定します。WSDL ファイルが必要なのは、Microsoft の .NET ツールを使用してサービスを利用するユーザー向けの Web サービスを提供する場合です。
オプションで、ホスティングサーバーの HTTP 名を格納するためのプロファイルフォームを作成できます。後で WSDL ファイルでサービスの URL を指定するときに、このプロファイルフォーム使用してサーバー名を抽出できます。
たとえば、「WebServiceProfile」というプロファイルフォームを作成する場合、次の入力変換式を使ってサーバーの HTTP 名を HttpName という編集可能フィールドに保存できます。
_Name := @LowerCase(HttpName);
@If(@Begins(_Name;"http://");_Name;"http://"+_Name)
これにより、ユーザーがサーバーの HTTP 名として「web2.mysite.net」を入力すると、必要な「http://」が前に挿入されるようになります。
この値は、下の GetQuoteWSDL ページの計算結果で取り出します。
WSDL ファイルを作成するには: 1 Web サービスとして提供する LotusScript 関数が含まれているデータベースを開きます。
2 [Designer] メニューから、[作成] - [設計] - [ページ] を選択します。
6 次の XML の内容をページに追加します。
名前領域は LotusScript ライブラリと同じです。
xmlns:wsdlns='http://tempuri.org/wsdl/' xmlns:typens='http://tempuri.org/type' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:stk='http://schemas.microsoft.com/soap-toolkit/wsdl-extension' xmlns='http://schemas.xmlsoap.org/wsdl/'> <types> <schema targetNamespace='http://tempuri.org/type' xmlns='http://www.w3.org/2001/XMLSchema' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'> </schema> </types> <message name='Domino.GetQuote'> <part name='DocIndex' type='xsd:string'/> </message> <message name='Domino.GetQuoteResponse'> <part name='Result' type='xsd:string'/> </message> <portType name='GetQuoteSoapPort'> <operation name='Add' parameterOrder='A B'> <input message='wsdlns:Calc.Add' /> <output message='wsdlns:Calc.AddResponse' /> </operation> <operation name='GetQuote'> <input message='wsdlns:Domino.GetQuote' /> <output message='wsdlns:Domino.GetQuoteResponse' /> </operation> </portType> <binding name='GetQuoteSoapBinding' type='wsdlns:GetQuoteSoapPort' > <stk:binding preferredEncoding='UTF-8'/> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http' /> <operation name='GetQuote' > <soap:operation soapAction='http://tempuri.org/action/Domino.GetQuote' /> <input> <soap:body use='encoded' namespace='uri:Domino' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' /> </input> <output> <soap:body use='encoded' namespace='uri:Domino' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' /> </output> </operation> </binding> <service name='GetQuote' > <port name='GetQuoteSoapPort' binding='wsdlns:GetQuoteSoapBinding' >
@GetProfileField("WebServicesProfile";"HttpName")
2 つ目の計算結果には、次のコードが含まれます。このコードでは、データベース名に含まれる空白をプラス符号 (+) に置き換えます。Domino 6 では、次のコードの代わりに @WebDbName を使用できます。
@ReplaceSubstring(@Subset(@DbName;-1);" ";"+")
ソースに 2 つの計算結果の値を含めるほかの方法として、サービスの URL (http://servername.com/Domino+App.nsf/WebService?OpenAgent) を WSDL ファイルにハードコードすることもできます。