summaryrefslogtreecommitdiff
path: root/lib/soap/rpc/driver.rb
blob: 028addc3eb97c3e89c4761e4e727b65294aa21e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# SOAP4R - SOAP RPC driver
# Copyright (C) 2000, 2001, 2003, 2004  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.

# This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.


require 'soap/soap'
require 'soap/mapping'
require 'soap/mapping/wsdlliteralregistry'
require 'soap/rpc/rpc'
require 'soap/rpc/proxy'
require 'soap/rpc/element'
require 'soap/streamHandler'
require 'soap/property'
require 'soap/header/handlerset'


module SOAP
module RPC


class Driver
  class EmptyResponseError < Error; end

  class << self
    def __attr_proxy(symbol, assignable = false)
      name = symbol.to_s
      module_eval <<-EOD
    	def #{name}
  	  @servant.#{name}
   	end
      EOD
      if assignable
	module_eval <<-EOD
  	  def #{name}=(rhs)
  	    @servant.#{name} = rhs
  	  end
	EOD
      end
    end
  end

  __attr_proxy :options
  __attr_proxy :headerhandler
  __attr_proxy :streamhandler
  __attr_proxy :test_loopback_response
  __attr_proxy :endpoint_url, true
  __attr_proxy :mapping_registry, true
  __attr_proxy :soapaction, true
  __attr_proxy :default_encodingstyle, true
  __attr_proxy :generate_explicit_type, true
  __attr_proxy :allow_unqualified_element, true

  def httpproxy
    options["protocol.http.proxy"]
  end

  def httpproxy=(httpproxy)
    options["protocol.http.proxy"] = httpproxy
  end

  def wiredump_dev
    options["protocol.http.wiredump_dev"]
  end

  def wiredump_dev=(wiredump_dev)
    options["protocol.http.wiredump_dev"] = wiredump_dev
  end

  def mandatorycharset
    options["protocol.mandatorycharset"]
  end

  def mandatorycharset=(mandatorycharset)
    options["protocol.mandatorycharset"] = mandatorycharset
  end

  def wiredump_file_base
    options["protocol.wiredump_file_base"]
  end

  def wiredump_file_base=(wiredump_file_base)
    options["protocol.wiredump_file_base"] = wiredump_file_base
  end

  def initialize(endpoint_url, namespace, soapaction = nil)
    @servant = Servant__.new(self, endpoint_url, namespace)
    @servant.soapaction = soapaction
    @proxy = @servant.proxy
  end

  def loadproperty(propertyname)
    unless options.loadproperty(propertyname)
      raise LoadError.new("No such property to load -- #{propertyname}")
    end
  end

  def inspect
    "#<#{self.class}:#{@servant.inspect}>"
  end

  def add_rpc_method(name, *params)
    param_def = create_rpc_param_def(params)
    @servant.add_rpc_method(name, @servant.soapaction, name, param_def)
  end

  def add_rpc_method_as(name, name_as, *params)
    param_def = create_rpc_param_def(params)
    @servant.add_rpc_method(name_as, @servant.soapaction, name, param_def)
  end

  def add_rpc_method_with_soapaction(name, soapaction, *params)
    param_def = create_rpc_param_def(params)
    @servant.add_rpc_method(name, soapaction, name, param_def)
  end

  def add_rpc_method_with_soapaction_as(name, name_as, soapaction, *params)
    param_def = create_rpc_param_def(params)
    @servant.add_rpc_method(name_as, soapaction, name, param_def)
  end

  # add_method is for shortcut of typical rpc/encoded method definition.
  alias add_method add_rpc_method
  alias add_method_as add_rpc_method_as
  alias add_method_with_soapaction add_rpc_method_with_soapaction
  alias add_method_with_soapaction_as add_rpc_method_with_soapaction_as

  def add_document_method(name, req_qname, res_qname)
    param_def = create_document_param_def(name, req_qname, res_qname)
    @servant.add_document_method(name, @servant.soapaction, name, param_def)
  end

  def add_document_method_as(name, name_as, req_qname, res_qname)
    param_def = create_document_param_def(name, req_qname, res_qname)
    @servant.add_document_method(name_as, @servant.soapaction, name, param_def)
  end

  def add_document_method_with_soapaction(name, soapaction, req_qname,
      res_qname)
    param_def = create_document_param_def(name, req_qname, res_qname)
    @servant.add_document_method(name, soapaction, name, param_def)
  end

  def add_document_method_with_soapaction_as(name, name_as, soapaction,
      req_qname, res_qname)
    param_def = create_document_param_def(name, req_qname, res_qname)
    @servant.add_document_method(name_as, soapaction, name, param_def)
  end

  def reset_stream
    @servant.reset_stream
  end

  def invoke(headers, body)
    @servant.invoke(headers, body)
  end

  def call(name, *params)
    @servant.call(name, *params)
  end

private

  def create_rpc_param_def(params)
    if params.size == 1 and params[0].is_a?(Array)
      params[0]
    else
      SOAPMethod.create_param_def(params)
    end
  end

  def create_document_param_def(name, req_qname, res_qname)
    [
      ['input', name, [nil, req_qname.namespace, req_qname.name]],
      ['output', name, [nil, res_qname.namespace, res_qname.name]]
    ]
  end

  def add_rpc_method_interface(name, param_def)
    @servant.add_rpc_method_interface(name, param_def)
  end

  def add_document_method_interface(name, paramname)
    @servant.add_document_method_interface(name, paramname)
  end

  class Servant__
    attr_reader :proxy
    attr_reader :options
    attr_accessor :soapaction

    def initialize(host, endpoint_url, namespace)
      @host = host
      @namespace = namespace
      @soapaction = nil
      @options = setup_options
      @wiredump_file_base = nil
      @endpoint_url = endpoint_url
      @proxy = Proxy.new(endpoint_url, @soapaction, @options)
    end

    def inspect
      "#<#{self.class}:#{@proxy.inspect}>"
    end

    def endpoint_url
      @proxy.endpoint_url
    end

    def endpoint_url=(endpoint_url)
      @proxy.endpoint_url = endpoint_url
    end

    def mapping_registry
      @proxy.mapping_registry
    end

    def mapping_registry=(mapping_registry)
      @proxy.mapping_registry = mapping_registry
    end

    def default_encodingstyle
      @proxy.default_encodingstyle
    end

    def default_encodingstyle=(encodingstyle)
      @proxy.default_encodingstyle = encodingstyle
    end

    def generate_explicit_type
      @proxy.generate_explicit_type
    end

    def generate_explicit_type=(generate_explicit_type)
      @proxy.generate_explicit_type = generate_explicit_type
    end

    def allow_unqualified_element
      @proxy.allow_unqualified_element
    end

    def allow_unqualified_element=(allow_unqualified_element)
      @proxy.allow_unqualified_element = allow_unqualified_element
    end

    def headerhandler
      @proxy.headerhandler
    end

    def streamhandler
      @proxy.streamhandler
    end

    def test_loopback_response
      @proxy.test_loopback_response
    end

    def reset_stream
      @proxy.reset_stream
    end

    def invoke(headers, body)
      if headers and !headers.is_a?(SOAPHeader)
        headers = create_header(headers)
      end
      set_wiredump_file_base(body.elename.name)
      env = @proxy.invoke(headers, body)
      if env.nil?
	return nil, nil
      else
	return env.header, env.body
      end
    end

    def call(name, *params)
      set_wiredump_file_base(name)
      @proxy.call(name, *params)
    end

    def add_rpc_method(name_as, soapaction, name, param_def)
      qname = XSD::QName.new(@namespace, name_as)
      @proxy.add_rpc_method(qname, soapaction, name, param_def)
      add_rpc_method_interface(name, param_def)
    end

    def add_document_method(name_as, soapaction, name, param_def)
      qname = XSD::QName.new(@namespace, name_as)
      @proxy.add_document_method(qname, soapaction, name, param_def)
      add_document_method_interface(name, param_def)
    end

    def add_rpc_method_interface(name, param_def)
      param_names = []
      i = 0
      @proxy.operation[name].each_param_name(RPC::SOAPMethod::IN,
  	  RPC::SOAPMethod::INOUT) do |param_name|
   	i += 1
    	param_names << "arg#{ i }"
      end
      callparam = (param_names.collect { |pname| ", " + pname }).join
      @host.instance_eval <<-EOS
     	def #{ name }(#{ param_names.join(", ") })
      	  @servant.call(#{ name.dump }#{ callparam })
       	end
      EOS
      @host.method(name)
    end

    def add_document_method_interface(name, paramname)
      @host.instance_eval <<-EOS
     	def #{ name }(param)
      	  @servant.call(#{ name.dump }, param)
       	end
      EOS
      @host.method(name)
    end

  private

    def set_wiredump_file_base(name)
      if @wiredump_file_base
      	@proxy.set_wiredump_file_base(@wiredump_file_base + "_#{ name }")
      end
    end

    def create_header(headers)
      header = SOAPHeader.new()
      headers.each do |content, mustunderstand, encodingstyle|
        header.add(SOAPHeaderItem.new(content, mustunderstand, encodingstyle))
      end
      header
    end

    def setup_options
      if opt = Property.loadproperty(::SOAP::PropertyName)
        opt = opt["client"]
      end
      opt ||= Property.new
      opt.add_hook("protocol.mandatorycharset") do |key, value|
        @proxy.mandatorycharset = value
      end
      opt.add_hook("protocol.wiredump_file_base") do |key, value|
        @wiredump_file_base = value
      end
      opt["protocol.http.charset"] ||= XSD::Charset.encoding_label
      opt["protocol.http.proxy"] ||= Env::HTTP_PROXY
      opt["protocol.http.no_proxy"] ||= Env::NO_PROXY
      opt
    end
  end
end


end
end