2005-03-29 07:31:39 -05:00
|
|
|
require 'ostruct'
|
|
|
|
require 'uri'
|
2005-03-31 05:47:01 -05:00
|
|
|
require 'benchmark'
|
2005-04-02 16:03:36 -05:00
|
|
|
require 'pathname'
|
2005-03-29 07:31:39 -05:00
|
|
|
|
|
|
|
module ActionWebService
|
|
|
|
module Scaffolding # :nodoc:
|
2005-04-02 16:03:36 -05:00
|
|
|
class ScaffoldingError < ActionWebServiceError # :nodoc:
|
|
|
|
end
|
|
|
|
|
2005-03-29 07:31:39 -05:00
|
|
|
def self.append_features(base)
|
|
|
|
super
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Web service invocation scaffolding provides a way to quickly invoke web service methods in a controller. The
|
|
|
|
# generated scaffold actions have default views to let you enter the method parameters and view the
|
|
|
|
# results.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class ApiController < ActionController
|
|
|
|
# web_service_scaffold :invoke
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This example generates an +invoke+ action in the +ApiController+ that you can navigate to from
|
|
|
|
# your browser, select the API method, enter its parameters, and perform the invocation.
|
|
|
|
#
|
|
|
|
# If you want to customize the default views, create the following views in "app/views":
|
|
|
|
#
|
|
|
|
# * <tt>action_name/methods.rhtml</tt>
|
|
|
|
# * <tt>action_name/parameters.rhtml</tt>
|
|
|
|
# * <tt>action_name/result.rhtml</tt>
|
|
|
|
# * <tt>action_name/layout.rhtml</tt>
|
|
|
|
#
|
|
|
|
# Where <tt>action_name</tt> is the name of the action you gave to ClassMethods#web_service_scaffold.
|
|
|
|
#
|
|
|
|
# You can use the default views in <tt>RAILS_DIR/lib/action_web_service/templates/scaffolds</tt> as
|
|
|
|
# a guide.
|
|
|
|
module ClassMethods
|
|
|
|
# Generates web service invocation scaffolding for the current controller. The given action name
|
|
|
|
# can then be used as the entry point for invoking API methods from a web browser.
|
|
|
|
def web_service_scaffold(action_name)
|
|
|
|
add_template_helper(Helpers)
|
|
|
|
module_eval <<-END, __FILE__, __LINE__
|
|
|
|
def #{action_name}
|
|
|
|
if @request.method == :get
|
2005-04-03 00:23:30 -05:00
|
|
|
setup_invocation_assigns
|
|
|
|
render_invocation_scaffold 'methods'
|
2005-03-29 07:31:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{action_name}_method_params
|
|
|
|
if @request.method == :get
|
2005-04-03 00:23:30 -05:00
|
|
|
setup_invocation_assigns
|
|
|
|
render_invocation_scaffold 'parameters'
|
2005-03-29 07:31:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{action_name}_submit
|
|
|
|
if @request.method == :post
|
2005-04-03 00:23:30 -05:00
|
|
|
setup_invocation_assigns
|
2005-03-29 07:31:39 -05:00
|
|
|
protocol_name = @params['protocol'] ? @params['protocol'].to_sym : :soap
|
|
|
|
case protocol_name
|
|
|
|
when :soap
|
2005-04-05 18:29:01 -04:00
|
|
|
@protocol = Protocol::Soap::SoapProtocol.new
|
2005-03-29 07:31:39 -05:00
|
|
|
when :xmlrpc
|
2005-04-05 18:29:01 -04:00
|
|
|
@protocol = Protocol::XmlRpc::XmlRpcProtocol.new
|
2005-03-29 07:31:39 -05:00
|
|
|
end
|
2005-04-03 00:23:30 -05:00
|
|
|
@invocation_cgi = @request.respond_to?(:cgi) ? @request.cgi : nil
|
2005-03-31 05:47:01 -05:00
|
|
|
bm = Benchmark.measure do
|
2005-04-05 18:29:01 -04:00
|
|
|
@protocol.register_api(@scaffold_service.api)
|
2005-04-02 16:03:36 -05:00
|
|
|
params = @params['method_params'] ? @params['method_params'].dup : nil
|
|
|
|
params = @scaffold_method.cast_expects(params)
|
2005-04-05 18:29:01 -04:00
|
|
|
method_name = public_method_name(@scaffold_service.name, @scaffold_method.public_name)
|
|
|
|
@method_request_xml = @protocol.encode_request(method_name, params, @scaffold_method.expects)
|
|
|
|
new_request = @protocol.encode_action_pack_request(@scaffold_service.name, @scaffold_method.public_name, @method_request_xml)
|
|
|
|
prepare_request(new_request, @scaffold_service.name, @scaffold_method.public_name)
|
2005-04-03 00:23:30 -05:00
|
|
|
@request = new_request
|
2005-03-31 05:47:01 -05:00
|
|
|
dispatch_web_service_request
|
|
|
|
@method_response_xml = @response.body
|
2005-04-05 18:29:01 -04:00
|
|
|
method_name, obj = @protocol.decode_response(@method_response_xml)
|
2005-04-03 00:23:30 -05:00
|
|
|
return if handle_invocation_exception(obj)
|
2005-04-02 16:03:36 -05:00
|
|
|
@method_return_value = @scaffold_method.cast_returns(obj)
|
2005-03-31 05:47:01 -05:00
|
|
|
end
|
|
|
|
@method_elapsed = bm.real
|
2005-03-29 07:31:39 -05:00
|
|
|
add_instance_variables_to_assigns
|
2005-04-03 00:23:30 -05:00
|
|
|
reset_invocation_response
|
|
|
|
render_invocation_scaffold 'result'
|
2005-03-29 07:31:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2005-04-03 00:23:30 -05:00
|
|
|
def setup_invocation_assigns
|
2005-03-29 07:31:39 -05:00
|
|
|
@scaffold_class = self.class
|
|
|
|
@scaffold_action_name = "#{action_name}"
|
|
|
|
@scaffold_container = WebServiceModel::Container.new(self)
|
|
|
|
if @params['service'] && @params['method']
|
|
|
|
@scaffold_service = @scaffold_container.services.find{ |x| x.name == @params['service'] }
|
|
|
|
@scaffold_method = @scaffold_service.api_methods[@params['method']]
|
|
|
|
end
|
|
|
|
add_instance_variables_to_assigns
|
|
|
|
end
|
|
|
|
|
2005-04-03 00:23:30 -05:00
|
|
|
def render_invocation_scaffold(action)
|
2005-03-29 07:31:39 -05:00
|
|
|
customized_template = "\#{self.class.controller_path}/#{action_name}/\#{action}"
|
|
|
|
default_template = scaffold_path(action)
|
|
|
|
@content_for_layout = template_exists?(customized_template) ? @template.render_file(customized_template) : @template.render_file(default_template, false)
|
|
|
|
self.active_layout ? render_file(self.active_layout, "200 OK", true) : render_file(scaffold_path("layout"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def scaffold_path(template_name)
|
2005-04-02 16:03:36 -05:00
|
|
|
Pathname.new(File.dirname(__FILE__) + "/templates/scaffolds/" + template_name + ".rhtml").realpath.to_s
|
2005-03-29 07:31:39 -05:00
|
|
|
end
|
2005-04-03 00:23:30 -05:00
|
|
|
|
|
|
|
def reset_invocation_response
|
|
|
|
template = @response.template
|
|
|
|
if @invocation_cgi
|
|
|
|
@response = ::ActionController::CgiResponse.new(@invocation_cgi)
|
|
|
|
else
|
|
|
|
@response = ::ActionController::TestResponse.new
|
|
|
|
end
|
|
|
|
@response.template = template
|
|
|
|
@performed_render = false
|
|
|
|
end
|
2005-04-05 18:29:01 -04:00
|
|
|
|
|
|
|
def public_method_name(service_name, method_name)
|
|
|
|
if web_service_dispatching_mode == :layered && @protocol.is_a?(ActionWebService::Protocol::XmlRpc::XmlRpcProtocol)
|
|
|
|
service_name + '.' + method_name
|
|
|
|
else
|
|
|
|
method_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_request(request, service_name, method_name)
|
|
|
|
request.parameters.update(@request.parameters)
|
|
|
|
if web_service_dispatching_mode == :layered && @protocol.is_a?(ActionWebService::Protocol::Soap::SoapProtocol)
|
|
|
|
request.env['HTTP_SOAPACTION'] = "/\#{controller_name()}/\#{service_name}/\#{method_name}"
|
|
|
|
end
|
|
|
|
end
|
2005-04-03 00:23:30 -05:00
|
|
|
|
|
|
|
def handle_invocation_exception(obj)
|
|
|
|
exception = nil
|
|
|
|
if obj.respond_to?(:detail) && obj.detail.respond_to?(:cause) && obj.detail.cause.is_a?(Exception)
|
|
|
|
exception = obj.detail.cause
|
|
|
|
elsif obj.is_a?(XMLRPC::FaultException)
|
|
|
|
exception = obj
|
|
|
|
end
|
|
|
|
return unless exception
|
|
|
|
reset_invocation_response
|
|
|
|
rescue_action(exception)
|
|
|
|
true
|
|
|
|
end
|
2005-03-29 07:31:39 -05:00
|
|
|
END
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Helpers # :nodoc:
|
2005-04-02 16:03:36 -05:00
|
|
|
def method_parameter_input_fields(method, type)
|
|
|
|
name = type.name.to_s
|
|
|
|
type_name = type.type
|
|
|
|
unless type_name.is_a?(Symbol)
|
2005-03-29 07:31:39 -05:00
|
|
|
raise "Parameter #{name}: Structured/array types not supported in scaffolding input fields yet"
|
|
|
|
end
|
|
|
|
field_name = "method_params[]"
|
|
|
|
case type_name
|
|
|
|
when :int
|
|
|
|
text_field_tag field_name
|
|
|
|
when :string
|
|
|
|
text_field_tag field_name
|
|
|
|
when :bool
|
|
|
|
radio_button_tag field_name, "True"
|
|
|
|
radio_button_tag field_name, "False"
|
|
|
|
when :float
|
|
|
|
text_field_tag field_name
|
|
|
|
when :time
|
|
|
|
select_datetime Time.now, 'name' => field_name
|
|
|
|
when :date
|
|
|
|
select_date Date.today, 'name' => field_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_method_list(service)
|
|
|
|
action = @scaffold_action_name + '_method_params'
|
|
|
|
methods = service.api_methods_full.map do |desc, name|
|
|
|
|
content_tag("li", link_to(desc, :action => action, :service => service.name, :method => name))
|
|
|
|
end
|
|
|
|
content_tag("ul", methods.join("\n"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module WebServiceModel # :nodoc:
|
|
|
|
class Container # :nodoc:
|
|
|
|
attr :services
|
|
|
|
|
|
|
|
def initialize(real_container)
|
|
|
|
@real_container = real_container
|
|
|
|
@services = []
|
|
|
|
if @real_container.class.web_service_dispatching_mode == :direct
|
|
|
|
@services << Service.new(@real_container.controller_name, @real_container)
|
|
|
|
else
|
2005-04-02 19:12:11 -05:00
|
|
|
@real_container.class.web_services.each do |name, obj|
|
2005-03-29 07:31:39 -05:00
|
|
|
@services << Service.new(name, @real_container.instance_eval{ web_service_object(name) })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Service # :nodoc:
|
|
|
|
attr :name
|
|
|
|
attr :object
|
|
|
|
attr :api
|
|
|
|
attr :api_methods
|
|
|
|
attr :api_methods_full
|
|
|
|
|
|
|
|
def initialize(name, real_service)
|
|
|
|
@name = name.to_s
|
|
|
|
@object = real_service
|
|
|
|
@api = @object.class.web_service_api
|
2005-04-02 16:03:36 -05:00
|
|
|
if @api.nil?
|
|
|
|
raise ScaffoldingError, "No web service API attached to #{object.class}"
|
|
|
|
end
|
2005-03-29 07:31:39 -05:00
|
|
|
@api_methods = {}
|
|
|
|
@api_methods_full = []
|
|
|
|
@api.api_methods.each do |name, method|
|
|
|
|
@api_methods[method.public_name.to_s] = method
|
|
|
|
@api_methods_full << [method.to_s, method.public_name.to_s]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
self.name.camelize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|