洛阳铲的日志

2010年10月24日

deeply play soap in ruby with SAVON or HandSoap

Filed under: ruby,web — 标签:, , , — 洛阳铲 @ 21:16

虽然soap4r是ruby的默认实现,但是恰如http://www.eggheadcafe.com/software/aspnet/36181571/soap-with-ruby-19-.aspx  批评的那样。文档奇缺,更新缓慢,最好的参考居然是code sample, 不过好消息是还有另外的选择:  SAVONHandSoap
而且在这儿也有SAVON和HandSoap的一个使用介绍:  http://blog.nofail.de/2010/01/savon-handsoap-shootout/

play soap with soap4r

Filed under: ruby,web — 标签:, , , , — 洛阳铲 @ 20:16

对于ruby而的soap言有两个库非常重要:soap4r这个库是NAKAMURA维护的获得ruby官方
支持的soap库,另外一个是基于soap4r的wss4r的库。遗憾的是这两个库,文档奇缺好消息是还有其他选择
最多的资料只能来自于google了,而且在soap4r的邮件组里面,NAKAMURA也常常会亲自解答一些
问题。搜索问题的时候加上NAKAMURA作为关键词之一,会有意想不到的惊喜。

soap4r无需安装,wss4r的安装略过,基础知识:

  1. wsdl学习笔记
  2. soap学习笔记

在soap4r的源代码的sample中有一个自定义header头的例子: sample/soapheader/authheader 和
sample/soapheader/soapext_basicauth两个例子。以authheader 来开始。server.rb略过。

client.rb

require ‘soap/rpc/driver’
require ‘soap/header/simplehandler’

server = ARGV.shift || ‘http://localhost:7000/’

class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
MyHeaderName = XSD::QName.new("http://schemas.xmlsoap.org/ws/2002/07/secext", "wsse")

def initialize(userid, passwd)
super(MyHeaderName)
@sessionid = nil
@userid = userid
@passwd = passwd
@mustunderstand = true
end

def on_simple_outbound
if @sessionid
{ "sessionid" => @sessionid }
else
{ "userid" => @userid, "passwd" => @passwd }
end
end

def on_simple_inbound(my_header, mustunderstand)
@sessionid = my_header["sessionid"]
end
end

ns = ‘http://tempuri.org/authHeaderPort’
serv = SOAP::RPC::Driver.new(server, ns)
serv.add_method(‘deposit’, ‘amt’)
serv.add_method(‘withdrawal’, ‘amt’)

serv.headerhandler << ClientAuthHeaderHandler.new(‘NaHi’, ‘passwd’)

serv.wiredump_dev = STDOUT

p serv.deposit(150)
p serv.withdrawal(120)

最后该程序发送的请求为:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<n1:wsse xmlns:n1="http://schemas.xmlsoap.org/ws/2002/07/secext"
env:mustUnderstand="1">
<n1:passwd>passwd</n1:passwd>
<n1:userid>NaHi</n1:userid>
</n1:wsse>
</env:Header>
<env:Body>
<n2:deposit xmlns:n2="http://tempuri.org/authHeaderPort"
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<amt xsi:type="xsd:int">150</amt>
</n2:deposit>
</env:Body>
</env:Envelope>

http://developer.searchmarketing.yahoo.com/docs/V7/gsg/requests.php

  1. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  2. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  3. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  4. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  6. < soapenv:Header >
  7. < wsse:Security >
  8. < wsse:UsernameToken >
  9. < wsse:Username > user </ wsse:Username >
  10. < wsse:Password > password </ wsse:Password >
  11. </ wsse:UsernameToken >
  12. </ wsse:Security >
  13. </ soapenv:Header >
  14. < soapenv:Body >
  15. </ soapenv:Body >
  16. </ soapenv:Envelope >

The preferred method is through the WS-Security extension, where both the username
and password elements are wrapped in the Security and UsernameToken elements,
as shown below. To use WS-Security you must also include the secext schema in the SOAP envelope,
here defined as wsse.

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  4. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  5. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  7. < soapenv:Header >
  8. < wsse:Security >
  9. < wsse:UsernameToken >
  10. < wsse:Username > user </ wsse:Username >
  11. < wsse:Password > password </ wsse:Password >
  12. </ wsse:UsernameToken >
  13. </ wsse:Security >
  14. </ soapenv:Header >
  15. < soapenv:Body >
  16. </ soapenv:Body >
  17. </ soapenv:Envelope >


If your SOAP toolkit does not support WS-Security, use the username and password elements in the header:
  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  4. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  5. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  7. < soapenv:Header >
  8. < username > user </ username >
  9. < password > password </ password >
  10. </ soapenv:Header >
  11. < soapenv:Body >
  12. </ soapenv:Body >
  13. </ soapenv:Envelope >

soap的tips:

  1. 如何删掉: env:mustUnderstand ?,两个两个方法,二选一

    1. 创建一个HTTP filter,用正则表达式来修改HTTP body: body.sub(/\s+[^:]+:mustUnderstand\S*/, ”)

    2. 如果使用svn的代码,可以在header_handle#on_outbound中使用self.mustunderstand = nil来关闭mustUnderstand属性

  2. 如何删掉: ns# namespace?

    1. http://www.google.com/url?sa=D&q=http://www.pluitsolutions.com/2007/08/10/remove-n1-namespace-for-soap4r/&usg=AFQjCNEYmZR3Xen_12paXCe4P7IscNa3Ag

    2. https://groups.google.com/group/soap4r/browse_thread/thread/aad6c8af7c0dc473/b421cd11225b1e6e?lnk=gst&q=header#b421cd11225b1e6e

  3. 如何自定义ns? 参见sample/howto/custom_ns。 请注意,系统自带的soap4r版本很低(1.5.5),建议升级至最新版。

    当前最新的是1.6.1. svn co http://dev.ctor.org/svn/soap4r/trunk soap4r-1.6.1

后记deeply play soap in ruby with SAVON or HandSoap

2010年03月29日

soap学习笔记

Filed under: web — 标签:, — HackGou @ 21:08

参考资料:

  1. http://www.w3school.com.cn/soap/soap_envelope.asp

soap是什么之类的废话就不扯了,直奔主题,按图索骥:

<?xml version=”1.0″?>
<soap:Envelope
xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

<soap:Header>
<m:Trans
xmlns:m=”http://www.w3school.com.cn/transaction/”
soap:mustUnderstand=”1″>234</m:Trans>
</soap:Header>
<soap:Body>
<m:GetPrice xmlns:m=”http://www.w3school.com.cn/prices”>
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>

</soap:Envelope>

soap必须:

  1. 使用命名空间: http://www.w3.org/2001/12/soap-envelope。 参见的命名是: soap-envelope或者soap 或者env
  2. 使用soap-envelope:encodingStyle指定文档中使用的数据类型: http://www.w3.org/2001/12/soap-encoding
  3. 必须使用Envelop根元素
  4. 可选的soap:Header元素。
    上面的例子包含了一个带有一个 “Trans” 元素的头部,它的值是 234,此元素的 “mustUnderstand” 属性的值是 “1”。
    SOAP 在默认的命名空间中 (“http://www.w3.org/2001/12/soap-envelope”) 定义了三个属性。这三个属性是:actor、
    mustUnderstand 以及 encodingStyle
    1. actor:

      通过沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者。
      并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,不过,另一个方面,
      也许打算传送给消息路径上的一个或多个端点。SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点。

    2. mustUnderstand 。

      SOAP 的 mustUnderstand 属性可用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的。
      假如您向 Header 元素的某个子元素添加了 “mustUnderstand=”1″,则它可指示处理此头部的接收者必须认可此元素。
      假如此接收者无法认可此元素,则在处理此头部时必须失效。

    3. encodingStyle。的 encodingStyle 属性在上一节中已解释过了
  5. 必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。必须
    使用http://schemas.xmlsoap.org/soap/encoding/这一命名空间作为encodingStyle
    比如下面这个Request:
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <env:Envelope xmlns:xsd=”http://www.w3.org/2001/XMLSchema
    xmlns:env=”http://schemas.xmlsoap.org/soap/envelope/
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>
    <env:Header>
    <n1:wsse xmlns:n1=”http://schemas.xmlsoap.org/ws/2002/07/secext
    env:mustUnderstand=”1″>
    <n1:sessionid>828000</n1:sessionid>
    </n1:wsse>
    </env:Header>
    <env:Body>
    <n2:withdrawal xmlns:n2=”http://tempuri.org/authHeaderPort
    env:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/“>
    <amt xsi:type=”xsd:int”>120</amt>
    </n2:withdrawal>
    </env:Body>
    </env:Envelope>
    他的response是:
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <env:Envelope xmlns:xsd=”http://www.w3.org/2001/XMLSchema
    xmlns:env=”http://schemas.xmlsoap.org/soap/envelope/
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>
    <env:Header>
    <n1:wsse xmlns:n1=”http://schemas.xmlsoap.org/ws/2002/07/secext
    env:mustUnderstand=”0″>
    <n1:sessionid>0</n1:sessionid>
    </n1:wsse>
    </env:Header>
    <env:Body>
    <n2:withdrawalResponse xmlns:n2=”http://tempuri.org/authHeaderPort
    env:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/“>
    <return xsi:type=”xsd:string”>withdrawal 120 OK</return>
    </n2:withdrawalResponse>
    </env:Body>
    </env:Envelope>
  6. 可选的Fault元素用来指示错误的

soap响应看起来是这样的:

<?xml version=”1.0″?>
<soap:Envelope
xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

<soap:Body>
<m:GetPriceResponse xmlns:m=”http://www.w3school.com.cn/prices”>
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>

</soap:Envelope>

Del.icio.us : ,

Powered by WordPress