2005-02-19 08:29:42 +00:00
|
|
|
require 'benchmark'
|
|
|
|
|
|
|
|
module ActionWebService # :nodoc:
|
|
|
|
module Dispatcher # :nodoc:
|
|
|
|
class DispatcherError < ActionWebService::ActionWebServiceError # :nodoc:
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.append_features(base) # :nodoc:
|
|
|
|
super
|
|
|
|
base.class_inheritable_option(:web_service_dispatching_mode, :direct)
|
|
|
|
base.class_inheritable_option(:web_service_exception_reporting, true)
|
|
|
|
base.send(:include, ActionWebService::Dispatcher::InstanceMethods)
|
|
|
|
end
|
|
|
|
|
2005-03-04 10:07:53 +00:00
|
|
|
def self.layered_service_name(public_method_name) # :nodoc:
|
|
|
|
if public_method_name =~ /^([^\.]+)\.(.*)$/
|
|
|
|
$1
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-19 08:29:42 +00:00
|
|
|
module InstanceMethods # :nodoc:
|
|
|
|
private
|
2005-02-25 23:39:39 +00:00
|
|
|
def invoke_web_service_request(protocol_request)
|
|
|
|
invocation = web_service_invocation(protocol_request)
|
2005-02-19 08:29:42 +00:00
|
|
|
case web_service_dispatching_mode
|
|
|
|
when :direct
|
2005-02-25 23:39:39 +00:00
|
|
|
web_service_direct_invoke(invocation)
|
2005-02-27 21:21:40 +00:00
|
|
|
when :delegated, :layered
|
2005-02-25 23:39:39 +00:00
|
|
|
web_service_delegated_invoke(invocation)
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
|
|
|
end
|
2005-02-25 23:39:39 +00:00
|
|
|
|
|
|
|
def web_service_direct_invoke(invocation)
|
|
|
|
@method_params = invocation.method_ordered_params
|
2005-03-28 03:20:13 +00:00
|
|
|
arity = method(invocation.api_method.name).arity rescue 0
|
2005-03-26 00:20:19 +00:00
|
|
|
if arity < 0 || arity > 0
|
2005-03-28 03:20:13 +00:00
|
|
|
return_value = self.__send__(invocation.api_method.name, *@method_params)
|
2005-03-26 00:20:19 +00:00
|
|
|
else
|
2005-03-28 03:20:13 +00:00
|
|
|
return_value = self.__send__(invocation.api_method.name)
|
2005-03-26 00:20:19 +00:00
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
if invocation.api.has_api_method?(invocation.api_method.name)
|
|
|
|
api_method = invocation.api_method
|
2005-02-27 21:21:40 +00:00
|
|
|
else
|
2005-03-28 03:20:13 +00:00
|
|
|
api_method = invocation.api_method.dup
|
|
|
|
api_method.instance_eval{ @returns = [ return_value.class ] }
|
2005-02-27 21:21:40 +00:00
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
invocation.protocol.marshal_response(api_method, return_value)
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
|
|
|
|
2005-02-25 23:39:39 +00:00
|
|
|
def web_service_delegated_invoke(invocation)
|
2005-02-19 08:29:42 +00:00
|
|
|
cancellation_reason = nil
|
2005-03-28 03:20:13 +00:00
|
|
|
return_value = invocation.service.perform_invocation(invocation.api_method.name, invocation.method_ordered_params) do |x|
|
2005-02-19 08:29:42 +00:00
|
|
|
cancellation_reason = x
|
|
|
|
end
|
|
|
|
if cancellation_reason
|
|
|
|
raise(DispatcherError, "request canceled: #{cancellation_reason}")
|
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
invocation.protocol.marshal_response(invocation.api_method, return_value)
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
|
|
|
|
2005-02-25 23:39:39 +00:00
|
|
|
def web_service_invocation(request)
|
2005-02-27 21:21:40 +00:00
|
|
|
public_method_name = request.method_name
|
2005-02-25 23:39:39 +00:00
|
|
|
invocation = Invocation.new
|
|
|
|
invocation.protocol = request.protocol
|
|
|
|
invocation.service_name = request.service_name
|
2005-02-27 21:21:40 +00:00
|
|
|
if web_service_dispatching_mode == :layered
|
|
|
|
if request.method_name =~ /^([^\.]+)\.(.*)$/
|
|
|
|
public_method_name = $2
|
|
|
|
invocation.service_name = $1
|
|
|
|
end
|
|
|
|
end
|
2005-02-19 08:29:42 +00:00
|
|
|
case web_service_dispatching_mode
|
|
|
|
when :direct
|
2005-02-25 23:39:39 +00:00
|
|
|
invocation.api = self.class.web_service_api
|
|
|
|
invocation.service = self
|
2005-02-27 21:21:40 +00:00
|
|
|
when :delegated, :layered
|
|
|
|
invocation.service = web_service_object(invocation.service_name) rescue nil
|
2005-02-25 23:39:39 +00:00
|
|
|
unless invocation.service
|
2005-02-27 21:21:40 +00:00
|
|
|
raise(DispatcherError, "service #{invocation.service_name} not available")
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
2005-02-25 23:39:39 +00:00
|
|
|
invocation.api = invocation.service.class.web_service_api
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
request.api = invocation.api
|
2005-02-27 21:21:40 +00:00
|
|
|
if invocation.api.has_public_api_method?(public_method_name)
|
2005-03-28 03:20:13 +00:00
|
|
|
invocation.api_method = invocation.api.public_api_method_instance(public_method_name)
|
2005-02-27 21:21:40 +00:00
|
|
|
else
|
|
|
|
if invocation.api.default_api_method.nil?
|
|
|
|
raise(DispatcherError, "no such method '#{public_method_name}' on API #{invocation.api}")
|
|
|
|
else
|
2005-03-28 03:20:13 +00:00
|
|
|
invocation.api_method = invocation.api.default_api_method_instance
|
2005-02-27 21:21:40 +00:00
|
|
|
end
|
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
unless invocation.service.respond_to?(invocation.api_method.name)
|
|
|
|
raise(DispatcherError, "no such method '#{public_method_name}' on API #{invocation.api} (#{invocation.api_method.name})")
|
2005-02-25 23:39:39 +00:00
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
request.api_method = invocation.api_method
|
|
|
|
begin
|
|
|
|
invocation.method_ordered_params = invocation.api_method.cast_expects_ws2ruby(request.protocol.marshaler, request.method_params)
|
|
|
|
rescue
|
|
|
|
invocation.method_ordered_params = request.method_params.map{ |x| x.value }
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
2005-03-28 03:20:13 +00:00
|
|
|
invocation.method_named_params = {}
|
|
|
|
invocation.api_method.param_names.inject(0) do |m, n|
|
|
|
|
invocation.method_named_params[n] = invocation.method_ordered_params[m]
|
|
|
|
m + 1
|
2005-02-27 21:21:40 +00:00
|
|
|
end
|
2005-02-25 23:39:39 +00:00
|
|
|
invocation
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
|
|
|
|
2005-02-27 21:21:40 +00:00
|
|
|
class Invocation # :nodoc:
|
2005-02-25 23:39:39 +00:00
|
|
|
attr_accessor :protocol
|
|
|
|
attr_accessor :service_name
|
|
|
|
attr_accessor :api
|
2005-03-28 03:20:13 +00:00
|
|
|
attr_accessor :api_method
|
2005-02-25 23:39:39 +00:00
|
|
|
attr_accessor :method_ordered_params
|
|
|
|
attr_accessor :method_named_params
|
|
|
|
attr_accessor :service
|
2005-02-19 08:29:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|