summaryrefslogtreecommitdiff
path: root/lib/soap/rpc/driver.rb
blob: 76fd14e34b0235a3640b97d967345f0072008ce7 (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
=begin
SOAP4R - SOAP RPC driver
Copyright (C) 2000, 2001, 2003  NAKAMURA, Hiroshi.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PRATICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
=end


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 :endpoint_url
  attr_reader :wiredump_dev
  attr_reader :wiredump_file_base
  attr_reader :httpproxy

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

  def endpoint_url=(endpoint_url)
    @endpoint_url = endpoint_url
    if @handler
      @handler.endpoint_url = @endpoint_url
      @handler.reset
    end
  end

  def wiredump_dev=(dev)
    @wiredump_dev = dev
    if @handler
      @handler.wiredump_dev = @wiredump_dev
      @handler.reset
    end
  end

  def wiredump_file_base=(base)
    @wiredump_file_base = base
  end

  def httpproxy=(httpproxy)
    @httpproxy = httpproxy
    if @handler
      @handler.proxy = @httpproxy
      @handler.reset
    end
  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
      @handler.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
      @handler.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
    @handler.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