summaryrefslogtreecommitdiff
path: root/lib/soap/rpc/driver.rb
blob: 739c8774d41eb8cea199848b05b8cf8bdf019a95 (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
# SOAP4R - SOAP RPC driver
# Copyright (C) 2000, 2001, 2003  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/rpc/rpc'
require 'soap/rpc/proxy'
require 'soap/rpc/element'
require 'soap/streamHandler'


module SOAP
module RPC


class Driver
public
  class EmptyResponseError < Error; end

  attr_accessor :mapping_registry
  attr_accessor :soapaction
  attr_reader :wiredump_dev
  attr_reader :wiredump_file_base
  attr_reader :streamhandler

  def initialize(endpoint_url, namespace, soapaction = nil)
    @namespace = namespace
    @mapping_registry = nil      # for unmarshal
    @soapaction = soapaction
    @wiredump_dev = nil
    @wiredump_file_base = nil
    name = 'http_proxy'
    @httpproxy = ENV[name] || ENV[name.upcase]
    @streamhandler = HTTPPostStreamHandler.new(endpoint_url, @httpproxy,
      XSD::Charset.encoding_label)
    @proxy = Proxy.new(@streamhandler, @soapaction)
    @proxy.allow_unqualified_element = true
  end

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

  def endpoint_url
    @streamhandler.endpoint_url
  end

  def endpoint_url=(endpoint_url)
    @streamhandler.endpoint_url = endpoint_url
    @streamhandler.reset
  end

  def wiredump_dev=(dev)
    @wiredump_dev = dev
    @streamhandler.wiredump_dev = @wiredump_dev
    @streamhandler.reset
  end

  def wiredump_file_base=(base)
    @wiredump_file_base = base
  end

  def httpproxy
    @httpproxy
  end

  def httpproxy=(httpproxy)
    @httpproxy = httpproxy
    @streamhandler.proxy = @httpproxy
    @streamhandler.reset
  end

  def mandatorycharset
    @proxy.mandatorycharset
  end

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

  def default_encodingstyle
    @proxy.default_encodingstyle
  end

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


  ###
  ## Method definition interfaces.
  #
  # params: [[param_def...]] or [paramname, paramname, ...]
  # param_def: See proxy.rb.  Sorry.

  def add_method(name, *params)
    add_method_with_soapaction_as(name, name, @soapaction, *params)
  end

  def add_method_as(name, name_as, *params)
    add_method_with_soapaction_as(name, name_as, @soapaction, *params)
  end

  def add_method_with_soapaction(name, soapaction, *params)
    add_method_with_soapaction_as(name, name, soapaction, *params)
  end

  def add_method_with_soapaction_as(name, name_as, soapaction, *params)
    param_def = if params.size == 1 and params[0].is_a?(Array)
        params[0]
      else
        SOAPMethod.create_param_def(params)
      end
    qname = XSD::QName.new(@namespace, name_as)
    @proxy.add_method(qname, soapaction, name, param_def)
    add_rpc_method_interface(name, param_def)
  end


  ###
  ## Driving interface.
  #
  def invoke(headers, body)
    if @wiredump_file_base
      @streamhandler.wiredump_file_base =
	@wiredump_file_base + '_' << body.elename.name
    end
    @proxy.invoke(headers, body)
  end

  def call(name, *params)
    # Convert parameters: params array => SOAPArray => members array
    params = Mapping.obj2soap(params, @mapping_registry).to_a
    if @wiredump_file_base
      @streamhandler.wiredump_file_base = @wiredump_file_base + '_' << name
    end

    # Then, call @proxy.call like the following.
    header, body = @proxy.call(nil, name, *params)
    unless body
      raise EmptyResponseError.new("Empty response.")
    end

    begin
      @proxy.check_fault(body)
    rescue SOAP::FaultError => e
      Mapping.fault2exception(e)
    end

    ret = body.response ? Mapping.soap2obj(body.response, @mapping_registry) : nil
    if body.outparams
      outparams = body.outparams.collect { |outparam| Mapping.soap2obj(outparam) }
      return [ret].concat(outparams)
    else
      return ret
    end
  end

  def reset_stream
    @streamhandler.reset
  end

private

  def add_rpc_method_interface(name, param_def)
    param_names = []
    i = 0
    @proxy.method[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
    self.instance_eval <<-EOS
      def #{ name }(#{ param_names.join(", ") })
        call("#{ name }"#{ callparam })
      end
    EOS
  end
end


end
end