1 | # SOAP4R - SOAP RPC driver
|
---|
2 | # Copyright (C) 2000, 2001, 2003-2005 NAKAMURA, Hiroshi <[email protected]>.
|
---|
3 |
|
---|
4 | # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
|
---|
5 | # redistribute it and/or modify it under the same terms of Ruby's license;
|
---|
6 | # either the dual license version in 2003, or any later version.
|
---|
7 |
|
---|
8 |
|
---|
9 | require 'soap/soap'
|
---|
10 | require 'soap/mapping'
|
---|
11 | require 'soap/mapping/wsdlliteralregistry'
|
---|
12 | require 'soap/rpc/rpc'
|
---|
13 | require 'soap/rpc/proxy'
|
---|
14 | require 'soap/rpc/element'
|
---|
15 | require 'soap/streamHandler'
|
---|
16 | require 'soap/property'
|
---|
17 | require 'soap/header/handlerset'
|
---|
18 |
|
---|
19 |
|
---|
20 | module SOAP
|
---|
21 | module RPC
|
---|
22 |
|
---|
23 |
|
---|
24 | class Driver
|
---|
25 | class << self
|
---|
26 | if RUBY_VERSION >= "1.7.0"
|
---|
27 | def __attr_proxy(symbol, assignable = false)
|
---|
28 | name = symbol.to_s
|
---|
29 | define_method(name) {
|
---|
30 | @proxy.__send__(name)
|
---|
31 | }
|
---|
32 | if assignable
|
---|
33 | aname = name + '='
|
---|
34 | define_method(aname) { |rhs|
|
---|
35 | @proxy.__send__(aname, rhs)
|
---|
36 | }
|
---|
37 | end
|
---|
38 | end
|
---|
39 | else
|
---|
40 | def __attr_proxy(symbol, assignable = false)
|
---|
41 | name = symbol.to_s
|
---|
42 | module_eval <<-EOS
|
---|
43 | def #{name}
|
---|
44 | @proxy.#{name}
|
---|
45 | end
|
---|
46 | EOS
|
---|
47 | if assignable
|
---|
48 | module_eval <<-EOS
|
---|
49 | def #{name}=(value)
|
---|
50 | @proxy.#{name} = value
|
---|
51 | end
|
---|
52 | EOS
|
---|
53 | end
|
---|
54 | end
|
---|
55 | end
|
---|
56 | end
|
---|
57 |
|
---|
58 | __attr_proxy :endpoint_url, true
|
---|
59 | __attr_proxy :mapping_registry, true
|
---|
60 | __attr_proxy :default_encodingstyle, true
|
---|
61 | __attr_proxy :generate_explicit_type, true
|
---|
62 | __attr_proxy :allow_unqualified_element, true
|
---|
63 | __attr_proxy :headerhandler
|
---|
64 | __attr_proxy :streamhandler
|
---|
65 | __attr_proxy :test_loopback_response
|
---|
66 | __attr_proxy :reset_stream
|
---|
67 |
|
---|
68 | attr_reader :proxy
|
---|
69 | attr_reader :options
|
---|
70 | attr_accessor :soapaction
|
---|
71 |
|
---|
72 | def inspect
|
---|
73 | "#<#{self.class}:#{@proxy.inspect}>"
|
---|
74 | end
|
---|
75 |
|
---|
76 | def httpproxy
|
---|
77 | options["protocol.http.proxy"]
|
---|
78 | end
|
---|
79 |
|
---|
80 | def httpproxy=(httpproxy)
|
---|
81 | options["protocol.http.proxy"] = httpproxy
|
---|
82 | end
|
---|
83 |
|
---|
84 | def wiredump_dev
|
---|
85 | options["protocol.http.wiredump_dev"]
|
---|
86 | end
|
---|
87 |
|
---|
88 | def wiredump_dev=(wiredump_dev)
|
---|
89 | options["protocol.http.wiredump_dev"] = wiredump_dev
|
---|
90 | end
|
---|
91 |
|
---|
92 | def mandatorycharset
|
---|
93 | options["protocol.mandatorycharset"]
|
---|
94 | end
|
---|
95 |
|
---|
96 | def mandatorycharset=(mandatorycharset)
|
---|
97 | options["protocol.mandatorycharset"] = mandatorycharset
|
---|
98 | end
|
---|
99 |
|
---|
100 | def wiredump_file_base
|
---|
101 | options["protocol.wiredump_file_base"]
|
---|
102 | end
|
---|
103 |
|
---|
104 | def wiredump_file_base=(wiredump_file_base)
|
---|
105 | options["protocol.wiredump_file_base"] = wiredump_file_base
|
---|
106 | end
|
---|
107 |
|
---|
108 | def initialize(endpoint_url, namespace = nil, soapaction = nil)
|
---|
109 | @namespace = namespace
|
---|
110 | @soapaction = soapaction
|
---|
111 | @options = setup_options
|
---|
112 | @wiredump_file_base = nil
|
---|
113 | @proxy = Proxy.new(endpoint_url, @soapaction, @options)
|
---|
114 | end
|
---|
115 |
|
---|
116 | def loadproperty(propertyname)
|
---|
117 | unless options.loadproperty(propertyname)
|
---|
118 | raise LoadError.new("No such property to load -- #{propertyname}")
|
---|
119 | end
|
---|
120 | end
|
---|
121 |
|
---|
122 | def add_rpc_method(name, *params)
|
---|
123 | add_rpc_method_with_soapaction_as(name, name, @soapaction, *params)
|
---|
124 | end
|
---|
125 |
|
---|
126 | def add_rpc_method_as(name, name_as, *params)
|
---|
127 | add_rpc_method_with_soapaction_as(name, name_as, @soapaction, *params)
|
---|
128 | end
|
---|
129 |
|
---|
130 | def add_rpc_method_with_soapaction(name, soapaction, *params)
|
---|
131 | add_rpc_method_with_soapaction_as(name, name, soapaction, *params)
|
---|
132 | end
|
---|
133 |
|
---|
134 | def add_rpc_method_with_soapaction_as(name, name_as, soapaction, *params)
|
---|
135 | param_def = SOAPMethod.create_rpc_param_def(params)
|
---|
136 | qname = XSD::QName.new(@namespace, name_as)
|
---|
137 | @proxy.add_rpc_method(qname, soapaction, name, param_def)
|
---|
138 | add_rpc_method_interface(name, param_def)
|
---|
139 | end
|
---|
140 |
|
---|
141 | # add_method is for shortcut of typical rpc/encoded method definition.
|
---|
142 | alias add_method add_rpc_method
|
---|
143 | alias add_method_as add_rpc_method_as
|
---|
144 | alias add_method_with_soapaction add_rpc_method_with_soapaction
|
---|
145 | alias add_method_with_soapaction_as add_rpc_method_with_soapaction_as
|
---|
146 |
|
---|
147 | def add_document_method(name, soapaction, req_qname, res_qname)
|
---|
148 | param_def = SOAPMethod.create_doc_param_def(req_qname, res_qname)
|
---|
149 | @proxy.add_document_method(soapaction, name, param_def)
|
---|
150 | add_document_method_interface(name, param_def)
|
---|
151 | end
|
---|
152 |
|
---|
153 | def add_rpc_operation(qname, soapaction, name, param_def, opt = {})
|
---|
154 | @proxy.add_rpc_operation(qname, soapaction, name, param_def, opt)
|
---|
155 | add_rpc_method_interface(name, param_def)
|
---|
156 | end
|
---|
157 |
|
---|
158 | def add_document_operation(soapaction, name, param_def, opt = {})
|
---|
159 | @proxy.add_document_operation(soapaction, name, param_def, opt)
|
---|
160 | add_document_method_interface(name, param_def)
|
---|
161 | end
|
---|
162 |
|
---|
163 | def invoke(headers, body)
|
---|
164 | if headers and !headers.is_a?(SOAPHeader)
|
---|
165 | headers = create_header(headers)
|
---|
166 | end
|
---|
167 | set_wiredump_file_base(body.elename.name)
|
---|
168 | env = @proxy.invoke(headers, body)
|
---|
169 | if env.nil?
|
---|
170 | return nil, nil
|
---|
171 | else
|
---|
172 | return env.header, env.body
|
---|
173 | end
|
---|
174 | end
|
---|
175 |
|
---|
176 | def call(name, *params)
|
---|
177 | set_wiredump_file_base(name)
|
---|
178 | @proxy.call(name, *params)
|
---|
179 | end
|
---|
180 |
|
---|
181 | private
|
---|
182 |
|
---|
183 | def set_wiredump_file_base(name)
|
---|
184 | if @wiredump_file_base
|
---|
185 | @proxy.set_wiredump_file_base("#{@wiredump_file_base}_#{name}")
|
---|
186 | end
|
---|
187 | end
|
---|
188 |
|
---|
189 | def create_header(headers)
|
---|
190 | header = SOAPHeader.new()
|
---|
191 | headers.each do |content, mustunderstand, encodingstyle|
|
---|
192 | header.add(SOAPHeaderItem.new(content, mustunderstand, encodingstyle))
|
---|
193 | end
|
---|
194 | header
|
---|
195 | end
|
---|
196 |
|
---|
197 | def setup_options
|
---|
198 | if opt = Property.loadproperty(::SOAP::PropertyName)
|
---|
199 | opt = opt["client"]
|
---|
200 | end
|
---|
201 | opt ||= Property.new
|
---|
202 | opt.add_hook("protocol.mandatorycharset") do |key, value|
|
---|
203 | @proxy.mandatorycharset = value
|
---|
204 | end
|
---|
205 | opt.add_hook("protocol.wiredump_file_base") do |key, value|
|
---|
206 | @wiredump_file_base = value
|
---|
207 | end
|
---|
208 | opt["protocol.http.charset"] ||= XSD::Charset.xml_encoding_label
|
---|
209 | opt["protocol.http.proxy"] ||= Env::HTTP_PROXY
|
---|
210 | opt["protocol.http.no_proxy"] ||= Env::NO_PROXY
|
---|
211 | opt
|
---|
212 | end
|
---|
213 |
|
---|
214 | def add_rpc_method_interface(name, param_def)
|
---|
215 | param_count = RPC::SOAPMethod.param_count(param_def,
|
---|
216 | RPC::SOAPMethod::IN, RPC::SOAPMethod::INOUT)
|
---|
217 | add_method_interface(name, param_count)
|
---|
218 | end
|
---|
219 |
|
---|
220 | def add_document_method_interface(name, param_def)
|
---|
221 | param_count = RPC::SOAPMethod.param_count(param_def, RPC::SOAPMethod::IN)
|
---|
222 | add_method_interface(name, param_count)
|
---|
223 | end
|
---|
224 |
|
---|
225 | if RUBY_VERSION > "1.7.0"
|
---|
226 | def add_method_interface(name, param_count)
|
---|
227 | ::SOAP::Mapping.define_singleton_method(self, name) do |*arg|
|
---|
228 | unless arg.size == param_count
|
---|
229 | raise ArgumentError.new(
|
---|
230 | "wrong number of arguments (#{arg.size} for #{param_count})")
|
---|
231 | end
|
---|
232 | call(name, *arg)
|
---|
233 | end
|
---|
234 | self.method(name)
|
---|
235 | end
|
---|
236 | else
|
---|
237 | def add_method_interface(name, param_count)
|
---|
238 | instance_eval <<-EOS
|
---|
239 | def #{name}(*arg)
|
---|
240 | unless arg.size == #{param_count}
|
---|
241 | raise ArgumentError.new(
|
---|
242 | "wrong number of arguments (\#{arg.size} for #{param_count})")
|
---|
243 | end
|
---|
244 | call(#{name.dump}, *arg)
|
---|
245 | end
|
---|
246 | EOS
|
---|
247 | self.method(name)
|
---|
248 | end
|
---|
249 | end
|
---|
250 | end
|
---|
251 |
|
---|
252 |
|
---|
253 | end
|
---|
254 | end
|
---|