rails--rails/actionwebservice/lib/action_web_service/client/soap_client.rb

109 lines
4.5 KiB
Ruby

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
# [<tt>:driver_options</tt>] If you want to supply any custom SOAP RPC driver
# options, you can provide them as a Hash here
#
# The <tt>:driver_options</tt> option can be used to configure the backend SOAP
# RPC driver. An example of configuring the SOAP backend to do
# client-certificate authenticated SSL connections to the server:
#
# opts = {}
# opts['protocol.http.ssl_config.verify_mode'] = 'OpenSSL::SSL::VERIFY_PEER'
# opts['protocol.http.ssl_config.client_cert'] = client_cert_file_path
# opts['protocol.http.ssl_config.client_key'] = client_key_file_path
# opts['protocol.http.ssl_config.ca_file'] = ca_cert_file_path
# client = ActionWebService::Client::Soap.new(api, 'https://some/service', :driver_options => opts)
def initialize(api, endpoint_uri, options={})
super(api, endpoint_uri)
@type_namespace = options[:type_namespace] || 'urn:ActionWebService'
@method_namespace = options[:method_namespace] || 'urn:ActionWebService'
@driver_options = options[:driver_options] || {}
@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)
@driver_options.each do |name, value|
@driver.options[name.to_s] = value.to_s
end
end
protected
def perform_invocation(method_name, args)
method = @api.api_methods[method_name.to_sym]
args = method.cast_expects(@marshaler, args)
return_value = @driver.send(method_name, *args)
method.cast_returns(@marshaler, return_value)
end
def soap_action(method_name)
"#{@soap_action_base}/#{method_name}"
end
private
def create_soap_rpc_driver(api, endpoint_uri)
api.api_methods.each{ |name, method| method.register_types(@marshaler) }
driver = SoapDriver.new(endpoint_uri, nil)
driver.mapping_registry = @marshaler.registry
api.api_methods.each do |name, method|
qname = XSD::QName.new(@method_namespace, method.public_name)
action = soap_action(method.public_name)
expects = method.expects
returns = method.returns
param_def = []
i = 0
if expects
expects.each do |spec|
param_name = method.param_name(spec, i)
type_binding = @marshaler.lookup_type(spec)
param_def << ['in', param_name, type_binding.mapping]
i += 1
end
end
if returns
type_binding = @marshaler.lookup_type(returns[0])
param_def << ['retval', 'return', type_binding.mapping]
end
driver.add_method(qname, action, method.name.to_s, param_def)
end
driver
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