summaryrefslogtreecommitdiff
path: root/lib/soap/rpc/proxy.rb
blob: 39a095838cf0438872d07cc436e39f18b4cb682d (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
=begin
SOAP4R - RPC Proxy library.
Copyright (C) 2000, 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/processor'
require 'soap/mapping'
require 'soap/rpc/rpc'
require 'soap/rpc/element'
require 'soap/streamHandler'


module SOAP
module RPC


class Proxy
  include SOAP

public

  attr_accessor :soapaction
  attr_accessor :allow_unqualified_element, :default_encodingstyle
  attr_reader :method

  def initialize(stream_handler, soapaction = nil)
    @handler = stream_handler
    @soapaction = soapaction
    @method = {}
    @allow_unqualified_element = false
    @default_encodingstyle = nil
  end

  class Request
    include RPC

  public

    attr_reader :method
    attr_reader :namespace
    attr_reader :name

    def initialize(model, values)
      @method = model.dup
      @namespace = @method.elename.namespace
      @name = @method.elename.name

      params = {}
    
      if ((values.size == 1) and (values[0].is_a?(Hash)))
        params = values[0]
      else
        i = 0
        @method.each_param_name(SOAPMethod::IN, SOAPMethod::INOUT) do |name|
          params[name] = values[i] || SOAPNil.new
          i += 1
        end
      end
      @method.set_param(params)
    end
  end

  def add_method(qname, soapaction, name, param_def)
    @method[name] = SOAPMethodRequest.new(qname, param_def, soapaction)
  end

  def create_request(name, *values)
    if (@method.key?(name))
      method = @method[name]
      method.encodingstyle = @default_encodingstyle if @default_encodingstyle
    else
      raise SOAP::RPC::MethodDefinitionError.new(
	"Method: #{ name } not defined.")
    end

    Request.new(method, values)
  end

  def invoke(req_header, req_body, soapaction = nil)
    if req_header and !req_header.is_a?(SOAPHeader)
      req_header = create_header(req_header)
    end
    if !req_body.is_a?(SOAPBody)
      req_body = SOAPBody.new(req_body)
    end
    opt = create_options
    send_string = Processor.marshal(req_header, req_body, opt)
    data = @handler.send(send_string, soapaction)
    if data.receive_string.empty?
      return nil, nil
    end
    res_charset = StreamHandler.parse_media_type(data.receive_contenttype)
    opt = create_options
    opt[:charset] = res_charset
    res_header, res_body = Processor.unmarshal(data.receive_string, opt)
    return res_header, res_body
  end

  def call(headers, name, *values)
    req = create_request(name, *values)
    return invoke(headers, req.method, req.method.soapaction || @soapaction)
  end

  def check_fault(body)
    if body.fault
      raise SOAP::FaultError.new(body.fault)
    end
  end

private

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

  def create_options
    opt = {}
    opt[:default_encodingstyle] = @default_encodingstyle
    if @allow_unqualified_element
      opt[:allow_unqualified_element] = true
    end
    opt
  end
end


end
end