aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/lib/action_web_service/client/soap_client.rb
blob: d3975d89a2bcefa50fe9e40024f37d384f25ff35 (plain) (blame)
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
require 'soap/rpc/driver'
require 'uri'

module ActionWebService # :nodoc:
  module Client # :nodoc:

    # Implements SOAP client support (using RPC encoding for the messages).
    #
    # ==== Example Usage
    #
    #   class PersonAPI < ActionWebService::API::Base
    #     api_method :find_all, :returns => [[Person]]
    #   end
    #
    #   soap_client = ActionWebService::Client::Soap.new(PersonAPI, "http://...")
    #   persons = soap_client.find_all
    #
    class Soap < Base

      # Creates a new web service client using the SOAP RPC protocol.
      #
      # +api+ must be an ActionWebService::API::Base derivative, and
      # +endpoint_uri+ must point at the relevant URL to which protocol requests
      # will be sent with HTTP POST.
      #
      # Valid options:
      # [<tt>:type_namespace</tt>]    If the remote server has used a custom namespace to
      #                               declare its custom types, you can specify it here
      # [<tt>:method_namespace</tt>]  If the remote server has used a custom namespace to
      #                               declare its methods, you can specify it here
      def initialize(api, endpoint_uri, options={})
        super(api, endpoint_uri)
        @type_namespace = options[:type_namespace] || 'urn:ActionWebService'
        @method_namespace = options[:method_namespace] || 'urn:ActionWebService'
        @marshaler = WS::Marshaling::SoapMarshaler.new @type_namespace
        @encoder = WS::Encoding::SoapRpcEncoding.new @method_namespace
        @soap_action_base = options[:soap_action_base]
        @soap_action_base ||= URI.parse(endpoint_uri).path
        @driver = create_soap_rpc_driver(api, endpoint_uri)
      end

      protected
        def perform_invocation(method_name, args)
          @driver.send(method_name, *args)
        end

        def soap_action(method_name)
          "#{@soap_action_base}/#{method_name}"
        end

      private
        def create_soap_rpc_driver(api, endpoint_uri)
          register_api(@marshaler, api)
          driver = SoapDriver.new(endpoint_uri, nil)
          driver.mapping_registry = @marshaler.registry
          api.api_methods.each do |name, info|
            public_name = api.public_api_method_name(name)
            qname = XSD::QName.new(@method_namespace, public_name)
            action = soap_action(public_name)
            expects = info[:expects]
            returns = info[:returns]
            param_def = []
            i = 0
            if expects
              expects.each do |spec|
                param_name = spec.is_a?(Hash) ? spec.keys[0].to_s : "param#{i}"
                type_binding = @marshaler.register_type(spec)
                param_def << ['in', param_name, type_binding.mapping]
                i += 1
              end
            end
            if returns
              type_binding = @marshaler.register_type(returns[0])
              param_def << ['retval', 'return', type_binding.mapping]
            end
            driver.add_method(qname, action, name.to_s, param_def)
          end
          driver
        end

        def register_api(marshaler, api)
          type_bindings = []
          api.api_methods.each do |name, info|
            expects, returns = info[:expects], info[:returns]
            if expects
              expects.each{|type| type_bindings << marshaler.register_type(type)}
            end
            if returns
              returns.each{|type| type_bindings << marshaler.register_type(type)}
            end
          end
          type_bindings
        end

        class SoapDriver < SOAP::RPC::Driver # :nodoc:
          def add_method(qname, soapaction, name, param_def)
            @proxy.add_rpc_method(qname, soapaction, name, param_def)
            add_rpc_method_interface(name, param_def)
          end
        end
    end
  end
end