<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-128  評論-55  文章-5  trackbacks-0
     

    理解 WSRF第1部分-使用 WS-ResourceProperties

     

    本教程是一個由 4 部分組成的系列文章中的第 1 部分,該系列介紹 WSRF 背后的概念。WSRF 是一組規范,提供一種標準方式,在 Web 服務應用程序的本質上“無狀態的”環境中與“有狀態”資源交互。這種有狀態資源實際上可以是任何東西,從數據庫到電子鼠標都是有狀態資源。實際上,可以使用 WSRF 來處理任何可以通過改變其屬性來操縱的東西。

    WS-Resource 是一個有狀態資源(比如數據庫或硬盤)和它與之交互的 Web 服務的組合。本教程解釋了創建 WS-Resources、請求和更新用于定義 WS-Resource 狀態的屬性。我們將介紹以下內容:

    1.       WSRF 規范的概述。

    2.       本系列教程中使用的系統的概述。

    3.       關于 WSDL 的基本信息。

    4.       關于的 WS-Addressing 的基本信息,WS-Addressing 用于定位 WS-Resource。

    5.       創建 WS-Resource。

    6.       獲得和設置 WS-Resource 屬性。

    注意,WSRF 作為一種規范,定義了描述這些操作的 WSDL 文件的結構。該 WSDL 文件然后可以被任何語言的實現所使用。本教程描述 WSDL 文件的創建并展示產生的 SOAP 消息。

    一定要明白,WSRF 規范只定義應該做什么,而不定義應該如何做。這里描述的概念的實際實現留給了應用程序去完成。在本系列的第 4 部分中,我們將使用 Globus Alliance 提供的 Core Java WSRF 類來討論該實現。

    在本系列中,我們將使用一系列圍繞地球的人造衛星作為例子。我們將創建一個 WS-Resource,它就代表這樣一個人造衛星。然后我們將請求它的屬性以檢索它的數據,更改它的屬性以將它發射到天空中,并更改它觀測的對象。

    l         網格計算簡介

    首先,我們指出 WSRF 的應用遠遠超出了網格,但是 WSRF 是在網格中開發的,所以網格是 WSRF 可以做什么的一個很好的例子。之后,將花點時間為那些不熟悉網格的人解釋一下網格是什么。

    數百萬的 Web 用戶已經體驗了網格計算,只是大多數人都沒有感覺到而已。1998 年,由于 NASA 發起了一個新計劃,使得 Search for Extraterrestrial Intelligence (SETI) 項目差點夭折,此時就創建了也許是最著名的網格計算項目。他們的 Aricebo 無線電望遠鏡得到非常大量的數據,但是沒有足夠的處理能力可以處理這些數據。為了解決這個問題,他們創建了一個 screensaver,當計算機不忙時,它會從中央服務器請求一包數據并進行分析,然后把結果發送回去。然后又請求一包新的數據,并重復剛才的步驟。數百萬用戶下載了 screensaver 并參與該項目。

    大型網格應用程序更加復雜,要處理的問題也更多,這些問題與身份驗證、跨組織邊界的進程間通信和高性能數據傳輸等方面有關。盡管網格狂熱者可能不愿意聽到 SETI@Home 的情況,但是它確實提供了網格工作原理的一個很好的例子。它涉及以下步驟:

    工作被分成適合于在多個系統上處理的“單元”。該“工作”可以是計算、存儲或其他類型的處理。

    單元被分布到多個客戶機。這個步驟通過讓客戶機請求或“pull”工作,或者讓中央服務器將工作“push”給可用的客戶機來完成。

    總體進展、需求和狀態由中央系統或系統組維護。

    當前的網格應用程序一般遵循這種模式,有一個中央系統與客戶機交互,這些客戶機位于那些一般在地理位置上與中央服務器分離的系統上。

    l         使用 Web 服務:承諾和問題

    既然多個客戶機在不同的地方,那么 Web 服務應該是網格計算順理成章的選擇。畢竟,它提供一種標準而容易的方法,以從一個系統到另一個系統獲得信息,而不用求助于特定于平臺或語言的方法,比如 CORBA、DCOM 或 Java-RMI。

    但是,原來并不是這樣的。早期的網格應用程序使用其他更加不可移植的方法。但是為什么呢?

    也許最貼切的是體系結構方面的原因。盡管網格應用程序可以在很多機器上實現,但它仍然是一個應用程序,因此很難與本質上是無狀態的體系結構相協調。當使用數據庫客戶機連接到一個數據庫時,您就保持了連接,并且可以插入記錄,然后再查看插入的結果。另一個客戶查看表時不會看到該記錄,除非您提交了事務,但是數據庫認識您的會話,并知道是您。

    Web 服務的工作方式不是這樣的。利用 Web 服務,您發出請求(比如插入一條記錄)并得到響應(比如插入成功),然后斷開連接。沒有正在進行的會話需要管理。例如 HTTP —— 在大多數情況下,Web 服務通過 HTTP 傳輸 —— 每個請求獨立于前一個請求,Web 服務沒有訪問或使用任何不是當前輸入消息一部分的信息。

    WSRF 的目標是通過創建“狀態”概念以及處理狀態的方法來解決該問題。

    l         有狀態資源

    那么到底什么是“狀態”,什么又是“有狀態”資源呢?

    有狀態資源是一些即使您不與之交互也存在的東西。例如數據庫,即使在您不查詢它的時候,它也存在。圍繞行星的人造衛星即使在您不與之對話時也存在。甚至簡單的計數器,在調用之間,或者每次調用它返回一個 1 時,都必須存在。

    此外,一定要明白,狀態概念也包含屬性的思想。當要求您把所借的東西以原狀態返還時,涉及某些屬性的值,比如清潔度、修理要求、油罐中的汽油量,等等。有狀態資源與此類似,具有定義其狀態的屬性,并且這些屬性就是我們將與資源交互的方式,如 WS-Resource 的屬性 中所示。

    l         Web 服務 + 有狀態資源 = WS-Resource

    根據規范可知,WS-Resource 是 Web 服務與它在其上起作用的有狀態資源的組合。但是這真正的含義是什么呢?

    讓我們從實際的角度來看這個問題。假設我們有一個系統,涉及到管理一組人造衛星。每個人造衛星是一個有狀態資源,因為即使在我們不與之對話時它也存在。我們還有一個 Web 服務,它提供人造衛星功能,比如更改反向、檢索信息,或者甚至調整姿勢。

    通過將者二者組合起來,我們創建了一個 WS-Resource。注意,并不需要一對一的對應關系。例如,這個 Web 服務可以與幾個不同的人造衛星交互,從而創建幾個引用相同服務的不同 WS-Resources。另一方面,一個人造衛星可以與幾個不同的服務(比如控制宇宙實驗的服務和發射激光束的服務)交互,從而創建幾個引用相同有狀態資源的不同 WS-Resources。

    為了使用 WS-Resource,必須了解它的屬性。

    l         WS-Resource 的屬性

    正如 有狀態資源 中所提到的,有狀態資源(以及 WS-Resource)具有各種與之相關的屬性。例如,人造衛星可能具有以下屬性:

    latitude

    longitude

    altitude

    pitch

    yaw

    roll

    focalLength

    currentView

    在本例中,latitude、longitude 和 altitude 這前三個屬性指定人造衛星的位置。其次,pitch、 yaw 和 roll 指定它的方位,或者說它看起來的方向。最后兩個屬性,focalLength 和 currentView,指定它離觀測點的距離以及它在這一點所看到的東西。

    這些屬性的值定義資源的狀態。更改屬性值,就更改了狀態。事實上,我們就是這樣來控制人造衛星的。要更改它的位置,我們就改變它的一個位置屬性。要更改它的方位,我們就改變它的一個方位屬性。實際上,我們想要對該人造衛星做任何事情(在這個非常有限的實現中),我們都只要改變這些屬性就可以了。

    但是如何來做到 這一點?

    l         進入 WSRF

    好了,既然 WS-Resource 是有狀態資源和 Web 服務的組合,并且我們通過請求和設置它的屬性來操縱它,那么如何來做到這一點呢?

    有很多方法來做到這一點,但是問題也正在于此。需要的是一種做出請求的標準方式,以獲取和設置各個屬性。

    進入 WSRF。WSRF 實際上是一系列規范,用于定義標準的“消息模式”或方法,以請求屬性的值或者指定這些屬性應該變更。實際上,WSRF 定義一些標準方法,以處理關于處理 WS-Resources 的各個方面,比如處理它們的屬性,從而將它們成組在一起,以達到諸如這樣的目的 —— 進行身份驗證,確保它們被及時地銷毀。

    WSRF 定義這些操作的方法是,指定它們應該如何出現在 Web 服務描述語言(Web Services Description Language,WSDL)文件中。WSDL 文件定義 Web 服務會話兩端之間傳輸的消息,所以通過定義 WSDL 文件,WSRF 定義了發生的任何交互的形式。

    當我們說到“WSRF”時,實際上是指幾個不同的規范:

    1.       WS-ResourceProperties (WSRF-RP)指定 ResourceProperties 在 WSDL 文件中被定義的形式。它還指定消息的形式,這些消息用于請求和接收屬性的值,還解釋了如何更改、添加和刪除 WS-Resource 的屬性。

    2.       WS-ResourceLifetime (WSRF-RL)談論這樣一些狀態,即 WS-Resource 需要過期了,或者在它不再需要了時應該被顯式地銷毀。

    3.       WS-ServiceGroup (WSRF-SG)定義創建一組 Web 服務(比如可用服務的注冊表)的方法。

    4.       WS-Base Faults (WSRF-BF)定義一種標準的方法,用于指出基于 WSRF 的應用程序中的錯誤。

    您將注意到,沒有哪一個規范簡單地描述了所有這一切應該如何一起工作。這項工作(幾乎)是由 Modeling Stateful Resources with Web Services 白皮書完成的,該書解釋了一般的概念,并將它們聯系在一起。

    本教程除了介紹白皮書中的一些概念之外,還將討論 WS-ResourceProperties 規范。本系列的以后各期將討論其他的 WSRF 規范,以及 Web Services Notifications (WSN) 規范(整個 WSRF 中都引用了這些規范)。

    l         什么是 WSDL,為什么我要關注它?

    在正式開始在 WSDL 文件中創建 WS-Resources 之前,我們首先花點時間來看一下 WSDL 文件的目的和結構。這些描述就是在 WSDL 文件中。

    Web 服務 —— 或者至少是與 WS-Resources 相關的 Web 服務 —— 由 SOAP 消息組成。SOAP 消息具有一個標準的“信封”,其中包含一個“有效負載”。該有效負載是由服務器(在請求時)和客戶機(在響應時)分析的數據??紤]下面這個 SOAP 消息:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

        <SOAP-ENV:Header/>

        <SOAP-ENV:Body>

            <SetAltitudeRequest xmlns="http://example.com/satellite.xsd">

                <altitude>47700</altitude>

            </SetAltitudeRequest>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    它包含標準信封和有效負載,前者在 http://schemas.xmlsoap.org/soap/envelope/ 名稱空間(SOAP-ENV)中,后者在 http://example.com/satellite.xsd 名稱空間中。

    有效負載可以是任何東西,因此存在這樣一個問題:如何定義應用程序期望看到什么,以及返回什么?因而有了 WSDL 文件的用武之地。最終,我們將使用一個 WSDL 文件來定義 WS-Resource 所使用的“消息模式”,但是在本節,我們只來看 WSDL 文件的各部分是如何組合在一起的。

    l         消息和類型

    我們首先定義一條我們將會發送的實際消息:

    <?xml version="1.0"?>

    <definitions name="Satellite"

        targetNamespace="http://example.com/satellite.wsdl"

        xmlns:tns="http://example.com/satellite.wsdl"

        xmlns:satTypes="http://example.com/satellite.xsd"

        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

        xmlns="http://schemas.xmlsoap.org/wsdl/">

        <types>

            <schema targetNamespace="http://example.com/satellite.xsd"

                  xmlns="http://www.w3.org/2000/10/XMLSchema">

               <element name="SetAltitudeRequest">

                  <complexType>

                      <all>

                          <element name="altitude" type="float"/>

                      </all>

                  </complexType>

               </element>

               <element name="SetAltitudeResponse">

                  <complexType>

                      <all>

                          <element name="result" type="string"/>

                      </all>

                  </complexType>

               </element>

           </schema>

        </types>

        <message name="SetAltitudeInput">

            <part name="body" element="satTypes:SetAltitudeRequest"/>

        </message>

        <message name="SetAltitudeOutput">

            <part name="body" element="satTypes:SetAltitudeResponse"/>

        </message>

    </definitions>

    從底部開始,我們定義了兩種類型的消息。第一種是 SetAltitudeInput,這是我們將發送給服務器作為輸入的消息。它是您在什么是 WSDL,為什么我要關注它? 中看到的 SOAP 消息中的消息。第二種消息是 SetAltitudeOutput,這是服務器發送給客戶機的響應。兩種消息都指定一個元素,消息體將放在該元素中。

    這些元素的實際定義位于文件頂部的模式(schema)中。例如,SetAltitudeInput 消息包含一個 SetAltitudeRequest 元素,該元素自己又包含一個 altitude 元素,后面這個元素的內容必須是一個 float。

    接下來,我們將組合這些消息,以創建服務器執行的一個操作。

    l         端口類型和操作

    既然知道了我們將要發送的消息是什么,現在就需要指定它們將要完成的角色。要做到這一點,我們將創建一個 portType 及其相關的 operation:

    ...

        <message name="SetAltitudeInput">

            <part name="body" element="satTypes:SetAltitudeRequest"/>

        </message>

        <message name="SetAltitudeOutput">

            <part name="body" element="satTypes:SetAltitudeResponse"/>

        </message>

        <portType name="AltitudePortType">

            <operation name="SetAltitude">

               <input message="tns:SetAltitudeInput"

                      wsa:Action="http://example.com/SetAltitude" />

               <output message="tns:SetAltitudeOutput"

                      wsa:Action="http://example.com/SetAltitudeResponse" />

            </operation>

        </portType>

    </definitions>

    這里,我們定義了一個 portType 叫做 AltitudePortType,以及它的一個 operation 叫做 SetAltitude。我們實際上可以在該 portType 中定義任意數量的操作,但是現在我們是為了保持簡單。SetAltitude 操作指定一個 input 消息 SetAltitudeInput 和一個 output 消息 SetAltitudeOutput。(下一節我們將處理 wsa:Action 屬性。此外,請注意名稱空間信息。)

    您也可以指定一個 fault 消息在有問題時發送,在本教程系列的后面將會介紹這一點,但是現在還是保持簡單。

    l         服務和綁定

    至此,我們已經使用 portType 定義了可以做什么 事情,但是沒有定義如何 做。要完成這個過程,我們需要創建一個描述如何做的 binding,并將它附加到實際的 service:

    ...

        <portType name="AltitudePortType">

            <operation name="SetAltitude">

               <input message="tns:SetAltitudeInput"

                      wsa:Action="http://example.com/SetAltitude" />

               <output message="tns:SetAltitudeOutput"

                      wsa:Action="http://example.com/SetAltitudeResponse" />

            </operation>

        </portType>

        <binding name="AltitudeSoapBinding" type="tns:AltitudePortType">

            <soap:binding style="document"

                      transport="http://schemas.xmlsoap.org/soap/http"/>

            <operation name="SetAltitude">

               <input>

                   <soap:body use="literal"/>

               </input>

               <output>

                   <soap:body use="literal"/>

               </output>

            </operation>

        </binding>

        <service name="SatelliteService">

            <port name="AltitudePort" binding="tns:AltitudeSoapBinding">

                <soap:address location="http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    我們還是從底部開始,先看 service 元素。一個 WSDL 文件可以定義多個服務。例如,您可能有用于不同目的的不同服務,或者在不同位置有具有相同目的的不同服務,或者具有不同綁定的不同服務,比如一個用于 SOAP 的服務和一個用于 SMTP 的服務。

    在本例中,我們將利用一個端口 AltitudePort 來定義一個服務 SatelliteService。但是我們知道關于該端口的哪些情況呢?哦,我們知道 SOAP 請求應該發送到 http://example.com/satellite。我們還知道,為了獲得關于如何發送消息的更多信息,應該檢查 AltitudeSoapBinding。

    AltitudeSoapBinding 指定它是 AltitudePortType 的一個實現,所以我們知道發送什么消息。binding 本身指定每個操作中的消息是如何格式化的。在本例中,我們使用“document/literal”樣式,這意味著我們只是將定義好的元素拖放到 Body 中。

    我們知道了消息將發送到哪里,如何格式化這些消息,以及這些消息應該是什么。到 創建 WS-Resource 一節,我們將介紹如何創建 WSRF 定義的特定消息,但是首先我們需要了解一下 WS-Addressing。

    l         什么是 WS-Addressing,為什么我要關注它?

    以前,很容易指定 Web 服務的地址。所有您真正需要的就是 URL,所有其他信息都包含在 SoapAction 頭部或消息本身中?,F在,Web 服務應用程序變得越來越復雜,并不總是那么簡單。您若想要讓應答發送到除最初的請求者之外的其他地方,或者需要其他信息(比如會話標識符)來定義實際的“位置”,那該怎么辦?

    或者您只是需要附加到 Web 服務的一個特定實例,那該怎么辦?我們在 WS-Resources 的情況中將會遇到這個問題,所以我們需要一種處理它的方式。

    WS-Addressing 提供一種方式來指定關于位置的信息,而不只是一個統一資源標識符(Universal Resource Identifier,URI)或 URL。實際上,在我們的例子中,它提供一種標準的方式,將大量的信息添加到 SOAP 消息。我們來構造一個 SOAP 消息,比如:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

    xmlns:wsa="http://www.w3.org/2005/02/addressing"

    xmlns:sat="http://example.org/satelliteSystem">

        <SOAP-ENV:Header>

          <wsa:To SOAP-ENV:mustUnderstand="1">http://example.com/satellite</wsa:To>

          <wsa:Action>http://example.com/SetAltitude</wsa:Action>

          <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

            <SetAltitudeRequest xmlns="http://example.com/satellite.xsd">

                <altitude>47700</altitude>

            </SetAltitudeRequest>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    在實際的 SOAP 消息中具有該信息似乎并不重要,但是請記住,消息可能會傳輸通過多個系統,甚至需要多次傳輸才能到達最終目的地。

    要指定該信息,我們需要創建一個 EndpointReference。

    WS-Addressing 引入了 EndpointReference 概念。EndpointReference 是一種方式,用于指定讓消息到達適當的位置并帶有適當的相關信息所需的信息。例如,我們在前一屏中指定的消息的 EndpointReference 應該是:

    <wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/02/addressing"

    xmlns:sat="http://example.org/satelliteSystem">

        <wsa:Address>http://example.com/satellite</wsa:Address>

        <wsa:ReferenceProperties>

             <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </wsa:ReferenceProperties>

    </wsa:EndpointReference>

    理解 EndpointReference 和 SOAP 消息之間的關系很重要,因為 EndpointReference 就是我們指定特定 WS-Resource 的位置的方式。例如,當我們請求創建新的 WS-Resource 時,響應將包含一個指向它的 EndpointReference。

    l         什么是 WS-Resource?

    至此,您應該在概念上對什么是 WS-Resource 有了很好的理解,并且應該對 WSDL 和 WS-Addressing 有了很好的基本了解?,F在開始實際地創建和使用 WS-Resources。

    我們首先來定義 WS-Resource 到底真正是什么。在我們的人造衛星例子中,我們可以具有幾種類型的 WS-Resources,比如:

    一個服務,用于設置或檢索特定人造衛星的高度。

    一個服務,用于設置或檢索特定人造衛星的位置或方位。

    一個服務,用于提供對一個進程的訪問,該進程計數特定人造衛星觀測到的恒星。

    關于該列表有兩件事情一定要注意:即所有三個 WS-Resources 都可以引用相同的人造衛星,還有,WS-Resource 是由服務和有狀態資源(在本例中是人造衛星)的組合定義的,而不是由它可以執行的操作數量定義的。

    現在,我們可以說 WS-Resource 是 Web 服務和有狀態資源的組合,但是我們如何在應用程序中表示這個有狀態資源呢?答案就在它的 ResouceProperties 中。正如 有狀態資源 中提到的,對象的狀態可以由它的各種屬性的值來決定。因為我們真正感興趣的就是對象的狀態,所以我們可以把有狀態資源表示為一個展示其屬性的 XML 文檔。該文檔叫做資源屬性文檔。在我們的人造衛星例子中,它可能是具有以下代碼行的文檔:

    <satProp:GenericSatelliteProperties  xmlns:satProp="http://example.com/satellite">

       <satProp:latitude>30.3</satProp:latitude>

       <satProp:longitude>223.2</satProp:latitude>

       <satProp:altitude>47700</satProp:altitude>

       <satProp:pitch>49</satProp:pitch>

       <satProp:yaw>0</satProp:yaw>

       <satProp:roll>32</satProp:roll>

       <satProp:focalLength>21999992</satProp:focalLength>

       <satProp:currentView>

            http://example.com/satellite/2239992333.zip

       </satProp:currentView>

    </satProp:GenericSatelliteProperties>

    狀態的更改需要一個或多個這些屬性的更改,反之亦然。

    就像可以通過添加成員或方法來擴展類一樣,我么可以通過添加屬性來擴展 WS-Resource。例如,考慮這樣一種情形,我們具有一個人造衛星,它也充當恒星計數器。除了有狀態資源的一般屬性之外,我們可能還有一個 currentCount 屬性:

    <satProp:GenericSatelliteProperties

           xmlns:satProp="http://example.com/satellite"

           xmlns:counterProp="http://example.com/satellite/CounterSatelliteProperties">

       <satProp:latitude>30.3</satProp:latitude>

       <satProp:longitude>223.2</satProp:latitude>

       <satProp:altitude>47700</satProp:altitude>

       <satProp:pitch>49</satProp:pitch>

       <satProp:yaw>0</satProp:yaw>

       <satProp:roll>32</satProp:roll>

       <satProp:focalLength>21999992</satProp:focalLength>

       <satProp:currentView>

            http://example.com/satellite/2239992333.zip

       </satProp:currentView>

       <counterProp:currentCount>92828</counterProp:currentCount>

    </satProp:GenericSatelliteProperties>

    注意新信息是在一個獨立的名稱空間中。

    l         合并 WS- 和 Resource:WSDL 文件

    至此,我們已經創建了有狀態資源(人造衛星)的表示,但是要真正地創建 WS-Resource,我們還必須使用 WSDL 文件將它綁定到服務。

    我們首先來看一個基本的 WSDL 文件:

    <?xml version="1.0" encoding="UTF-8"?>

    <definitions name="Satellite"

        targetNamespace="http://example.com/satellite"

        xmlns="http://schemas.xmlsoap.org/wsdl/"

        xmlns:tns="http://example.com/satellite"

        xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing"

        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

        xmlns:wsrp=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd"

        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

            <wsdl:import namespace=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

           location="WS-ResourceProperties.wsdl" />

            <types>

              <xsd:schema targetNamespace="http://example.com/satellite"

                          xmlns:xsd="http://www.w3.org/2001/XMLSchema">

                            <xsd:import namespace=

     "http://schemas.xmlsoap.org/ws/2004/03/addressing"

                               schemaLocation="WS-Addressing.xsd" />

              </xsd:schema>

            </types>

    </definitions>

    該文件現在還很空,但是請注意,為了能夠工作,還需要導入兩個文件。WS-ResourceProperties.wsdl 和 WS-Addressing.xsd 文件的典型版本可能會引用您還沒創建在機器上的目錄,所以為了簡單起見,您可以從教程參考資料 下載簡化的版本。

    既然有了框架,現在我們就來填充它吧。

    首先,我們將實際的有狀態資源添加到文件,并將之與 Web 服務關聯:

    <?xml version="1.0" encoding="UTF-8"?>

    <definitions name="Satellite"

        targetNamespace="http://example.com/satellite"

        xmlns="http://schemas.xmlsoap.org/wsdl/"

        xmlns:tns="http://example.com/satellite"

        xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing"

        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

        xmlns:wsrp=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-

    WS-ResourceProperties-1.2-draft-01.xsd"

        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

            <wsdl:import namespace=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-

    WS-ResourceProperties-1.2-draft-01.wsdl"

              location="WS-ResourceProperties.wsdl" />

          <types>

          <xsd:schema targetNamespace="http://example.com/satellite"

              xmlns:xsd="http://www.w3.org/2001/XMLSchema">

         

              <xsd:import namespace=

                      "http://schemas.xmlsoap.org/ws/2004/03/addressing"

                      schemaLocation="WS-Addressing.xsd" />

               

              <xsd:element name="latitude" type="xsd:float" />

              <xsd:element name="longitude" type="xsd:float" />

              <xsd:element name="altitude" type="xsd:float" />

              <xsd:element name="pitch" type="xsd:float" />

              <xsd:element name="yaw" type="xsd:float" />

              <xsd:element name="roll" type="xsd:float" />

              <xsd:element name="focalLength" type="xsd:float" />

              <xsd:element name="currentView" type="xsd:string" />

               

              <xsd:element name="GenericSatelliteProperties">

                <xsd:complexType>

                   <xsd:sequence>

                     <xsd:element ref="latitude" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="longitude" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="altitude" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="pitch" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="yaw" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="roll" minOccurs="1"

                     maxOccurs="1"/>

                    <xsd:element ref="focalLength" minOccurs="1"

                     maxOccurs="1"/>

                     <xsd:element ref="currentView" minOccurs="1"

                     maxOccurs="1"/>

                   </xsd:sequence>

                </xsd:complexType>

              </xsd:element>             

               

          </xsd:schema>

           

        </types>

        <portType name="SatellitePortType"

                wsrp:ResourceProperties=

                "tns:GenericSatelliteProperties">

        </portType>

       

        <binding name="SatelliteSoapBinding"

        type="tns:SatellitePortType">

          <soap:binding style="document"

          transport="http://schemas.xmlsoap.org/soap/http"/>

        </binding>

       

        <service name="SatelliteService">

            <port name="SatellitePort"

            binding="tns:SatelliteSoapBinding">

                <soap:address location=

                "http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    我們首先是添加 Web 服務的基礎、實際的 service 元素和將之與 portType 關聯的 binding。portType 本身還沒有任何操作,但是重要的部分是 wsrp:ResourceProperties 屬性。該屬性指定,Web 服務執行的任何操作都是在一個特定類型的有狀態資源上執行的,如 GenericSatelliteProperties 元素所定義的。GenericSatelliteProperties 元素定義在 schema 中。該有狀態資源和該 Web 服務的組合就是 WS-Resource。

    注意,規范中指出,在創建資源屬性文檔(比如本例中的 GenericSatelliteProperties)時,必須 使用這里展示的樣式,即初始元素是定義和引用的,而不是內聯地定義的。

    現在我們向 WSDL 文件添加一些實際的操作,看它是如何工作的。

    l         請求新的人造衛星

    當然,該練習的整個目的是真正對 WS-Resource 做一些事情,所以我們要做的第一件事情是,創建對實際 WS-Resource 實例的一個引用:

    ...

        <types>

           <xsd:schema targetNamespace="http://example.com/satellite"

               xmlns:xsd="http://www.w3.org/2001/XMLSchema">

               

                <xsd:import namespace=

                   "http://schemas.xmlsoap.org/ws/2004/03/addressing"

                      schemaLocation="WS-Addressing.xsd" />

               

              <xsd:element name="createSatellite">

                  <xsd:complexType/>

              </xsd:element>

               

              <xsd:element name="createSatelliteResponse">

                 <xsd:complexType>

                     <xsd:sequence>

                         <xsd:element ref="wsa:EndpointReference"/>

                      </xsd:sequence>

                  </xsd:complexType>

              </xsd:element>

               

              <xsd:element name="GenericSatelliteProperties">

                   ...

              </xsd:element>           

               

            </xsd:schema>

           

        </types>

        <message name="CreateSatelliteRequest">

          <part name="request" element="tns:createSatellite"/>

        </message>

       

        <message name="CreateSatelliteResponse">

         <part name="response" element=

         "tns:createSatelliteResponse"/>

        </message>

       

        <portType name="SatellitePortType"

          wsrp:ResourceProperties=

          "tns:GenericSatelliteProperties">

          <operation name="createSatellite">

              <input message="tns:CreateSatelliteRequest"

                   wsa:Action=

                   "http://example.com/CreateSatellite" />

               <output message="tns:CreateSatelliteResponse"

                  wsa:Action=

                  "http://example.com/CreateSatelliteResponse" />

            </operation>

        </portType>

       

       <binding name="SatelliteSoapBinding" type=

       "tns:SatellitePortType">

        <soap:binding style="document" transport=

        "http://schemas.xmlsoap.org/soap/http"/>

          <operation name="createSatellite">

              <input>

                  <soap:body use="literal"/>

               </input>

               <output>

                  <soap:body use="literal"/>

               </output>

         </operation>

       </binding>

       

       <service name="SatelliteService">

          <port name="SatellitePort" binding=

          "tns:SatelliteSoapBinding">

              <soap:address location=

              "http://example.com/satellite"/>

           </port>

       </service>

    </definitions>

    乍一看,這與我們在 需要了解的 WSDL 知識 中創建的 WSDL 文件沒有太大的區別。我們具有一個指向 binding 的 service,binding 解釋如何實現 portType。portType 定義一個操作,即 createSatellite,該操作使用一個 input 和一個 output 消息。這兩個消息定義在 schema 中。

    這個文件有一點稍微與最初的文件不同:不是返回一個簡單的值,服務是返回一個指向新創建的 WS-Resource 的 EndpointReference。我們來看這在 SOAP 消息中是如何實現的。

    l         SOAP 請求

    關于創建 WS-Resource 的實際 SOAP 請求是非常簡單的:

    <SOAP-ENV:Envelope  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

        <SOAP-ENV:Header/>

        <SOAP-ENV:Body>

            <createSatellite xmlns="http://example.com/satellite"/>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    我們還沒有實際的對象,所以該請求到達 WSDL 文件中列出的 URI,并且我們將該請求定義為一個簡單的 createSatellite 元素。

    響應要稍微有意思一些。

    l         SOAP 響應

    一旦您發送針對新人造衛星的請求,服務器就創建一個對新 WS-Resource 的引用,并以 EndpointReference 形式將它返回:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

        <SOAP-ENV:Header/>

        <SOAP-ENV:Body>

           <wsa:EndpointReference

           xmlns:wsa="http://www.w3.org/2005/02/addressing"

                 xmlns:sat="http://example.org/satelliteSystem">

               <wsa:Address>http://example.com/satellite</wsa:Address>

               <wsa:ReferenceProperties>

                   <sat:SatelliteId>SAT9928</sat:SatelliteId>

               </wsa:ReferenceProperties>

           </wsa:EndpointReference>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    注意,EndpointReference 的 Address 元素指向我們在 WSDL 文件中列出的同一 URI,所以信息仍然到達相同的地方,只是量的增多。

    現在我們應該注意,這不是一個普通的端點引用。 ReferenceProperties 元素展示一個標識符,該標識符最終將用于識別 WS-Resource,所以這實際上是一個 WS-Resource 限定的端點引用。正如您馬上就會看到的,我們可以使用該信息來對 WS-Resource 做出后續調用。

    l         我們想要完成什么?

    好的,已經創建了 WS-Resource,那么我們可以對它做什么呢?

    實際上,可以通過調整它的屬性來做任何事情。例如,可以通過更改 altitude 屬性來改變人造衛星軌道的大小。不,只是更改值并不能移動人造衛星;人造衛星的實際移動是由 Web 服務背后的應用程序決定的。但是這是 WS-Resources 所真正關心的:創建一種方式,以便通過更改屬性來操縱對象。規范只是指出了如何將這些更改告訴 Web 服務。它不關心應用程序是如何真正操縱對象的,我們也不關心。

    但是在真正開始更改屬性之前,我們先來看看屬性。在本節中,首先來看我們在 創建 WS-Resource 中創建的人造衛星 WS-Resource 的 altitude 屬性。然后將講述一次性請求所有的 orientation 值。然后再介紹使用 XPath 來查詢多個值。

    l         請求屬性

    請求屬性的值是構造適當 SOAP 消息過程中的一個簡單過程。例如,假設我們想要請求 altitude 屬性的值。基本的 SOAP 消息可能看起來像下面這樣:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

        xmlns:wsrp=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-

    ResourceProperties-1.2-draft-01.xsd">

     <SOAP-ENV:Header>...</SOAP-ENV:Header>

     <SOAP-ENV:Body>

         <wsrp:GetResourceProperty xmlns:satProp=

         "http://example.com/satellite">

            satProp:altitude

         </wsrp:GetResourceProperty>

     </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    wsrp:GetResourceProperty 元素是 WS-ResourceProperties 規范的一部分。我們甚至不必在 WSDL 文件中定義它。它給我們這樣一個地方,可以指定我們想要獲得值的屬性。

    但是,SOAP 消息還沒有真正完成。是的,它是一個 SOAP 消息,但是如果我們像這樣把它發送給 Web 服務,服務將不知道我們引用的是哪個 WS-Resource。接下來我們將關心這個問題。

    l         完全的 SOAP 請求

    在前一屏,即 請求屬性 中,我們創建了一個 SOAP 消息,它指定了我們想要檢索的屬性,但是為哪個 WS-Resource 創建的呢?

    當創建人造衛星時,Web 服務返回一個指向新創建的 WS-Resource 的端點引用。我們可以將該信息添加到 SOAP 消息的 Header,像下面這樣:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

           xmlns:sat="http://example.org/satelliteSystem"

           xmlns:wsa="http://www.w3.org/2005/02/addressing"

           xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/wsrf-

           WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

    http://docs.oasis-open.org/wsrf/2004/06/WS-ResourceProperties/GetResourceProperty

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/satellite

           </wsa:To>

           <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:GetResourceProperty

           xmlns:satProp="http://example.com/satellite">

              satProp:altitude

           </wsrp:GetResourceProperty>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    wsa:Action 元素不是初始端點引用的一部分;它隨我們想要做的事情而變化。在本例中,我們使用 GetResourceProperty 操作。wsa:To 元素從端點引用中的 wsa:Address 獲得值,而任何 wsa:ReferenceProperty 值都直接包含在 Header 中。

    您會注意到,我們沒有討論 SatelliteId 值。這是故意的。包含在端點引用中的任何用于識別特定 WS-Resource 的信息都必須被應用程序忽略,只是在發送消息時要傳送它。根據規范,即使嘗試去解釋值也認為是不適當的。這意味著被傳輸為一個“黑盒”,導致一種未經檢查的生活。

    l         接收 ResourceProperty

    一旦請求屬性,就需要獲得返回值,并且 WS-ResourceProperties 規范也定義了這種消息的形式。在我們的例子中,我們將接收這樣一個消息:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV=

    "http://schemas.xmlsoap.org/soap/envelope/"

         xmlns:sat="http://example.org/satelliteSystem"

         xmlns:wsa="http://www.w3.org/2005/02/addressing"

         xmlns:wsrp=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-

    ResourceProperties-1.2-draft-01.xsd">

     <SOAP-ENV:Header>

           <wsa:Action>

    http://docs.oasis-open.org/wsrf/2004/06/WS-

    ResourceProperties/GetResourcePropertyResponse

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/myClient

           </wsa:To>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:GetResourcePropertyResponse

                 xmlns:satProp=

                 "http://example.com/satellite">

              <satProp:altitude>

              47700

              </satProp:altitude>

           </wsrp:GetResourcePropertyResponse>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    在本例中,Header 信息并不引用服務,而是引用客戶機; http://example.com/myClient 是應該接收響應的客戶機的 URI。在大多數情況下,這與發出請求的客戶機是相同的,但是您也可以使用 wsa:Reply-To 元素將響應發送到別的地方。

    在實際的消息體中還有一個標準元素 wsrp:GetResourcePropertyResponse,但是在本例中,它包含請求的實際屬性,以及它的當前值。

    現在來看這在 WSDL 文件中是什么樣的。

    l         WSDL 文件

    為了將這些功能添加到應用程序,我們需要將它們添加到 WSDL 文件,但是因為我們使用已經定義好的標準消息交換模式,所以我們只需要添加一個新操作,像下面這樣:

    <?xml version="1.0" encoding="UTF-8"?>

    <definitions name="Satellite"

        targetNamespace="http://example.com/satellite"

        xmlns="http://schemas.xmlsoap.org/wsdl/"

        xmlns:tns="http://example.com/satellite"

        xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing"

        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

        xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/

        wsrf-WS-ResourceProperties-1.2-draft-01.xsd"

        xmlns:wsrpwsdl=

        "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-

        ResourceProperties-1.2-draft-01.wsdl"

        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

       

        <wsdl:import namespace=

    "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

                  location="WS-ResourceProperties.wsdl" />

       

     <types>

          <xsd:schema targetNamespace="http://example.com/satellite"

              xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     ...

         </xsd:schema>

           

        </types>

     <message name="CreateSatelliteRequest">

          <part name="request" element="tns:createSatellite"/>

     </message>

       

     <message name="CreateSatelliteResponse">

          <part name="response" element="tns:createSatelliteResponse"/>

     </message>

       

     <portType name="SatellitePortType"

               wsrp:ResourceProperties="tns:GenericSatelliteProperties">

          <operation name="createSatellite">

              <input message="tns:CreateSatelliteRequest"

                  wsa:Action="http://example.com/CreateSatellite" />

              <output message="tns:CreateSatelliteResponse"

                  wsa:Action="http://example.com/CreateSatelliteResponse" />

          </operation>

          <operation name="getAltitude">

              <input message="wsrpwsdl:GetResourcePropertyRequest"

                  wsa:Action="http://docs.oasis-open.org/wsrf/2004/

                  06/WS-ResourceProperties/GetResourceProperty/>

                <output message="wsrpwsdl:GetResourcePropertyResponse"

                    wsa:Action="http://docs.oasis-open.org/wsrf/2004/

                    06/WS-ResourceProperties/GetResourcePropertyResponse/>

            </operation>

        </portType>

        <binding name="SatelliteSoapBinding" type="tns:SatellitePortType">

            <soap:binding style="document"

                transport="http://schemas.xmlsoap.org/soap/http"/>

            <operation name="createSatellite">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="getAltitude">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

        </binding>

        <service name="SatelliteService">

            <port name="SatellitePort" binding=

            "tns:SatelliteSoapBinding">

                <soap:address location=

                "http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    一開始是 portType,我們創建一個叫做 getAltitude 的新操作。該操作具有一個 input 和一個 output 消息,但是兩個消息都已經定義在我們以前導入的 WS-ReferenceProperties.wsdl 文件中,所以我們需要做的就是使用適當的名稱空間別名來引用它們。

    一旦創建了 operation,我們只要將它添加到 binding 就行了,而這一點我們已經很內行了。

    l         請求多個屬性

    幸運的是,我們并不局限于檢索單個屬性值。我們也可以檢索多個屬性:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

            xmlns:sat="http://example.org/satelliteSystem"

            xmlns:wsa="http://www.w3.org/2005/02/addressing"

            xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-Resour

    ceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/

                WS-ResourceProperties/GetMultipleResourceProperties

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/satellite

           </wsa:To>

           <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:GetMultipleResourceProperties

                  xmlns:satProp="http://example.com/satellite">

              <wsrp:ResourceProperty>

              satProp:roll

              </wsrp:ResourceProperty>

              <wsrp:ResourceProperty>

              satProp:pitch

              </wsrp:ResourceProperty>

              <wsrp:ResourceProperty>

              satProp:yaw

              </wsrp:ResourceProperty>

           </wsrp:GetMultipleResourceProperties>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    同前面一樣,Header 中的信息來自端點引用。

    l         接收多個屬性

    響應消息類似于它的單個屬性對應物:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

             xmlns:sat="http://example.org/satelliteSystem"

             xmlns:wsa="http://www.w3.org/2005/02/addressing"

             xmlns:wsrp="http://docs.oasis-open.org/wsrf/200

             4/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/WS-Res

                ourceProperties/GetMultipleResourcePropertiesResponse

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/myClient

           </wsa:To>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:GetMultipleResourcePropertiesResponse    

                   xmlns:satProp="http://example.com/satellite">

              <satProp:roll>32</satProp:roll>

              <satProp:pitch>49</satProp:pitch>                    

              <satProp:yaw>0</satProp:yaw>

           </wsrp:GetMultipleResourcePropertiesResponse>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    同樣,我們要將它添加到 WSDL 文件。

    l         WSDL 文件

    同樣,因為我們傳遞的實際消息已經定義在 WS-ResourceProperties.wsdl 文件中,把該功能添加到應用程序所要做的就是創建一個新 operation:

    ...

        <message name="CreateSatelliteRequest">

            <part name="request" element=

            "tns:createSatellite"/>

        </message>

       

       <message name="CreateSatelliteResponse">

            <part name="response" element=

            "tns:createSatelliteResponse"/>

        </message>

       

        <portType name="SatellitePortType"

               wsrp:ResourceProperties =

               "tns:GenericSatelliteProperties">

            <operation name="createSatellite">

                <input message="tns:CreateSatelliteRequest"

                     wsa:Action=

                     "http://example.com/CreateSatellite" />

                <output message="tns:CreateSatelliteResponse"

                     wsa:Action=

                     "http://example.com/CreateSatelliteResponse" />

            </operation>

           

            <operation name="getAltitude">

                <input message=

                "wsrpwsdl:GetResourcePropertyRequest"

                     wsa:Action="http://docs.oasis-open.org/ws

                     rf/2004/06/WS-ResourceProperties/GetResourceProperty"/>

                <output message=

                "wsrpwsdl:GetResourcePropertyResponse"

                     wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

                     6/WS-ResourceProperties/GetResourcePropertyResponse"/>

            </operation>

           

            <operation name="getOrientation">

                <input message="wsrpwsdl:

                GetMultipleResourcePropertiesRequest"

                     wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

                     6/WS-ResourceProperties/GetMultipleResourceProperties"/>

                <output message=

                "wsrpwsdl:GetMultipleResourcePropertiesResponse"

                     wsa:Action=

                     "http://docs.oasis-open.org/wsrf/2004/06/WS-ResourcePro

                     perties/GetMultipleResourcePropertiesResponse"/>

            </operation>

           

        </portType>

       

        <binding name="SatelliteSoapBinding" type=

        "tns:SatellitePortType">

           <soap:binding style="document"

                  transport=

                  "http://schemas.xmlsoap.org/soap/http"/>

            <operation name="createSatellite">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="getAltitude">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="getOrientation">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

        </binding>

       

        <service name="SatelliteService">

            <port name="SatellitePort" binding="tns:SatelliteSoapBinding">

                <soap:address location="http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    一旦我們將 getOrientation operation 添加到了 binding,它就可以由應用程序使用了。

    我們也可以查詢屬性,正如您馬上就會看到的。

    l         使用 XPath 查詢

    盡管請求所需的屬性相當直觀,但是也有需要使用其他方法的情景。不是簡單地按名稱請求資源屬性,我們可以使用 XPath 中可用的查詢。(有關 XPath 的更多信息,請參閱 參考資料。)例如,如果不能確定某個特定的屬性是否存在,您可能想要在屬性上使用 XPath 函數。

    XPath 的另一個有用功能是,能夠請求多個相同命名的屬性,或者甚至能夠查詢參數。我們將在“理解 WSRF(第 2 部分)”中做前一件事,而馬上就會做后一件事。例如,我們可以與 boolean() 函數一起使用一個 XPath 表達式,來確定人造衛星是否指向正確的方向,而不用顯式地分析數據:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV=

    "http://schemas.xmlsoap.org/soap/envelope/"

            xmlns:sat="http://example.org/satelliteSystem"

            xmlns:wsa="http://www.w3.org/2005/02/addressing"

            xmlns:wsrp="http://docs.oasis-open.org/wsrf/20

            04/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/WS-ResourcePro

                perties/QueryResourceProperties

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/satellite

           </wsa:To>

           <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:QueryResourceProperties>

              <wsrp:QueryExpression Dialect=

              "http://www.w3.org/TR/1999/REC-xpath-19991116">

                 boolean(/*/pitch=25 and /*/roll=0 and /*/yaw=10)

              </wsrp:QueryExpression>

           </wsrp:QueryResourceProperties>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    注意 Dialect 屬性的使用,以區分 XPath V1.0 (這里展示的)和 XPath V2.0(http://www.w3.org/TR/2003/WD-xpath20-20031112)。規范沒有限制您可以支持的方言(dialect),但是如果實現不認識 Dialect,它就會返回一個 fault 和錯誤。

    l         查詢結果

    結果看起來非常像前面的兩個響應:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV=

    "http://schemas.xmlsoap.org/soap/envelope/"

            xmlns:sat="http://example.org/satelliteSystem"

            xmlns:wsa="http://www.w3.org/2005/02/addressing"

            xmlns:wsrp="http://docs.oasis-open.org/wsrf/20

            04/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/

                WS-ResourceProperties/Quer

    yResourcePropertiesResponse

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/myClient

           </wsa:To>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:QueryResourcePropertiesResponse>

               false

           </wsrp:QueryResourcePropertiesResponse>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    在本例中,我們只是返回一個布爾值,但是您可以返回 XPath 可以返回的任何類型的值。

    l         WSDL 文件

    同樣,我們向 WSDL 文件添加一個新 operation:

    ...

        <portType name="SatellitePortType"

              wsrp:ResourceProperties=

              "tns:GenericSatelliteProperties">

    ...

          <operation name="getOrientation">

              <input message=

              "wsrpwsdl:GetMultipleResourcePropertiesRequest"

                  wsa:Action="http://docs.oasis-open.org/wsrf/20

                04/06/WS-ResourceProperties/GetMultipleResourceProperties"/>

              <output message=

              "wsrpwsdl:GetMultipleResourcePropertiesResponse"

                  wsa:Action="http://docs.oasis-open.org/wsrf/2004/06/W

              S-ResourceProperties/GetMultipleResourcePropertiesResponse"/>

          </operation>

           

            <operation name="checkOrientation">

                <input message=

                "wsrpwsdl:QueryResourcePropertiesRequest"

                    wsa:Action="http://docs.oasis-open.org/wsrf/20

                    04/06/WS-ResourceProperties/QueryResourceProperties"/>

                <output message=

                "wsrpwsdl:QueryResourcePropertiesResponse"

                    wsa:Action="http://docs.oasis-open.org/wsrf/2004/06/W

                 S-ResourceProperties/QueryResourcePropertiesResponse"/>

            </operation>

           

        </portType>

       

        <binding name="SatelliteSoapBinding" type=

        "tns:SatellitePortType">

            <soap:binding style="document"

                transport="http://schemas.xmlsoap.org/soap/http"/>

    ...

            <operation name="getOrientation">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                   <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="checkOrientation">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

        </binding>

       

        <service name="SatelliteService">

            <port name="SatellitePort" binding=

            "tns:SatelliteSoapBinding">

                <soap:address location=

                "http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    這關注檢索屬性?,F在來看設置屬性的值。

    l         我們想要完成什么

    至此,我們創建了一個 WS-Resource,并且了解了一個或多個用于表示它的狀態的屬性。但是這與真正地操縱 WS-Resource 沒有太大的關系。在本節中,我們來看添加、懸掛和刪除 WS-Resource 的屬性。

    至此,我們的人造衛星已經在天空中相對于地球的軌道中穩定了,而不是觀測到某些特定的東西了。在本節中,我們要添加一個 ResourceProperty,它表示一個特定的目標。然后我們通過更新位置屬性將人造衛星移向該目標,然后再刪除所創建的目標屬性。最后,通過將適當的操作添加到 WSDL 文件,我們將把所有事情綜合在一起。

    l         添加屬性

    向 WS-Resource 添加屬性涉及到使用 Insert 元素:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

             xmlns:sat="http://example.org/satelliteSystem"

             xmlns:wsa="http://www.w3.org/2005/02/addressing"

             xmlns:wsrp="http://docs.oasis-open.org/wsrf/

         2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

               http://docs.oasis-open.org/wsrf/2004/06/W

               S-ResourceProperties/SetResourceProperties

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/satellite

           </wsa:To>

           <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:SetResourceProperties

                  xmlns:satProp="http://example.com/satellite">

               <wsrp:Insert>

                   <satProp:targetCoords>

                   36n11, 115w08

                   </satProp:targetCoords>

               </wsrp:Insert>

           </wsrp:SetResourceProperties>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    我們可以給這個新屬性取任何好聽的名字,但是我們必須允許該新元素在 Resource Properties 文檔中。(當我們在 WSDL 文件 中調整 WSDL 文件時將來做這一件事。)

    l         添加屬性的結果

    當成功添加、刪除或更改屬性之后,我們將會得到一個只是確認操作的響應消息:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV=

    "http://schemas.xmlsoap.org/soap/envelope/"

              xmlns:sat="http://example.org/satelliteSystem"

              xmlns:wsa="http://www.w3.org/2005/02/addressing"

              xmlns:wsrp="http://docs.oasis-open.org/wsrf/

        2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/W

        S-ResourceProperties/SetResourcePropertiesResponse

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/myClient

           </wsa:To>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:SetResourcePropertiesResponse>

           </wsrp:SetResourcePropertiesResponse>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    該響應與 Update 和 Delete 操作產生的響應是相同的。

    l         更改屬性值

    在 添加屬性 中,我們添加了一個新屬性,但是我們也能更改現有屬性的值。例如,我們可以告訴系統,通過使用 Update 元素更改人造衛星的位置屬性來移動人造衛星:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV=

    "http://schemas.xmlsoap.org/soap/envelope/"

           xmlns:sat="http://example.org/satelliteSystem"

           xmlns:wsa="http://www.w3.org/2005/02/addressing"

           xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/0

        6/wsrf-WS-ResourceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

                http://docs.oasis-open.org/wsrf/2004/06/WS-Re

           sourceProperties/SetResourceProperties

          </wsa:Action>

          <wsa:To SOAP-ENV:mustUnderstand="1">

               http://example.com/satellite

          </wsa:To>

         <sat:SatelliteId>SAT9928</sat:SatelliteId>

     </SOAP-ENV:Header>

     <SOAP-ENV:Body>

         <wsrp:SetResourceProperties

                 xmlns:satProp="http://example.com/satellite">

             <wsrp:Update>

                <satProp:latitude>36.11</satProp:latitude>

             </wsrp:Update>

             <wsrp:Update>

                <satProp:longitude>158.08</satProp:latitude>

             </wsrp:Update>

          </wsrp:SetResourceProperties>

       </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    在本例中,我們使用了兩個 Update 組件,但是我們實際上可以使用 Insert、Update 和 Delete 組件的任意組合。

    l         刪除屬性

    屬性可以被完全刪除。例如,如果我們決定不再觀測某個特定的目標,就可以刪除該屬性:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

             xmlns:sat="http://example.org/satelliteSystem"

             xmlns:wsa="http://www.w3.org/2005/02/addressing"

             xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-Resour

    ceProperties-1.2-draft-01.xsd">

        <SOAP-ENV:Header>

           <wsa:Action>

             http://docs.oasis-open.org/wsrf/2004/06/WS-Resou

             rceProperties/SetResourceProperties

           </wsa:Action>

           <wsa:To SOAP-ENV:mustUnderstand="1">

                http://example.com/satellite

           </wsa:To>

           <sat:SatelliteId>SAT9928</sat:SatelliteId>

        </SOAP-ENV:Header>

        <SOAP-ENV:Body>

           <wsrp:SetResourceProperties

                    xmlns:satProp="http://example.com/satellite">

               <wsrp:Delete resourceProperty="targetCoords"/>

           </wsrp:SetResourceProperties>

        </SOAP-ENV:Body>

    </SOAP-ENV:Envelope>

    同樣,我們的資源屬性文檔的模式定義必須允許我們做這一更改。

    l         WSDL 文件

    同樣,因為標準元素已經定義在 WS-ResourceProperties.wsdl 中,所以我們可以簡單地添加新操作:

    <?xml version="1.0" encoding="UTF-8"?>

    <definitions name="Satellite"

        targetNamespace="http://example.com/satellite"

        xmlns="http://schemas.xmlsoap.org/wsdl/"

        xmlns:tns="http://example.com/satellite"

        xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing"

        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

        xmlns:wsrp="http://docs.oasis-open.org/wsrf/2004/06/

        wsrf-WS-ResourceProperties-1.2-draft-01.xsd"

        xmlns:wsrpwsdl="http://docs.oasis-open.org/wsrf/200

    4/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

       

        <wsdl:import namespace="http://docs.oasis-open.org/

     wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.wsdl"

            location="WS-ResourceProperties.wsdl" />

       

        <types>

            <xsd:schema

              targetNamespace="http://example.com/satellite"

                xmlns:xsd="http://www.w3.org/2001/XMLSchema">

               

                <xsd:import namespace=

          "http://schemas.xmlsoap.org/ws/2004/03/addressing"

                       schemaLocation="WS-Addressing.xsd" />

               

               <xsd:element name="createSatellite">

                   <xsd:complexType/>

               </xsd:element>

               

               <xsd:element name="createSatelliteResponse">

                   <xsd:complexType>

                       <xsd:sequence>

                           <xsd:element ref=

                           "wsa:EndpointReference"/>

                       </xsd:sequence>

                   </xsd:complexType>

               </xsd:element>

               

              <xsd:element name="latitude" type="xsd:float" />

              <xsd:element name="longitude" type="xsd:float" />

              <xsd:element name="altitude" type="xsd:float" />

              <xsd:element name="pitch" type="xsd:float" />

              <xsd:element name="yaw" type="xsd:float" />

              <xsd:element name="roll" type="xsd:float" />

              <xsd:element name="focalLength" type="xsd:float" />

              <xsd:element name="currentView" type="xsd:string" />

               

              <xsd:element name="GenericSatelliteProperties">

                  <xsd:complexType>

                      <xsd:sequence>

                          <xsd:element ref="latitude" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="longitude" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="altitude" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="pitch" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="yaw" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="roll" minOccurs="1"

                          maxOccurs="1"/>

                          <xsd:element ref="focalLength"

                          minOccurs="1" maxOccurs="1"/>

                          <xsd:element ref="currentView"

                          minOccurs="1" maxOccurs="1"/>

                          <xsd:any/>

                      </xsd:sequence>

                  </xsd:complexType>

               </xsd:element>

               

            </xsd:schema>

           

        </types>

        <message name="CreateSatelliteRequest">

            <part name="request" element="tns:createSatellite"/>

        </message>

       

        <message name="CreateSatelliteResponse">

            <part name="response" element=

            "tns:createSatelliteResponse"/>

        </message>

       

        <portType name="SatellitePortType"

                 wsrp:ResourceProperties=

                 "tns:GenericSatelliteProperties">

            <operation name="createSatellite">

                <input message="tns:CreateSatelliteRequest"

           wsa:Action="http://example.com/CreateSatellite" />

                <output message="tns:CreateSatelliteResponse"

                    wsa:Action=

              "http://example.com/CreateSatelliteResponse" />

            </operation>

           

            <operation name="getAltitude">

                <input message=

                "wsrpwsdl:GetResourcePropertyRequest"

                  wsa:Action="http://docs.oasis-open.org/wsr

    f/2004/06/WS-ResourceProperties/GetResourceProperty"/>

                <output message=

                "wsrpwsdl:GetResourcePropertyResponse"

          wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/GetResourcePropertyResponse"/>

            </operation>

           

            <operation name="getOrientation">

                <input message=

             "wsrpwsdl:GetMultipleResourcePropertiesRequest"

          wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/GetMultipleResourceProperties"/>

                <output message=

            "wsrpwsdl:GetMultipleResourcePropertiesResponse"

                          wsa:Action=

    "http://docs.oasis-open.org/wsrf/2004/06/WS-ResourceProp

    erties/GetMultipleResourcePropertiesResponse"/>

            </operation>

           

            <operation name="checkOrientation">

                <input message=

                    "wsrpwsdl:QueryResourcePropertiesRequest"

                   wsa:Action="http://docs.oasis-open.org/wsr

    f/2004/06/WS-ResourceProperties/QueryResourceProperties"/>

                <output message=

                   "wsrpwsdl:QueryResourcePropertiesResponse"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/QueryResourcePropertiesResponse"/>

            </operation>

           

            <operation name="addTarget">

                <input message=

                      "wsrpwsdl:SetResourcePropertiesRequest"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/SetResourceProperty"/>

                <output message=

                     "wsrpwsdl:SetResourcePropertiesResponse"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/SetResourcePropertyResponse"/>

            </operation>

            <operation name="moveToTarget">

                <input message=

                      "wsrpwsdl:SetResourcePropertiesRequest"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/SetResourceProperty"/>

                <output message=

                     "wsrpwsdl:SetResourcePropertiesResponse"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/SetResourcePropertyResponse"/>

            </operation>

            <operation name="removeTarget">

                <input message=

                      "wsrpwsdl:SetResourcePropertiesRequest"

                     wsa:Action="http://docs.oasis-open.org/ws

    rf/2004/06/WS-ResourceProperties/SetResourceProperty"/>

                <output message=

                     "wsrpwsdl:SetResourcePropertiesResponse"

           wsa:Action="http://docs.oasis-open.org/wsrf/2004/0

    6/WS-ResourceProperties/SetResourcePropertyResponse"/>

            </operation>

           

        </portType>

       

        <binding name="SatelliteSoapBinding"

        type="tns:SatellitePortType">

            <soap:binding style="document" transport=

                    "http://schemas.xmlsoap.org/soap/http"/>

            <operation name="createSatellite">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="getAltitude">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="getOrientation">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

           <operation name="checkOrientation">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="addTarget">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="moveToTarget">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

            <operation name="removeTarget">

                <input>

                    <soap:body use="literal"/>

                </input>

                <output>

                    <soap:body use="literal"/>

                </output>

            </operation>

        </binding>

       

        <service name="SatelliteService">

            <port name="SatellitePort"

            binding="tns:SatelliteSoapBinding">

                <soap:address location=

                "http://example.com/satellite"/>

            </port>

        </service>

    </definitions>

    還請注意,我們可以添加任意元素到 GenericSatelliteProperties,所以我們可以容易地添加新屬性,比如 targetCoords。

    l         結束語

    在本教程,也即 4 篇關于 WSRF 的系列文章中的第一篇文章中,我們一開始解釋了 WSRF 背后的目的,以及為什么單有 Web 服務還不夠。然后解釋了 WS-Resource 是有狀態資源(比如數據庫或衛星)與 Web 服務的組合。

    資源本身是由一系列屬性來描述的,這些屬性是與 WSDL 文件中的 Web 服務相關聯的。我們還介紹了 WSDL 和 WS-Addressing 的基礎,WSRF 使用 WS-Addressing 來指向一個特定的 WS-Resource 實例。

    我們介紹了創建 WS-Resources,了解了它們的屬性,以及調整這些屬性,以便操縱資源。

    在本系列的以后部分中,我們將會介紹 WSRF 的一些更高級的用途,比如 ServiceGroups 和錯誤處理,以及 WS-Notifications。在本系列的最后一部分中,我們將把所有內容綜合在一起,并編寫一個應用程序,它使用類來實現本系列前兩部分中討論的每個概念。

    l         參考資料

    Web 服務資源框架(Web Services Resource Framework,WSRF)涉及大量不同領域。下面是起步所需的一些參考資料:

    單擊 下載本教程中介紹的完整 WSDL 文件。

    WSRF 和相關規范

    WSRF 文檔 的主要位置在 Globus Alliance Web 站點上,但是最新的規范可以在 Oasis 處找到。文檔包括:

    Web 服務資源框架(白皮書)

    The WS-Resource Framework

    WS-ResourceProperties (WSRF-RP)

    WS-ResourceLifetime (WSRF-RL)

    WS-ServiceGroup (WSRF-SG)

    WS-Base Faults (WSRF-BF)

    Web 服務和相關的規范



    Author: orangelizq
    email: orangelizq@163.com

    歡迎大家訪問我的個人網站 萌萌的IT人
    posted on 2007-11-07 22:04 桔子汁 閱讀(1375) 評論(1)  編輯  收藏 所屬分類: Web Service

    評論:
    # re: 理解WSRF之一 使用WS-ResourceProperties (整理自IBM網站) 2007-11-23 10:34 | aier
    thans!  回復  更多評論
      
    主站蜘蛛池模板: 成人黄色免费网址| 亚洲一级毛片免费观看| 日本高清免费观看| 18女人水真多免费高清毛片| 亚洲网站在线免费观看| 成人免费午夜视频| 又粗又黄又猛又爽大片免费| 亚洲一区视频在线播放| 亚洲av午夜福利精品一区人妖| 亚洲色图在线观看| 亚洲免费闲人蜜桃| 国产AV日韩A∨亚洲AV电影| 久久精品免费网站网| 99爱在线观看免费完整版| 中文字幕无码视频手机免费看| 国产精品久免费的黄网站| 亚洲免费一区二区| 亚洲人成影院在线| 亚洲中文字幕乱码熟女在线| 国产亚洲精品免费| 久久精品成人免费网站| 成人毛片免费观看视频大全| 亚洲最大av无码网址| 亚洲欧洲日本精品| 免费人成视频在线观看免费| 日本高清免费观看| 日本黄色免费观看| 久久精品国产亚洲| 美女被免费网站在线视频免费| 日韩免费无码视频一区二区三区| 夜夜爽免费888视频| 亚洲成AV人在线观看天堂无码| 亚洲一区二区三区乱码在线欧洲| 国产大片免费天天看| 国产成人A在线观看视频免费| 亚洲精品无码专区2| 亚洲一区二区三区91| 丝袜足液精子免费视频| 免费视频中文字幕| 亚洲福利在线观看| 免费人成动漫在线播放r18|