Kafka 樣式的 soap 端點(diǎn)
Christopher Dix 所開(kāi)發(fā)的“Kafka — XSL SOAP 工具箱”(請(qǐng)參閱 參考資料)是一種用于構(gòu)造 SOAP 端點(diǎn)的 XSLT 框架。它只涵蓋了 SOAP 1.1,但 Kafka 端點(diǎn)演示了傳遞 UserLand SOAP 驗(yàn)證器(UserLand SOAP Validator)的能力,并且根據(jù) SOAP 1.2 對(duì)它進(jìn)行更新似乎并不太困難。 清單 1展示了一個(gè)樣本 Kafka 端點(diǎn):求兩數(shù)之和的 SOAP 服務(wù)器(一個(gè)典型而簡(jiǎn)單的 SOAP 樣本)。
清單 1. 求兩數(shù)之和的 Kafka SOAP 端點(diǎn)
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:method="http://www.topxml.com/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- add.xsl : Kafka SOAP Endpoint Example, with modifications --> <!-- Import soap.xsl to use the framework --> <xsl:import href="kafka/soap.xsl"/> <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes"/> <!-- Define the global variables for the framework --> <xsl:variable name="Method">Add</xsl:variable> <xsl:variable name="MethodNS">http://www.topxml.com/</xsl:variable> <!-- Add : Add two numbers and return the sum --> <!-- Function Add( A as Double, B as Double ) as Double --> <xsl:template name="ProcessPayload"> <xsl:param name="Payload"/> <xsl:for-each select="$Payload"> <!-- This is how to retrieve parameters from the input --> <xsl:variable name="A" select="number(A|method:A)"/> <xsl:variable name="B" select="number(B|method:B)"/> <!-- The WriteParameter template takes the qualified name for a response parameter as well as its value and a QName specifying the tpe (for the xsi:type attribute) --> <xsl:call-template name="WriteParameter"> <xsl:with-param name="p" select="'Result'"/> <xsl:with-param name="v" select="$A + $B"/> <xsl:with-param name="t" select="'xsd:double'"/> </xsl:call-template> </xsl:for-each> </xsl:template> </xsl:stylesheet>
XSLT 端點(diǎn)導(dǎo)入 SOAP 框架(文件 kafka/soap.xsl),然后設(shè)置該框架將要使用的參數(shù),并設(shè)置它在處理構(gòu)成 SOAP 消息的整個(gè) XML 文檔的過(guò)程中將要分派的模板。全局變量 Method 和 MethodNS 聲明了組成消息的 XML 元素。在處理完 SOAP 信封之后,該框架調(diào)用 ProcessPayload 模板,該模板傳入了 XML 主體的有效負(fù)載。 xsl:for-each 是將上下文切換成想要的節(jié)點(diǎn)的標(biāo)準(zhǔn)技巧。參數(shù) A 和 B 是使用簡(jiǎn)單 XPaths 從這個(gè)元素讀取的,而框架被再次調(diào)用以幫助寫(xiě)出響應(yīng)參數(shù)。 WriteParameter 模板讓您指定元素名稱、數(shù)據(jù)類型和每個(gè)輸出參數(shù)的值。本示例中的響應(yīng)值是將兩個(gè)輸入?yún)?shù)相加所得的結(jié)果。
將這個(gè)端點(diǎn)部署為服務(wù)器相當(dāng)于設(shè)置一個(gè) HTTP 偵聽(tīng)器。Python 的 BaseHTTPServer 模塊向您提供了所需的機(jī)制,能夠輕而易舉地處理該協(xié)議的 HTTP 部分。請(qǐng)參閱 清單 2。
新聞熱點(diǎn)
疑難解答
圖片精選