Checkout of Instiki Trunk 1/21/2007.

This commit is contained in:
Jacques Distler 2007-01-22 07:43:50 -06:00
commit 69b62b6f33
1138 changed files with 139586 additions and 0 deletions

View file

@ -0,0 +1,112 @@
module ActionWebService # :nodoc:
module Protocol # :nodoc:
class ProtocolError < ActionWebServiceError # :nodoc:
end
class AbstractProtocol # :nodoc:
def setup(controller)
end
def decode_action_pack_request(action_pack_request)
end
def encode_action_pack_request(service_name, public_method_name, raw_body, options={})
klass = options[:request_class] || SimpleActionPackRequest
request = klass.new
request.request_parameters['action'] = service_name.to_s
request.env['RAW_POST_DATA'] = raw_body
request.env['REQUEST_METHOD'] = 'POST'
request.env['HTTP_CONTENT_TYPE'] = 'text/xml'
request
end
def decode_request(raw_request, service_name, protocol_options={})
end
def encode_request(method_name, params, param_types)
end
def decode_response(raw_response)
end
def encode_response(method_name, return_value, return_type, protocol_options={})
end
def protocol_client(api, protocol_name, endpoint_uri, options)
end
def register_api(api)
end
end
class Request # :nodoc:
attr :protocol
attr_accessor :method_name
attr_accessor :method_params
attr :service_name
attr_accessor :api
attr_accessor :api_method
attr :protocol_options
def initialize(protocol, method_name, method_params, service_name, api=nil, api_method=nil, protocol_options=nil)
@protocol = protocol
@method_name = method_name
@method_params = method_params
@service_name = service_name
@api = api
@api_method = api_method
@protocol_options = protocol_options || {}
end
end
class Response # :nodoc:
attr :body
attr :content_type
attr :return_value
def initialize(body, content_type, return_value)
@body = body
@content_type = content_type
@return_value = return_value
end
end
class SimpleActionPackRequest < ActionController::AbstractRequest # :nodoc:
def initialize
@env = {}
@qparams = {}
@rparams = {}
@cookies = {}
reset_session
end
def query_parameters
@qparams
end
def request_parameters
@rparams
end
def env
@env
end
def host
''
end
def cookies
@cookies
end
def session
@session
end
def reset_session
@session = {}
end
end
end
end

View file

@ -0,0 +1,37 @@
module ActionWebService # :nodoc:
module Protocol # :nodoc:
module Discovery # :nodoc:
def self.included(base)
base.extend(ClassMethods)
base.send(:include, ActionWebService::Protocol::Discovery::InstanceMethods)
end
module ClassMethods # :nodoc:
def register_protocol(klass)
write_inheritable_array("web_service_protocols", [klass])
end
end
module InstanceMethods # :nodoc:
private
def discover_web_service_request(action_pack_request)
(self.class.read_inheritable_attribute("web_service_protocols") || []).each do |protocol|
protocol = protocol.create(self)
request = protocol.decode_action_pack_request(action_pack_request)
return request unless request.nil?
end
nil
end
def create_web_service_client(api, protocol_name, endpoint_uri, options)
(self.class.read_inheritable_attribute("web_service_protocols") || []).each do |protocol|
protocol = protocol.create(self)
client = protocol.protocol_client(api, protocol_name, endpoint_uri, options)
return client unless client.nil?
end
nil
end
end
end
end
end

View file

@ -0,0 +1,176 @@
require 'action_web_service/protocol/soap_protocol/marshaler'
require 'soap/streamHandler'
require 'action_web_service/client/soap_client'
module ActionWebService # :nodoc:
module API # :nodoc:
class Base # :nodoc:
def self.soap_client(endpoint_uri, options={})
ActionWebService::Client::Soap.new self, endpoint_uri, options
end
end
end
module Protocol # :nodoc:
module Soap # :nodoc:
def self.included(base)
base.register_protocol(SoapProtocol)
base.class_inheritable_option(:wsdl_service_name)
base.class_inheritable_option(:wsdl_namespace)
end
class SoapProtocol < AbstractProtocol # :nodoc:
AWSEncoding = 'UTF-8'
XSDEncoding = 'UTF8'
attr :marshaler
def initialize(namespace=nil)
namespace ||= 'urn:ActionWebService'
@marshaler = SoapMarshaler.new namespace
end
def self.create(controller)
SoapProtocol.new(controller.wsdl_namespace)
end
def decode_action_pack_request(action_pack_request)
return nil unless soap_action = has_valid_soap_action?(action_pack_request)
service_name = action_pack_request.parameters['action']
input_encoding = parse_charset(action_pack_request.env['HTTP_CONTENT_TYPE'])
protocol_options = {
:soap_action => soap_action,
:charset => input_encoding
}
decode_request(action_pack_request.raw_post, service_name, protocol_options)
end
def encode_action_pack_request(service_name, public_method_name, raw_body, options={})
request = super
request.env['HTTP_SOAPACTION'] = '/soap/%s/%s' % [service_name, public_method_name]
request
end
def decode_request(raw_request, service_name, protocol_options={})
envelope = SOAP::Processor.unmarshal(raw_request, :charset => protocol_options[:charset])
unless envelope
raise ProtocolError, "Failed to parse SOAP request message"
end
request = envelope.body.request
method_name = request.elename.name
params = request.collect{ |k, v| marshaler.soap_to_ruby(request[k]) }
Request.new(self, method_name, params, service_name, nil, nil, protocol_options)
end
def encode_request(method_name, params, param_types)
param_types.each{ |type| marshaler.register_type(type) } if param_types
qname = XSD::QName.new(marshaler.namespace, method_name)
param_def = []
if param_types
params = param_types.zip(params).map do |type, param|
param_def << ['in', type.name, marshaler.lookup_type(type).mapping]
[type.name, marshaler.ruby_to_soap(param)]
end
else
params = []
end
request = SOAP::RPC::SOAPMethodRequest.new(qname, param_def)
request.set_param(params)
envelope = create_soap_envelope(request)
SOAP::Processor.marshal(envelope)
end
def decode_response(raw_response)
envelope = SOAP::Processor.unmarshal(raw_response)
unless envelope
raise ProtocolError, "Failed to parse SOAP request message"
end
method_name = envelope.body.request.elename.name
return_value = envelope.body.response
return_value = marshaler.soap_to_ruby(return_value) unless return_value.nil?
[method_name, return_value]
end
def encode_response(method_name, return_value, return_type, protocol_options={})
if return_type
return_binding = marshaler.register_type(return_type)
marshaler.annotate_arrays(return_binding, return_value)
end
qname = XSD::QName.new(marshaler.namespace, method_name)
if return_value.nil?
response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
else
if return_value.is_a?(Exception)
detail = SOAP::Mapping::SOAPException.new(return_value)
response = SOAP::SOAPFault.new(
SOAP::SOAPQName.new('%s:%s' % [SOAP::SOAPNamespaceTag, 'Server']),
SOAP::SOAPString.new(return_value.to_s),
SOAP::SOAPString.new(self.class.name),
marshaler.ruby_to_soap(detail))
else
if return_type
param_def = [['retval', 'return', marshaler.lookup_type(return_type).mapping]]
response = SOAP::RPC::SOAPMethodResponse.new(qname, param_def)
response.retval = marshaler.ruby_to_soap(return_value)
else
response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
end
end
end
envelope = create_soap_envelope(response)
# FIXME: This is not thread-safe, but StringFactory_ in SOAP4R only
# reads target encoding from the XSD::Charset.encoding variable.
# This is required to ensure $KCODE strings are converted
# correctly to UTF-8 for any values of $KCODE.
previous_encoding = XSD::Charset.encoding
XSD::Charset.encoding = XSDEncoding
response_body = SOAP::Processor.marshal(envelope, :charset => AWSEncoding)
XSD::Charset.encoding = previous_encoding
Response.new(response_body, "text/xml; charset=#{AWSEncoding}", return_value)
end
def protocol_client(api, protocol_name, endpoint_uri, options={})
return nil unless protocol_name == :soap
ActionWebService::Client::Soap.new(api, endpoint_uri, options)
end
def register_api(api)
api.api_methods.each do |name, method|
method.expects.each{ |type| marshaler.register_type(type) } if method.expects
method.returns.each{ |type| marshaler.register_type(type) } if method.returns
end
end
private
def has_valid_soap_action?(request)
return nil unless request.method == :post
soap_action = request.env['HTTP_SOAPACTION']
return nil unless soap_action
soap_action = soap_action.dup
soap_action.gsub!(/^"/, '')
soap_action.gsub!(/"$/, '')
soap_action.strip!
return nil if soap_action.empty?
soap_action
end
def create_soap_envelope(body)
header = SOAP::SOAPHeader.new
body = SOAP::SOAPBody.new(body)
SOAP::SOAPEnvelope.new(header, body)
end
def parse_charset(content_type)
return AWSEncoding if content_type.nil?
if /^text\/xml(?:\s*;\s*charset=([^"]+|"[^"]+"))$/i =~ content_type
$1
else
AWSEncoding
end
end
end
end
end
end

View file

@ -0,0 +1,241 @@
require 'soap/mapping'
module ActionWebService
module Protocol
module Soap
# Workaround for SOAP4R return values changing
class Registry < SOAP::Mapping::Registry
if SOAP::Version >= "1.5.4"
def find_mapped_soap_class(obj_class)
return @map.instance_eval { @obj2soap[obj_class][0] }
end
def find_mapped_obj_class(soap_class)
return @map.instance_eval { @soap2obj[soap_class][0] }
end
end
end
class SoapMarshaler
attr :namespace
attr :registry
def initialize(namespace=nil)
@namespace = namespace || 'urn:ActionWebService'
@registry = Registry.new
@type2binding = {}
register_static_factories
end
def soap_to_ruby(obj)
SOAP::Mapping.soap2obj(obj, @registry)
end
def ruby_to_soap(obj)
soap = SOAP::Mapping.obj2soap(obj, @registry)
soap.elename = XSD::QName.new if SOAP::Version >= "1.5.5" && soap.elename == XSD::QName::EMPTY
soap
end
def register_type(type)
return @type2binding[type] if @type2binding.has_key?(type)
if type.array?
array_mapping = @registry.find_mapped_soap_class(Array)
qname = XSD::QName.new(@namespace, soap_type_name(type.element_type.type_class.name) + 'Array')
element_type_binding = register_type(type.element_type)
@type2binding[type] = SoapBinding.new(self, qname, type, array_mapping, element_type_binding)
elsif (mapping = @registry.find_mapped_soap_class(type.type_class) rescue nil)
qname = mapping[2] ? mapping[2][:type] : nil
qname ||= soap_base_type_name(mapping[0])
@type2binding[type] = SoapBinding.new(self, qname, type, mapping)
else
qname = XSD::QName.new(@namespace, soap_type_name(type.type_class.name))
@registry.add(type.type_class,
SOAP::SOAPStruct,
typed_struct_factory(type.type_class),
{ :type => qname })
mapping = @registry.find_mapped_soap_class(type.type_class)
@type2binding[type] = SoapBinding.new(self, qname, type, mapping)
end
if type.structured?
type.each_member do |m_name, m_type|
register_type(m_type)
end
end
@type2binding[type]
end
alias :lookup_type :register_type
def annotate_arrays(binding, value)
if value.nil?
return
elsif binding.type.array?
mark_typed_array(value, binding.element_binding.qname)
if binding.element_binding.type.custom?
value.each do |element|
annotate_arrays(binding.element_binding, element)
end
end
elsif binding.type.structured?
binding.type.each_member do |name, type|
member_binding = register_type(type)
member_value = value.respond_to?('[]') ? value[name] : value.send(name)
annotate_arrays(member_binding, member_value) if type.custom?
end
end
end
private
def typed_struct_factory(type_class)
if Object.const_defined?('ActiveRecord')
if type_class.ancestors.include?(ActiveRecord::Base)
qname = XSD::QName.new(@namespace, soap_type_name(type_class.name))
type_class.instance_variable_set('@qname', qname)
return SoapActiveRecordStructFactory.new
end
end
SOAP::Mapping::Registry::TypedStructFactory
end
def mark_typed_array(array, qname)
(class << array; self; end).class_eval do
define_method(:arytype) do
qname
end
end
end
def soap_base_type_name(type)
xsd_type = type.ancestors.find{ |c| c.const_defined? 'Type' }
xsd_type ? xsd_type.const_get('Type') : XSD::XSDAnySimpleType::Type
end
def soap_type_name(type_name)
type_name.gsub(/::/, '..')
end
def register_static_factories
@registry.add(ActionWebService::Base64,
SOAP::SOAPBase64,
SoapBase64Factory.new,
nil)
mapping = @registry.find_mapped_soap_class(ActionWebService::Base64)
@type2binding[ActionWebService::Base64] =
SoapBinding.new(self, SOAP::SOAPBase64::Type,
ActionWebService::Base64, mapping)
@registry.add(Array,
SOAP::SOAPArray,
SoapTypedArrayFactory.new,
nil)
end
end
class SoapBinding
attr :qname
attr :type
attr :mapping
attr :element_binding
def initialize(marshaler, qname, type, mapping, element_binding=nil)
@marshaler = marshaler
@qname = qname
@type = type
@mapping = mapping
@element_binding = element_binding
end
def type_name
@type.custom? ? @qname.name : nil
end
def qualified_type_name(ns=nil)
if @type.custom?
"#{ns ? ns : @qname.namespace}:#{@qname.name}"
else
ns = XSD::NS.new
ns.assign(XSD::Namespace, SOAP::XSDNamespaceTag)
ns.assign(SOAP::EncodingNamespace, "soapenc")
xsd_klass = mapping[0].ancestors.find{|c| c.const_defined?('Type')}
return ns.name(XSD::AnyTypeName) unless xsd_klass
ns.name(xsd_klass.const_get('Type'))
end
end
def eql?(other)
@qname == other.qname
end
alias :== :eql?
def hash
@qname.hash
end
end
class SoapActiveRecordStructFactory < SOAP::Mapping::Factory
def obj2soap(soap_class, obj, info, map)
unless obj.is_a?(ActiveRecord::Base)
return nil
end
soap_obj = soap_class.new(obj.class.instance_variable_get('@qname'))
obj.class.columns.each do |column|
key = column.name.to_s
value = obj.send(key)
soap_obj[key] = SOAP::Mapping._obj2soap(value, map)
end
soap_obj
end
def soap2obj(obj_class, node, info, map)
unless node.type == obj_class.instance_variable_get('@qname')
return false
end
obj = obj_class.new
node.each do |key, value|
obj[key] = value.data
end
obj.instance_variable_set('@new_record', false)
return true, obj
end
end
class SoapTypedArrayFactory < SOAP::Mapping::Factory
def obj2soap(soap_class, obj, info, map)
unless obj.respond_to?(:arytype)
return nil
end
soap_obj = soap_class.new(SOAP::ValueArrayName, 1, obj.arytype)
mark_marshalled_obj(obj, soap_obj)
obj.each do |item|
child = SOAP::Mapping._obj2soap(item, map)
soap_obj.add(child)
end
soap_obj
end
def soap2obj(obj_class, node, info, map)
return false
end
end
class SoapBase64Factory < SOAP::Mapping::Factory
def obj2soap(soap_class, obj, info, map)
unless obj.is_a?(ActionWebService::Base64)
return nil
end
return soap_class.new(obj)
end
def soap2obj(obj_class, node, info, map)
unless node.type == SOAP::SOAPBase64::Type
return false
end
return true, obj_class.new(node.string)
end
end
end
end
end

View file

@ -0,0 +1,97 @@
require 'xmlrpc/marshal'
require 'action_web_service/client/xmlrpc_client'
module XMLRPC # :nodoc:
class FaultException # :nodoc:
alias :message :faultString
end
end
module ActionWebService # :nodoc:
module API # :nodoc:
class Base # :nodoc:
def self.xmlrpc_client(endpoint_uri, options={})
ActionWebService::Client::XmlRpc.new self, endpoint_uri, options
end
end
end
module Protocol # :nodoc:
module XmlRpc # :nodoc:
def self.included(base)
base.register_protocol(XmlRpcProtocol)
end
class XmlRpcProtocol < AbstractProtocol # :nodoc:
def self.create(controller)
XmlRpcProtocol.new
end
def decode_action_pack_request(action_pack_request)
service_name = action_pack_request.parameters['action']
decode_request(action_pack_request.raw_post, service_name)
end
def decode_request(raw_request, service_name)
method_name, params = XMLRPC::Marshal.load_call(raw_request)
Request.new(self, method_name, params, service_name)
end
def encode_request(method_name, params, param_types)
if param_types
params = params.dup
param_types.each_with_index{ |type, i| params[i] = value_to_xmlrpc_wire_format(params[i], type) }
end
XMLRPC::Marshal.dump_call(method_name, *params)
end
def decode_response(raw_response)
[nil, XMLRPC::Marshal.load_response(raw_response)]
end
def encode_response(method_name, return_value, return_type, protocol_options={})
if return_value && return_type
return_value = value_to_xmlrpc_wire_format(return_value, return_type)
end
return_value = false if return_value.nil?
raw_response = XMLRPC::Marshal.dump_response(return_value)
Response.new(raw_response, 'text/xml', return_value)
end
def protocol_client(api, protocol_name, endpoint_uri, options={})
return nil unless protocol_name == :xmlrpc
ActionWebService::Client::XmlRpc.new(api, endpoint_uri, options)
end
def value_to_xmlrpc_wire_format(value, value_type)
if value_type.array?
value.map{ |val| value_to_xmlrpc_wire_format(val, value_type.element_type) }
else
if value.is_a?(ActionWebService::Struct)
struct = {}
value.class.members.each do |name, type|
member_value = value[name]
next if member_value.nil?
struct[name.to_s] = value_to_xmlrpc_wire_format(member_value, type)
end
struct
elsif value.is_a?(ActiveRecord::Base)
struct = {}
value.attributes.each do |key, member_value|
next if member_value.nil?
struct[key.to_s] = member_value
end
struct
elsif value.is_a?(ActionWebService::Base64)
XMLRPC::Base64.new(value)
elsif value.is_a?(Exception) && !value.is_a?(XMLRPC::FaultException)
XMLRPC::FaultException.new(2, value.message)
else
value
end
end
end
end
end
end
end