removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionwebservice / lib / action_web_service / client / soap_client.rb
blobebabd8ea8231140b8a375a88aa29ab1cc3b5e1bf
1 require 'soap/rpc/driver'
2 require 'uri'
4 module ActionWebService # :nodoc:
5   module Client # :nodoc:
7     # Implements SOAP client support (using RPC encoding for the messages).
8     #
9     # ==== Example Usage
10     #
11     #   class PersonAPI < ActionWebService::API::Base
12     #     api_method :find_all, :returns => [[Person]]
13     #   end
14     #
15     #   soap_client = ActionWebService::Client::Soap.new(PersonAPI, "http://...")
16     #   persons = soap_client.find_all
17     #
18     class Soap < Base
19       # provides access to the underlying soap driver
20       attr_reader :driver
22       # Creates a new web service client using the SOAP RPC protocol.
23       #
24       # +api+ must be an ActionWebService::API::Base derivative, and
25       # +endpoint_uri+ must point at the relevant URL to which protocol requests
26       # will be sent with HTTP POST.
27       #
28       # Valid options:
29       # [<tt>:namespace</tt>]    If the remote server has used a custom namespace to
30       #                          declare its custom types, you can specify it here. This would
31       #                          be the namespace declared with a [WebService(Namespace = "http://namespace")] attribute
32       #                          in .NET, for example.
33       # [<tt>:driver_options</tt>]    If you want to supply any custom SOAP RPC driver
34       #                               options, you can provide them as a Hash here
35       #
36       # The <tt>:driver_options</tt> option can be used to configure the backend SOAP
37       # RPC driver. An example of configuring the SOAP backend to do
38       # client-certificate authenticated SSL connections to the server:
39       #
40       #   opts = {}
41       #   opts['protocol.http.ssl_config.verify_mode'] = 'OpenSSL::SSL::VERIFY_PEER'
42       #   opts['protocol.http.ssl_config.client_cert'] = client_cert_file_path
43       #   opts['protocol.http.ssl_config.client_key'] = client_key_file_path
44       #   opts['protocol.http.ssl_config.ca_file'] = ca_cert_file_path
45       #   client = ActionWebService::Client::Soap.new(api, 'https://some/service', :driver_options => opts)
46       def initialize(api, endpoint_uri, options={})
47         super(api, endpoint_uri)
48         @namespace = options[:namespace] || 'urn:ActionWebService'
49         @driver_options = options[:driver_options] || {}
50         @protocol = ActionWebService::Protocol::Soap::SoapProtocol.new @namespace
51         @soap_action_base = options[:soap_action_base]
52         @soap_action_base ||= URI.parse(endpoint_uri).path
53         @driver = create_soap_rpc_driver(api, endpoint_uri)
54         @driver_options.each do |name, value|
55           @driver.options[name.to_s] = value
56         end
57       end
59       protected
60         def perform_invocation(method_name, args)
61           method = @api.api_methods[method_name.to_sym]
62           args = method.cast_expects(args.dup) rescue args
63           return_value = @driver.send(method_name, *args)
64           method.cast_returns(return_value.dup) rescue return_value
65         end
67         def soap_action(method_name)
68           "#{@soap_action_base}/#{method_name}"
69         end
71       private
72         def create_soap_rpc_driver(api, endpoint_uri)
73           @protocol.register_api(api)
74           driver = SoapDriver.new(endpoint_uri, nil)
75           driver.mapping_registry = @protocol.marshaler.registry
76           api.api_methods.each do |name, method|
77             qname = XSD::QName.new(@namespace, method.public_name)
78             action = soap_action(method.public_name)
79             expects = method.expects
80             returns = method.returns
81             param_def = []
82             if expects
83               expects.each do |type|
84                 type_binding = @protocol.marshaler.lookup_type(type)
85                 if SOAP::Version >= "1.5.5"
86                   param_def << ['in', type.name.to_s, [type_binding.type.type_class.to_s]]
87                 else
88                   param_def << ['in', type.name, type_binding.mapping]
89                 end
90               end
91             end
92             if returns
93               type_binding = @protocol.marshaler.lookup_type(returns[0])
94               if SOAP::Version >= "1.5.5"
95                 param_def << ['retval', 'return', [type_binding.type.type_class.to_s]]
96               else
97                 param_def << ['retval', 'return', type_binding.mapping]
98               end
99             end
100             driver.add_method(qname, action, method.name.to_s, param_def)
101           end
102           driver
103         end
105         class SoapDriver < SOAP::RPC::Driver # :nodoc:
106           def add_method(qname, soapaction, name, param_def)
107             @proxy.add_rpc_method(qname, soapaction, name, param_def)
108             add_rpc_method_interface(name, param_def)
109           end
110         end
111     end
112   end