diff options
Diffstat (limited to 'lib/soap')
27 files changed, 938 insertions, 432 deletions
diff --git a/lib/soap/baseData.rb b/lib/soap/baseData.rb index 30963f1d64..49c1d2d1f4 100644 --- a/lib/soap/baseData.rb +++ b/lib/soap/baseData.rb @@ -1,5 +1,5 @@ # soap/baseData.rb: SOAP4R - Base type library -# Copyright (C) 2000, 2001, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2000, 2001, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -30,20 +30,10 @@ end ### -## Marker of SOAP/DM types. +## for SOAP type(base and compound) # -module SOAPType; end - - -### -## Mix-in module for SOAP base type instances. -# -module SOAPBasetype - include SOAPType - include SOAP - +module SOAPType attr_accessor :encodingstyle - attr_accessor :elename attr_accessor :id attr_reader :precedents @@ -51,17 +41,18 @@ module SOAPBasetype attr_accessor :parent attr_accessor :position attr_reader :extraattr + attr_accessor :definedtype -public - - def initialize(*vars) - super(*vars) + def initialize(*arg) + super(*arg) @encodingstyle = nil @elename = XSD::QName.new @id = nil @precedents = [] + @root = false @parent = nil @position = nil + @definedtype = nil @extraattr = {} end @@ -76,38 +67,27 @@ end ### -## Mix-in module for SOAP compound type instances. +## for SOAP base type # -module SOAPCompoundtype +module SOAPBasetype include SOAPType include SOAP - attr_accessor :encodingstyle - - attr_accessor :elename - attr_accessor :id - attr_reader :precedents - attr_accessor :root - attr_accessor :parent - attr_accessor :position - attr_reader :extraattr + def initialize(*arg) + super(*arg) + end +end - attr_accessor :definedtype -public +### +## for SOAP compound type +# +module SOAPCompoundtype + include SOAPType + include SOAP - def initialize(type) - super() - @type = type - @encodingstyle = nil - @elename = XSD::QName.new - @id = nil - @precedents = [] - @root = false - @parent = nil - @position = nil - @definedtype = nil - @extraattr = {} + def initialize(*arg) + super(*arg) end end @@ -122,18 +102,11 @@ class SOAPReference < XSD::NSDBase public attr_accessor :refid - attr_accessor :elename # Override the definition in SOAPBasetype. def initialize(obj = nil) super() @type = XSD::QName.new - @encodingstyle = nil - @elename = XSD::QName.new - @id = nil - @precedents = [] - @root = false - @parent = nil @refid = nil @obj = nil __setobj__(obj) if obj @@ -198,11 +171,6 @@ class SOAPExternalReference < XSD::NSDBase def initialize super() @type = XSD::QName.new - @encodingstyle = nil - @elename = XSD::QName.new - @precedents = [] - @root = false - @parent = nil end def referred @@ -377,7 +345,8 @@ class SOAPStruct < XSD::NSDBase public def initialize(type = nil) - super(type || XSD::QName.new) + super() + @type = type || XSD::QName.new @array = [] @data = [] end @@ -459,7 +428,7 @@ private end -# SOAPElement is not typed so it does not derive NSDBase. +# SOAPElement is not typed so it is not derived from NSDBase. class SOAPElement include Enumerable @@ -534,7 +503,7 @@ class SOAPElement else hash = {} each do |k, v| - hash[k] = v.to_obj + hash[k] = v.is_a?(SOAPElement) ? v.to_obj : v.to_s end hash end @@ -547,8 +516,7 @@ class SOAPElement end def self.decode(elename) - o = SOAPElement.new - o.elename = elename + o = SOAPElement.new(elename) o end @@ -557,7 +525,7 @@ class SOAPElement if hash_or_string.is_a?(Hash) hash_or_string.each do |k, v| child = self.from_obj(v) - child.elename = XSD::QName.new(nil, k) + child.elename = k.is_a?(XSD::QName) ? k : XSD::QName.new(nil, k.to_s) o.add(child) end else @@ -616,7 +584,8 @@ public attr_reader :arytype def initialize(type = nil, rank = 1, arytype = nil) - super(type || XSD::QName.new) + super() + @type = type || XSD::QName.new @rank = rank @data = Array.new @sparse = false diff --git a/lib/soap/element.rb b/lib/soap/element.rb index 29a075825a..1494cd61dd 100644 --- a/lib/soap/element.rb +++ b/lib/soap/element.rb @@ -1,5 +1,5 @@ # SOAP4R - SOAP elements library -# Copyright (C) 2000, 2001, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2000, 2001, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -95,8 +95,6 @@ end class SOAPBody < SOAPStruct include SOAPEnvelopeElement -public - def initialize(data = nil, is_fault = false) super(nil) @elename = EleBodyName @@ -142,39 +140,39 @@ class SOAPHeaderItem < XSD::NSDBase public - attr_accessor :content + attr_accessor :element attr_accessor :mustunderstand attr_accessor :encodingstyle - def initialize(content, mustunderstand = true, encodingstyle = nil) - super(nil) - @content = content + def initialize(element, mustunderstand = true, encodingstyle = nil) + super() + @type = nil + @element = element @mustunderstand = mustunderstand - @encodingstyle = encodingstyle || LiteralNamespace - content.parent = self if content + @encodingstyle = encodingstyle + element.parent = self if element end def encode(generator, ns, attrs = {}) attrs.each do |key, value| - @content.attr[key] = value + @element.extraattr[key] = value end - @content.attr[ns.name(EnvelopeNamespace, AttrMustUnderstand)] = + @element.extraattr[ns.name(AttrMustUnderstandName)] = (@mustunderstand ? '1' : '0') if @encodingstyle - @content.attr[ns.name(EnvelopeNamespace, AttrEncodingStyle)] = - @encodingstyle + @element.extraattr[ns.name(AttrEncodingStyleName)] = @encodingstyle end - @content.encodingstyle = @encodingstyle if !@content.encodingstyle - yield(@content, true) + @element.encodingstyle = @encodingstyle if !@element.encodingstyle + yield(@element, true) end end -class SOAPHeader < SOAPArray +class SOAPHeader < SOAPStruct include SOAPEnvelopeElement - def initialize() - super(nil, 1) # rank == 1 + def initialize + super(nil) @elename = EleHeaderName @encodingstyle = nil end @@ -188,9 +186,17 @@ class SOAPHeader < SOAPArray generator.encode_tag_end(name, true) end + def add(name, value) + mu = (value.extraattr[AttrMustUnderstandName] == '1') + encstyle = value.extraattr[AttrEncodingStyleName] + item = SOAPHeaderItem.new(value, mu, encstyle) + super(name, item) + end + def length @data.length end + alias size length end @@ -203,7 +209,8 @@ class SOAPEnvelope < XSD::NSDBase attr_reader :external_content def initialize(header = nil, body = nil) - super(nil) + super() + @type = nil @elename = EleEnvelopeName @encodingstyle = nil @header = header diff --git a/lib/soap/encodingstyle/handler.rb b/lib/soap/encodingstyle/handler.rb index 8ea23ef146..7bf65a2fd5 100644 --- a/lib/soap/encodingstyle/handler.rb +++ b/lib/soap/encodingstyle/handler.rb @@ -44,8 +44,8 @@ class Handler attr_reader :charset attr_accessor :generate_explicit_type - def decode_typemap=(complextypes) - @decode_typemap = complextypes + def decode_typemap=(definedtypes) + @decode_typemap = definedtypes end def initialize(charset) diff --git a/lib/soap/encodingstyle/literalHandler.rb b/lib/soap/encodingstyle/literalHandler.rb index 995e1a5361..72a10b2daa 100644 --- a/lib/soap/encodingstyle/literalHandler.rb +++ b/lib/soap/encodingstyle/literalHandler.rb @@ -1,5 +1,5 @@ # SOAP4R - XML Literal EncodingStyle handler library -# Copyright (C) 2001, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2001, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -41,14 +41,18 @@ class LiteralHandler < Handler generator.encode_rawstring(data.to_s) when XSD::XSDString generator.encode_tag(name, attrs) - generator.encode_string(@charset ? XSD::Charset.encoding_to_xml(data.to_s, @charset) : data.to_s) + str = data.to_s + str = XSD::Charset.encoding_to_xml(str, @charset) if @charset + generator.encode_string(str) when XSD::XSDAnySimpleType generator.encode_tag(name, attrs) generator.encode_string(data.to_s) when SOAPStruct generator.encode_tag(name, attrs) data.each do |key, value| - value.elename.namespace = data.elename.namespace if !value.elename.namespace + if !value.elename.namespace + value.elename.namespace = data.elename.namespace + end yield(value, true) end when SOAPArray @@ -61,8 +65,6 @@ class LiteralHandler < Handler generator.encode_tag(name, attrs.update(data.extraattr)) generator.encode_rawstring(data.text) if data.text data.each do |key, value| - value.elename.namespace = data.elename.namespace if !value.elename.namespace - #yield(value, data.qualified) yield(value, qualified) end else @@ -76,7 +78,8 @@ class LiteralHandler < Handler else data.elename.name end - generator.encode_tag_end(name) + cr = data.is_a?(SOAPElement) && !data.text + generator.encode_tag_end(name, cr) end @@ -92,15 +95,17 @@ class LiteralHandler < Handler end class SOAPUnknown < SOAPTemporalObject - def initialize(handler, elename) + def initialize(handler, elename, extraattr) super() @handler = handler @elename = elename + @extraattr = extraattr end - def as_struct - o = SOAPStruct.decode(@elename, XSD::AnyTypeName) + def as_element + o = SOAPElement.decode(@elename) o.parent = @parent + o.extraattr.update(@extraattr) @handler.decode_parent(@parent, o) o end @@ -108,6 +113,7 @@ class LiteralHandler < Handler def as_string o = SOAPString.decode(@elename) o.parent = @parent + o.extraattr.update(@extraattr) @handler.decode_parent(@parent, o) o end @@ -115,6 +121,7 @@ class LiteralHandler < Handler def as_nil o = SOAPNil.decode(@elename) o.parent = @parent + o.extraattr.update(@extraattr) @handler.decode_parent(@parent, o) o end @@ -123,7 +130,7 @@ class LiteralHandler < Handler def decode_tag(ns, elename, attrs, parent) # ToDo: check if @textbuf is empty... @textbuf = '' - o = SOAPUnknown.new(self, elename) + o = SOAPUnknown.new(self, elename, decode_attrs(ns, attrs)) o.parent = parent o end @@ -132,7 +139,7 @@ class LiteralHandler < Handler o = node.node if o.is_a?(SOAPUnknown) newnode = if /\A\s*\z/ =~ @textbuf - o.as_struct + o.as_element else o.as_string end @@ -149,6 +156,15 @@ class LiteralHandler < Handler @textbuf << text end + def decode_attrs(ns, attrs) + extraattr = {} + attrs.each do |key, value| + qname = ns.parse(key) + extraattr[qname] = value + end + extraattr + end + def decode_prologue end @@ -158,13 +174,18 @@ class LiteralHandler < Handler def decode_parent(parent, node) case parent.node when SOAPUnknown - newparent = parent.node.as_struct + newparent = parent.node.as_element node.parent = newparent parent.replace_node(newparent) decode_parent(parent, node) + when SOAPElement + parent.node.add(node) + node.parent = parent.node + when SOAPStruct - parent.node.add(node.name, node) + parent.node.add(node.elename.name, node) + node.parent = parent.node when SOAPArray if node.position @@ -173,13 +194,14 @@ class LiteralHandler < Handler else parent.node.add(node) end + node.parent = parent.node when SOAPBasetype raise EncodingStyleError.new("SOAP base type must not have a child.") else # SOAPUnknown does not have parent. - # raise EncodingStyleError.new("Illegal parent: #{ parent }.") + raise EncodingStyleError.new("Illegal parent: #{ parent }.") end end diff --git a/lib/soap/encodingstyle/soapHandler.rb b/lib/soap/encodingstyle/soapHandler.rb index d755d7b952..114060bd02 100644 --- a/lib/soap/encodingstyle/soapHandler.rb +++ b/lib/soap/encodingstyle/soapHandler.rb @@ -33,7 +33,7 @@ class SOAPHandler < Handler attrs = encode_attrs(generator, ns, data, parent) if parent && parent.is_a?(SOAPArray) && parent.position - attrs[ns.name(AttrPositionName)] = '[' << parent.position.join(',') << ']' + attrs[ns.name(AttrPositionName)] = "[#{ parent.position.join(',') }]" end name = nil @@ -207,16 +207,12 @@ class SOAPHandler < Handler node.replace_node(newnode) o = node.node end - if o.is_a?(SOAPCompoundtype) - o.definedtype = nil - end - decode_textbuf(o) - @textbuf = '' + # unlink definedtype + o.definedtype = nil end def decode_text(ns, text) - # @textbuf is set at decode_tag_end. @textbuf << text end @@ -240,11 +236,9 @@ class SOAPHandler < Handler end parent.replace_node(newparent) decode_parent(parent, node) - when SOAPStruct parent.node.add(node.elename.name, node) node.parent = parent.node - when SOAPArray if node.position parent.node[*(decode_arypos(node.position))] = node @@ -253,10 +247,8 @@ class SOAPHandler < Handler parent.node.add(node) end node.parent = parent.node - when SOAPBasetype raise EncodingStyleError.new("SOAP base type must not have a child.") - else raise EncodingStyleError.new("Illegal parent: #{ parent.node }.") end @@ -274,7 +266,7 @@ private def create_arytype(ns, data) XSD::QName.new(data.arytype.namespace, - content_typename(data.arytype.name) << '[' << data.size.join(',') << ']') + content_typename(data.arytype.name) + "[#{ data.size.join(',') }]") end def encode_attrs(generator, ns, data, parent) @@ -353,8 +345,7 @@ private typename = ns.parse(typestr) typedef = @decode_typemap[typename] if typedef - return decode_defined_compoundtype(elename, typename, typedef, - arytypestr) + return decode_definedtype(elename, typename, typedef, arytypestr) end end return decode_tag_by_type(ns, elename, typestr, parent, arytypestr, @@ -376,21 +367,42 @@ private definedtype_name = parenttype.child_type(elename) if definedtype_name and (klass = TypeMap[definedtype_name]) - return klass.decode(elename) + return decode_basetype(klass, elename) elsif definedtype_name == XSD::AnyTypeName return decode_tag_by_type(ns, elename, typestr, parent, arytypestr, extraattr) end - typedef = definedtype_name ? @decode_typemap[definedtype_name] : - parenttype.child_defined_complextype(elename) - decode_defined_compoundtype(elename, definedtype_name, typedef, arytypestr) + if definedtype_name + typedef = @decode_typemap[definedtype_name] + else + typedef = parenttype.child_defined_complextype(elename) + end + decode_definedtype(elename, definedtype_name, typedef, arytypestr) end - def decode_defined_compoundtype(elename, typename, typedef, arytypestr) + def decode_definedtype(elename, typename, typedef, arytypestr) unless typedef raise EncodingStyleError.new("Unknown type '#{ typename }'.") end + if typedef.is_a?(::WSDL::XMLSchema::SimpleType) + decode_defined_simpletype(elename, typename, typedef, arytypestr) + else + decode_defined_complextype(elename, typename, typedef, arytypestr) + end + end + + def decode_basetype(klass, elename) + klass.decode(elename) + end + + def decode_defined_simpletype(elename, typename, typedef, arytypestr) + o = decode_basetype(TypeMap[typedef.base], elename) + o.definedtype = typedef + o + end + + def decode_defined_complextype(elename, typename, typedef, arytypestr) case typedef.compoundtype when :TYPE_STRUCT o = SOAPStruct.decode(elename, typename) @@ -410,7 +422,7 @@ private o.definedtype = typedef return o end - return nil + nil end def decode_tag_by_type(ns, elename, typestr, parent, arytypestr, extraattr) @@ -435,7 +447,7 @@ private end if (klass = TypeMap[type]) - node = klass.decode(elename) + node = decode_basetype(klass, elename) node.extraattr.update(extraattr) return node end @@ -450,10 +462,12 @@ private node.set_encoded(@textbuf) when XSD::XSDString if @charset - node.set(XSD::Charset.encoding_from_xml(@textbuf, @charset)) - else - node.set(@textbuf) + @textbuf = XSD::Charset.encoding_from_xml(@textbuf, @charset) + end + if node.definedtype + node.definedtype.check_lexical_format(@textbuf) end + node.set(@textbuf) when SOAPNil # Nothing to do. when SOAPBasetype @@ -461,6 +475,7 @@ private else # Nothing to do... end + @textbuf = '' end NilLiteralMap = { diff --git a/lib/soap/generator.rb b/lib/soap/generator.rb index 5da6b8f26e..edd90492c6 100644 --- a/lib/soap/generator.rb +++ b/lib/soap/generator.rb @@ -90,9 +90,9 @@ public raise FormatEncodeError.new("Element name not defined: #{ obj }.") end - handler.encode_data(self, ns, qualified, obj, parent) do |child, child_q| + handler.encode_data(self, ns, qualified, obj, parent) do |child, nextq| indent_backup, @indent = @indent, @indent + ' ' - encode_data(ns.clone_ns, child_q, child, obj) + encode_data(ns.clone_ns, nextq, child, obj) @indent = indent_backup end handler.encode_data_end(self, ns, qualified, obj, parent) @@ -109,9 +109,9 @@ public attrs = {} if obj.is_a?(SOAPBody) @reftarget = obj - obj.encode(self, ns, attrs) do |child, child_q| + obj.encode(self, ns, attrs) do |child, nextq| indent_backup, @indent = @indent, @indent + ' ' - encode_data(ns.clone_ns, child_q, child, obj) + encode_data(ns.clone_ns, nextq, child, obj) @indent = indent_backup end @reftarget = nil @@ -124,9 +124,9 @@ public SOAPGenerator.assign_ns(attrs, ns, XSD::Namespace, XSDNamespaceTag) end end - obj.encode(self, ns, attrs) do |child, child_q| + obj.encode(self, ns, attrs) do |child, nextq| indent_backup, @indent = @indent, @indent + ' ' - encode_data(ns.clone_ns, child_q, child, obj) + encode_data(ns.clone_ns, nextq, child, obj) @indent = indent_backup end end diff --git a/lib/soap/header/handler.rb b/lib/soap/header/handler.rb new file mode 100644 index 0000000000..7da2836e24 --- /dev/null +++ b/lib/soap/header/handler.rb @@ -0,0 +1,57 @@ +# SOAP4R - SOAP Header handler item +# Copyright (C) 2003, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. + +# This program is copyrighted free software by NAKAMURA, Hiroshi. You can +# redistribute it and/or modify it under the same terms of Ruby's license; +# either the dual license version in 2003, or any later version. + + +require 'soap/element' + + +module SOAP +module Header + + +class Handler + attr_reader :elename + attr_reader :mustunderstand + attr_reader :encodingstyle + + def initialize(elename) + @elename = elename + @mustunderstand = false + @encodingstyle = nil + end + + # Should return a SOAP/OM, a SOAPHeaderItem or nil. + def on_outbound + nil + end + + # Given header is a SOAPHeaderItem or nil. + def on_inbound(header, mustunderstand = false) + # do something. + end + + def on_outbound_headeritem + item = on_outbound + if item.nil? + nil + elsif item.is_a?(::SOAP::SOAPHeaderItem) + item.elename = @elename + item + else + item.elename = @elename + ::SOAP::SOAPHeaderItem.new(item, @mustunderstand, @encodingstyle) + end + end + + def on_inbound_headeritem(header) + on_inbound(header.element, header.mustunderstand) + end +end + + +end +end diff --git a/lib/soap/header/handlerset.rb b/lib/soap/header/handlerset.rb new file mode 100644 index 0000000000..499d6bb8a1 --- /dev/null +++ b/lib/soap/header/handlerset.rb @@ -0,0 +1,58 @@ +# SOAP4R - SOAP Header handler set +# Copyright (C) 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. + +# This program is copyrighted free software by NAKAMURA, Hiroshi. You can +# redistribute it and/or modify it under the same terms of Ruby's license; +# either the dual license version in 2003, or any later version. + + +require 'xsd/namedelements' + + +module SOAP +module Header + + +class HandlerSet + def initialize + @store = XSD::NamedElements.new + end + + def add(handler) + @store << handler + end + alias << add + + def delete(handler) + @store.delete(handler) + end + + def include?(handler) + @store.include?(handler) + end + + # returns: Array of SOAPHeaderItem + def on_outbound + @store.collect { |handler| + handler.on_outbound_headeritem + }.compact + end + + # headers: SOAPHeaderItem enumerable object + def on_inbound(headers) + headers.each do |name, item| + handler = @store.find { |handler| + handler.elename == item.element.elename + } + if handler + handler.on_inbound_headeritem(item) + elsif item.mustunderstand + raise UnhandledMustUnderstandHeaderError.new(item.element.elename.to_s) + end + end + end +end + + +end +end diff --git a/lib/soap/header/simplehandler.rb b/lib/soap/header/simplehandler.rb new file mode 100644 index 0000000000..e7268e04a3 --- /dev/null +++ b/lib/soap/header/simplehandler.rb @@ -0,0 +1,44 @@ +# SOAP4R - SOAP Simple header item handler +# Copyright (C) 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. + +# This program is copyrighted free software by NAKAMURA, Hiroshi. You can +# redistribute it and/or modify it under the same terms of Ruby's license; +# either the dual license version in 2003, or any later version. + + +require 'soap/header/handler' +require 'soap/baseData' + + +module SOAP +module Header + + +class SimpleHandler < SOAP::Header::Handler + def initialize(elename) + super(elename) + end + + # Should return a Hash or nil. + def on_simple_outbound + nil + end + + # Given header is a Hash or nil. + def on_simple_inbound(header, mustunderstand) + end + + def on_outbound + h = on_simple_outbound + h ? SOAPElement.from_obj(h) : nil + end + + def on_inbound(header, mustunderstand) + h = header.to_obj + on_simple_inbound(h, mustunderstand) + end +end + + +end +end diff --git a/lib/soap/mapping/factory.rb b/lib/soap/mapping/factory.rb index fe6a6de7ae..6b9ac1eeaa 100644 --- a/lib/soap/mapping/factory.rb +++ b/lib/soap/mapping/factory.rb @@ -70,6 +70,7 @@ class Factory end def setiv2soap(node, obj, map) + # should we sort instance_variables? obj.instance_variables.each do |var| name = var.sub(/^@/, '') node.add(Mapping.name2elename(name), diff --git a/lib/soap/mapping/mapping.rb b/lib/soap/mapping/mapping.rb index 38a01bac07..db7ea607fd 100644 --- a/lib/soap/mapping/mapping.rb +++ b/lib/soap/mapping/mapping.rb @@ -68,24 +68,26 @@ module Mapping md_ary end - def self.fault2exception(e, registry = nil) + def self.fault2exception(fault, registry = nil) registry ||= Mapping::DefaultRegistry - detail = if e.detail - soap2obj(e.detail, registry) || "" + detail = if fault.detail + soap2obj(fault.detail, registry) || "" else "" end if detail.is_a?(Mapping::SOAPException) begin - remote_backtrace = detail.to_e.backtrace - raise detail.to_e - rescue Exception => e2 - e2.set_backtrace(remote_backtrace + e2.backtrace) + e = detail.to_e + remote_backtrace = e.backtrace + e.set_backtrace(nil) + raise e # ruby sets current caller as local backtrace of e => e2. + rescue Exception => e + e.set_backtrace(remote_backtrace + e.backtrace[1..-1]) raise end else - e.detail = detail - e.set_backtrace( + fault.detail = detail + fault.set_backtrace( if detail.is_a?(Array) detail else diff --git a/lib/soap/mapping/registry.rb b/lib/soap/mapping/registry.rb index 8142047724..1317d40cd6 100644 --- a/lib/soap/mapping/registry.rb +++ b/lib/soap/mapping/registry.rb @@ -44,14 +44,15 @@ class SOAPException; include Marshallable if @cause.is_a?(::Exception) @cause.extend(::SOAP::Mapping::MappedException) return @cause + elsif @cause.respond_to?(:message) and @cause.respond_to?(:backtrace) + e = RuntimeError.new(@cause.message) + e.set_backtrace(@cause.backtrace) + return e end klass = Mapping.class_from_name( Mapping.elename2name(@excn_type_name.to_s)) - if klass.nil? - raise RuntimeError.new(@cause.message) - end - unless klass <= ::Exception - raise NameError.new + if klass.nil? or not klass <= ::Exception + return RuntimeError.new(@cause.inspect) end obj = klass.new(@cause.message) obj.extend(::SOAP::Mapping::MappedException) @@ -62,50 +63,78 @@ end # For anyType object: SOAP::Mapping::Object not ::Object class Object; include Marshallable - def set_property(name, value) - var_name = name - begin - instance_eval <<-EOS - def #{ var_name } - @#{ var_name } - end + def initialize + @__members = [] + @__value_type = {} + end - def #{ var_name }=(value) - @#{ var_name } = value - end - EOS - self.send(var_name + '=', value) - rescue SyntaxError - var_name = safe_name(var_name) - retry + def [](name) + if @__members.include?(name) + self.__send__(name) + else + self.__send__(Object.safe_name(name)) end + end + + def []=(name, value) + if @__members.include?(name) + self.__send__(name + '=', value) + else + self.__send__(Object.safe_name(name) + '=', value) + end + end + def __set_property(name, value) + var_name = name + unless @__members.include?(name) + var_name = __define_attr_accessor(var_name) + end + __set_property_value(var_name, value) var_name end - def members - instance_variables.collect { |str| str[1..-1] } + def __members + @__members end - def [](name) - if self.respond_to?(name) - self.send(name) +private + + def __set_property_value(name, value) + org = self.__send__(name) + case @__value_type[name] + when :single + self.__send__(name + '=', [org, value]) + @__value_type[name] = :multi + when :multi + org << value else - self.send(safe_name(name)) + self.__send__(name + '=', value) + @__value_type[name] = :single end + value end - def []=(name, value) - if self.respond_to?(name) - self.send(name + '=', value) - else - self.send(safe_name(name) + '=', value) + def __define_attr_accessor(name) + var_name = name + begin + instance_eval <<-EOS + def #{ var_name } + @#{ var_name } + end + + def #{ var_name }=(value) + @#{ var_name } = value + end + EOS + rescue SyntaxError + var_name = Object.safe_name(var_name) + retry end + @__members << var_name + var_name end -private - - def safe_name(name) + def Object.safe_name(name) require 'md5' "var_" << MD5.new(name).hexdigest end @@ -309,7 +338,7 @@ class Registry def add(obj_class, soap_class, factory, info = nil) @map.add(obj_class, soap_class, factory, info) end - alias :set :add + alias set add # This mapping registry ignores type hint. def obj2soap(klass, obj, type_qname = nil) diff --git a/lib/soap/mapping/rubytypeFactory.rb b/lib/soap/mapping/rubytypeFactory.rb index f79bc78cc7..a46d93275f 100644 --- a/lib/soap/mapping/rubytypeFactory.rb +++ b/lib/soap/mapping/rubytypeFactory.rb @@ -38,7 +38,7 @@ class RubytypeFactory < Factory def obj2soap(soap_class, obj, info, map) param = nil case obj - when String + when ::String unless @allow_original_mapping return nil end @@ -47,7 +47,7 @@ class RubytypeFactory < Factory param.extraattr[RubyTypeName] = obj.class.name end addiv2soapattr(param, obj, map) - when Time + when ::Time unless @allow_original_mapping return nil end @@ -56,7 +56,7 @@ class RubytypeFactory < Factory param.extraattr[RubyTypeName] = obj.class.name end addiv2soapattr(param, obj, map) - when Array + when ::Array unless @allow_original_mapping return nil end @@ -65,19 +65,19 @@ class RubytypeFactory < Factory param.extraattr[RubyTypeName] = obj.class.name end addiv2soapattr(param, obj, map) - when NilClass + when ::NilClass unless @allow_original_mapping return nil end param = @basetype_factory.obj2soap(SOAPNil, obj, info, map) addiv2soapattr(param, obj, map) - when FalseClass, TrueClass + when ::FalseClass, ::TrueClass unless @allow_original_mapping return nil end param = @basetype_factory.obj2soap(SOAPBoolean, obj, info, map) addiv2soapattr(param, obj, map) - when Integer + when ::Integer unless @allow_original_mapping return nil end @@ -85,7 +85,7 @@ class RubytypeFactory < Factory param ||= @basetype_factory.obj2soap(SOAPInteger, obj, info, map) param ||= @basetype_factory.obj2soap(SOAPDecimal, obj, info, map) addiv2soapattr(param, obj, map) - when Float + when ::Float unless @allow_original_mapping return nil end @@ -94,7 +94,7 @@ class RubytypeFactory < Factory param.extraattr[RubyTypeName] = obj.class.name end addiv2soapattr(param, obj, map) - when Hash + when ::Hash unless @allow_original_mapping return nil end @@ -114,7 +114,7 @@ class RubytypeFactory < Factory end param.add('default', Mapping._obj2soap(obj.default, map)) addiv2soapattr(param, obj, map) - when Regexp + when ::Regexp unless @allow_original_mapping return nil end @@ -150,7 +150,7 @@ class RubytypeFactory < Factory end param.add('options', SOAPInt.new(options)) addiv2soapattr(param, obj, map) - when Range + when ::Range unless @allow_original_mapping return nil end @@ -163,29 +163,29 @@ class RubytypeFactory < Factory param.add('end', Mapping._obj2soap(obj.end, map)) param.add('exclude_end', SOAP::SOAPBoolean.new(obj.exclude_end?)) addiv2soapattr(param, obj, map) - when Class + when ::Class unless @allow_original_mapping return nil end if obj.to_s[0] == ?# - raise TypeError.new("Can't dump anonymous class #{ obj }.") + raise TypeError.new("can't dump anonymous class #{ obj }") end param = SOAPStruct.new(TYPE_CLASS) mark_marshalled_obj(obj, param) param.add('name', SOAPString.new(obj.name)) addiv2soapattr(param, obj, map) - when Module + when ::Module unless @allow_original_mapping return nil end if obj.to_s[0] == ?# - raise TypeError.new("Can't dump anonymous module #{ obj }.") + raise TypeError.new("can't dump anonymous module #{ obj }") end param = SOAPStruct.new(TYPE_MODULE) mark_marshalled_obj(obj, param) param.add('name', SOAPString.new(obj.name)) addiv2soapattr(param, obj, map) - when Symbol + when ::Symbol unless @allow_original_mapping return nil end @@ -193,28 +193,37 @@ class RubytypeFactory < Factory mark_marshalled_obj(obj, param) param.add('id', SOAPString.new(obj.id2name)) addiv2soapattr(param, obj, map) - when Struct + when ::Struct unless @allow_original_mapping - return nil - end - param = SOAPStruct.new(TYPE_STRUCT) - mark_marshalled_obj(obj, param) - param.add('type', ele_type = SOAPString.new(obj.class.to_s)) - ele_member = SOAPStruct.new - obj.members.each do |member| - ele_member.add(Mapping.name2elename(member), - Mapping._obj2soap(obj[member], map)) + # treat it as an user defined class. [ruby-talk:104980] + #param = unknownobj2soap(soap_class, obj, info, map) + param = SOAPStruct.new(XSD::AnyTypeName) + mark_marshalled_obj(obj, param) + obj.members.each do |member| + param.add(Mapping.name2elename(member), + Mapping._obj2soap(obj[member], map)) + end + else + param = SOAPStruct.new(TYPE_STRUCT) + mark_marshalled_obj(obj, param) + param.add('type', ele_type = SOAPString.new(obj.class.to_s)) + ele_member = SOAPStruct.new + obj.members.each do |member| + ele_member.add(Mapping.name2elename(member), + Mapping._obj2soap(obj[member], map)) + end + param.add('member', ele_member) + addiv2soapattr(param, obj, map) end - param.add('member', ele_member) - addiv2soapattr(param, obj, map) - when IO, Binding, Continuation, Data, Dir, File::Stat, MatchData, Method, - Proc, Thread, ThreadGroup # from 1.8: Process::Status, UnboundMethod + when ::IO, ::Binding, ::Continuation, ::Data, ::Dir, ::File::Stat, + ::MatchData, Method, ::Proc, ::Thread, ::ThreadGroup + # from 1.8: Process::Status, UnboundMethod return nil when ::SOAP::Mapping::Object param = SOAPStruct.new(XSD::AnyTypeName) mark_marshalled_obj(obj, param) addiv2soapattr(param, obj, map) - when Exception + when ::Exception typestr = Mapping.name2elename(obj.class.to_s) param = SOAPStruct.new(XSD::QName.new(RubyTypeNamespace, typestr)) mark_marshalled_obj(obj, param) @@ -249,7 +258,7 @@ private def unknownobj2soap(soap_class, obj, info, map) if obj.class.name.empty? - raise TypeError.new("Can't dump anonymous class #{ obj }.") + raise TypeError.new("can't dump anonymous class #{ obj }") end singleton_class = class << obj; self; end if !singleton_methods_true(obj).empty? or @@ -369,7 +378,7 @@ private obj = klass.new mark_unmarshalled_obj(node, obj) node.each do |name, value| - obj.set_property(name, Mapping._soap2obj(value, map)) + obj.__set_property(name, Mapping._soap2obj(value, map)) end return true, obj else diff --git a/lib/soap/mapping/wsdlRegistry.rb b/lib/soap/mapping/wsdlRegistry.rb index 66d16c6f90..64f49f2265 100644 --- a/lib/soap/mapping/wsdlRegistry.rb +++ b/lib/soap/mapping/wsdlRegistry.rb @@ -18,10 +18,10 @@ module Mapping class WSDLRegistry include TraverseSupport - attr_reader :complextypes + attr_reader :definedtypes - def initialize(complextypes, config = {}) - @complextypes = complextypes + def initialize(definedtypes, config = {}) + @definedtypes = definedtypes @config = config @excn_handler_obj2soap = nil # For mapping AnyType element. @@ -37,27 +37,20 @@ class WSDLRegistry soap_obj = SOAPNil.new elsif obj.is_a?(XSD::NSDBase) soap_obj = soap2soap(obj, type_qname) - elsif (type = @complextypes[type_qname]) - case type.compoundtype - when :TYPE_STRUCT - soap_obj = struct2soap(obj, type_qname, type) - when :TYPE_ARRAY - soap_obj = array2soap(obj, type_qname, type) - end + elsif type = @definedtypes[type_qname] + soap_obj = obj2type(obj, type) elsif (type = TypeMap[type_qname]) soap_obj = base2soap(obj, type) elsif type_qname == XSD::AnyTypeName soap_obj = @rubytype_factory.obj2soap(nil, obj, nil, nil) end return soap_obj if soap_obj - if @excn_handler_obj2soap soap_obj = @excn_handler_obj2soap.call(obj) { |yield_obj| Mapping._obj2soap(yield_obj, self) } end return soap_obj if soap_obj - raise MappingError.new("Cannot map #{ klass.name } to SOAP/OM.") end @@ -74,12 +67,12 @@ private def soap2soap(obj, type_qname) if obj.is_a?(SOAPBasetype) obj - elsif obj.is_a?(SOAPStruct) && (type = @complextypes[type_qname]) + elsif obj.is_a?(SOAPStruct) && (type = @definedtypes[type_qname]) soap_obj = obj mark_marshalled_obj(obj, soap_obj) elements2soap(obj, soap_obj, type.content.elements) soap_obj - elsif obj.is_a?(SOAPArray) && (type = @complextypes[type_qname]) + elsif obj.is_a?(SOAPArray) && (type = @definedtypes[type_qname]) soap_obj = obj contenttype = type.child_type mark_marshalled_obj(obj, soap_obj) @@ -92,6 +85,33 @@ private end end + def obj2type(obj, type) + if type.is_a?(::WSDL::XMLSchema::SimpleType) + simple2soap(obj, type) + else + complex2soap(obj, type) + end + end + + def simple2soap(obj, type) + o = base2soap(obj, TypeMap[type.base]) + if type.restriction.enumeration.empty? + STDERR.puts("#{type.name}: simpleType which is not enum type not supported.") + return o + end + type.check_lexical_format(obj) + o + end + + def complex2soap(obj, type) + case type.compoundtype + when :TYPE_STRUCT + struct2soap(obj, type.name, type) + when :TYPE_ARRAY + array2soap(obj, type.name, type) + end + end + def base2soap(obj, type) soap_obj = nil if type <= XSD::XSDString diff --git a/lib/soap/netHttpClient.rb b/lib/soap/netHttpClient.rb index 4505de815e..1e9d71c5a3 100644 --- a/lib/soap/netHttpClient.rb +++ b/lib/soap/netHttpClient.rb @@ -34,6 +34,10 @@ class NetHttpClient @session_manager = SessionManager.new @no_proxy = nil end + + def test_loopback_response + raise NotImplementedError.new("not supported for now") + end def proxy=(proxy_str) if proxy_str.nil? @@ -54,7 +58,11 @@ class NetHttpClient end def set_cookie_store(filename) - # ignored. + raise NotImplementedError.new + end + + def save_cookie_store(filename) + raise NotImplementedError.new end def reset(url) @@ -70,8 +78,8 @@ class NetHttpClient extra = header.dup extra['User-Agent'] = @agent if @agent res = start(url) { |http| - http.post(url.request_uri, req_body, extra) - } + http.post(url.request_uri, req_body, extra) + } Response.new(res) end diff --git a/lib/soap/parser.rb b/lib/soap/parser.rb index 457d681d30..14704a6d9b 100644 --- a/lib/soap/parser.rb +++ b/lib/soap/parser.rb @@ -117,7 +117,13 @@ public encodingstyle = find_encodingstyle(ns, attrs) # Children's encodingstyle is derived from its parent. - encodingstyle ||= parent_encodingstyle || @default_encodingstyle + if encodingstyle.nil? + if parent.node.is_a?(SOAPHeader) + encodingstyle = LiteralNamespace + else + encodingstyle = parent_encodingstyle || @default_encodingstyle + end + end node = decode_tag(ns, name, attrs, parent, encodingstyle) diff --git a/lib/soap/property.rb b/lib/soap/property.rb index 079c294a77..113cc64f3c 100644 --- a/lib/soap/property.rb +++ b/lib/soap/property.rb @@ -34,22 +34,24 @@ module SOAP class Property include Enumerable - def self.load(stream) - new.load(stream) + module Util + def const_from_name(fqname) + fqname.split("::").inject(Kernel) { |klass, name| klass.const_get(name) } + end + module_function :const_from_name + + def require_from_name(fqname) + require File.join(fqname.split("::").collect { |ele| ele.downcase }) + end + module_function :require_from_name end - def self.open(filename) - File.open(filename) { |f| load(f) } + def self.load(stream) + new.load(stream) end - # find property from $:. def self.loadproperty(propname) - $:.each do |path| - if File.file?(file = File.join(path, propname)) - return open(file) - end - end - nil + new.loadproperty(propname) end def initialize @@ -87,6 +89,17 @@ class Property self end + # find property from $:. + def loadproperty(propname) + return loadpropertyfile(propname) if File.file?(propname) + $:.each do |path| + if File.file?(file = File.join(path, propname)) + return loadpropertyfile(file) + end + end + nil + end + # name: a Symbol, String or an Array def [](name) referent(name_to_a(name)) @@ -95,10 +108,10 @@ class Property # name: a Symbol, String or an Array # value: an Object def []=(name, value) - hooks = assign(name_to_a(name), value) - normalized_name = normalize_name(name) + name_pair = name_to_a(name).freeze + hooks = assign(name_pair, value) hooks.each do |hook| - hook.call(normalized_name, value) + hook.call(name_pair, value) end value end @@ -109,13 +122,15 @@ class Property self[generate_new_key] = value end - # name: a Symbol, String or an Array. nil means hook to the root + # name: a Symbol, String or an Array; nil means hook to the root + # cascade: true/false; for cascading hook of sub key # hook: block which will be called with 2 args, name and value - def add_hook(name = nil, &hook) - if name.nil? - assign_self_hook(&hook) + def add_hook(name = nil, cascade = false, &hook) + if name == nil or name == true or name == false + cascade = name + assign_self_hook(cascade, &hook) else - assign_hook(name_to_a(name), &hook) + assign_hook(name_to_a(name), cascade, &hook) end end @@ -192,14 +207,18 @@ protected @store[key] = value end - def local_hook(key) - @self_hook + (@hook[key] || NO_HOOK) + def local_hook(key, direct) + hooks = [] + (@self_hook + (@hook[key] || NO_HOOK)).each do |hook, cascade| + hooks << hook if direct or cascade + end + hooks end - def local_assign_hook(key, &hook) + def local_assign_hook(key, cascade, &hook) check_lock(key) @store[key] ||= nil - (@hook[key] ||= []) << hook + (@hook[key] ||= []) << [hook, cascade] end private @@ -217,23 +236,23 @@ private hook = NO_HOOK ary[0..-2].each do |name| key = to_key(name) - hook += ref.local_hook(key) + hook += ref.local_hook(key, false) ref = ref.deref_key(key) end last_key = to_key(ary.last) ref.local_assign(last_key, value) - hook + ref.local_hook(last_key) + hook + ref.local_hook(last_key, true) end - def assign_hook(ary, &hook) + def assign_hook(ary, cascade, &hook) ary[0..-2].inject(self) { |ref, name| ref.deref_key(to_key(name)) - }.local_assign_hook(to_key(ary.last), &hook) + }.local_assign_hook(to_key(ary.last), cascade, &hook) end - def assign_self_hook(&hook) + def assign_self_hook(cascade, &hook) check_lock(nil) - @self_hook << hook + @self_hook << [hook, cascade] end def each_key @@ -267,10 +286,6 @@ private end end - def normalize_name(name) - name_to_a(name).collect { |key| to_key(key) }.join('.') - end - def to_key(name) name.to_s.downcase end @@ -286,6 +301,13 @@ private def key_max (@store.keys.max { |l, r| l.to_s.to_i <=> r.to_s.to_i }).to_s.to_i end + + def loadpropertyfile(file) + puts "find property at #{file}" if $DEBUG + File.open(file) do |f| + load(f) + end + end end diff --git a/lib/soap/rpc/cgistub.rb b/lib/soap/rpc/cgistub.rb index 4fc8b98cee..55437bac59 100644 --- a/lib/soap/rpc/cgistub.rb +++ b/lib/soap/rpc/cgistub.rb @@ -1,5 +1,5 @@ # SOAP4R - CGI stub library -# Copyright (C) 2001, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2001, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -94,15 +94,21 @@ class CGIStub < Logger::Application on_init end - def add_servant(obj, namespace = @default_namespace, soapaction = nil) + def add_rpc_servant(obj, namespace = @default_namespace, soapaction = nil) RPC.defined_methods(obj).each do |name| qname = XSD::QName.new(namespace, name) param_size = obj.method(name).arity.abs - params = (1..param_size).collect { |i| "p#{ i }" } + params = (1..param_size).collect { |i| "p#{i}" } param_def = SOAP::RPC::SOAPMethod.create_param_def(params) @router.add_method(obj, qname, soapaction, name, param_def) end end + alias add_servant add_rpc_servant + + def add_rpc_headerhandler(obj) + @router.headerhandler << obj + end + alias add_headerhandler add_rpc_headerhandler def on_init # Override this method in derived class to call 'add_method' to add methods. diff --git a/lib/soap/rpc/driver.rb b/lib/soap/rpc/driver.rb index 655174cf33..0e59dde9be 100644 --- a/lib/soap/rpc/driver.rb +++ b/lib/soap/rpc/driver.rb @@ -1,5 +1,5 @@ # SOAP4R - SOAP RPC driver -# Copyright (C) 2000, 2001, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2000, 2001, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -13,6 +13,7 @@ require 'soap/rpc/proxy' require 'soap/rpc/element' require 'soap/streamHandler' require 'soap/property' +require 'soap/header/handlerset' module SOAP @@ -41,6 +42,8 @@ class Driver end __attr_proxy :options + __attr_proxy :headerhandler + __attr_proxy :test_loopback_response __attr_proxy :endpoint_url, true __attr_proxy :mapping_registry, true __attr_proxy :soapaction, true @@ -84,6 +87,12 @@ class Driver @proxy = @servant.proxy end + def loadproperty(propertyname) + unless options.loadproperty(propertyname) + raise LoadError.new("No such property to load -- #{propertyname}") + end + end + def inspect "#<#{self.class}:#{@servant.streamhandler.inspect}>" end @@ -130,6 +139,7 @@ private class Servant__ attr_reader :options attr_reader :streamhandler + attr_reader :headerhandler attr_reader :proxy def initialize(host, endpoint_url, namespace) @@ -141,6 +151,7 @@ private @options = setup_options @streamhandler = HTTPPostStreamHandler.new(endpoint_url, @options["protocol.http"] ||= ::SOAP::Property.new) + @headerhandler = Header::HandlerSet.new @proxy = Proxy.new(@streamhandler, @soapaction) @proxy.allow_unqualified_element = true end @@ -178,6 +189,10 @@ private @proxy.default_encodingstyle = encodingstyle end + def test_loopback_response + @streamhandler.test_loopback_response + end + def invoke(headers, body) set_wiredump_file_base(body.elename.name) env = @proxy.invoke(headers, body) @@ -192,19 +207,19 @@ private set_wiredump_file_base(name) # Convert parameters: params array => SOAPArray => members array params = Mapping.obj2soap(params, @mapping_registry).to_a - env = @proxy.call(nil, name, *params) + env = @proxy.call(call_headers, name, *params) raise EmptyResponseError.new("Empty response.") unless env - header, body = env.header, env.body + receive_headers(env.header) begin - @proxy.check_fault(body) + @proxy.check_fault(env.body) rescue SOAP::FaultError => e Mapping.fault2exception(e) end - ret = body.response ? - Mapping.soap2obj(body.response, @mapping_registry) : nil - if body.outparams - outparams = body.outparams.collect { |outparam| + ret = env.body.response ? + Mapping.soap2obj(env.body.response, @mapping_registry) : nil + if env.body.outparams + outparams = env.body.outparams.collect { |outparam| Mapping.soap2obj(outparam) } return [ret].concat(outparams) @@ -233,10 +248,28 @@ private @servant.call(#{ name.dump }#{ callparam }) end EOS + @host.method(name) end private + def call_headers + headers = @headerhandler.on_outbound + if headers.empty? + nil + else + h = ::SOAP::SOAPHeader.new + headers.each do |header| + h.add(header.elename.name, header) + end + h + end + end + + def receive_headers(headers) + @headerhandler.on_inbound(headers) if headers + end + def set_wiredump_file_base(name) if @wiredump_file_base @streamhandler.wiredump_file_base = @wiredump_file_base + "_#{ name }" diff --git a/lib/soap/rpc/httpserver.rb b/lib/soap/rpc/httpserver.rb new file mode 100644 index 0000000000..7b1f961d9e --- /dev/null +++ b/lib/soap/rpc/httpserver.rb @@ -0,0 +1,105 @@ +# SOAP4R - WEBrick HTTP Server +# Copyright (C) 2003, 2004 by NAKAMURA, Hiroshi <nahi@ruby-lang.org>. + +# This program is copyrighted free software by NAKAMURA, Hiroshi. You can +# redistribute it and/or modify it under the same terms of Ruby's license; +# either the dual license version in 2003, or any later version. + + +require 'logger' +require 'soap/rpc/soaplet' +require 'soap/streamHandler' +require 'webrick' + + +module SOAP +module RPC + + +class HTTPServer < Logger::Application + attr_reader :server + attr_accessor :default_namespace + + def initialize(config) + super(config[:SOAPHTTPServerApplicationName] || self.class.name) + @default_namespace = config[:SOAPDefaultNamespace] + @webrick_config = config.dup + @webrick_config[:Logger] ||= @log + @server = nil + @soaplet = ::SOAP::RPC::SOAPlet.new + self.level = Logger::Severity::INFO + on_init + end + + def on_init + # define extra methods in derived class. + end + + def status + if @server + @server.status + else + nil + end + end + + def shutdown + @server.shutdown if @server + end + + def mapping_registry + @soaplet.app_scope_router.mapping_registry + end + + def mapping_registry=(mapping_registry) + @soaplet.app_scope_router.mapping_registry = mapping_registry + end + + def add_rpc_request_servant(factory, namespace = @default_namespace, + mapping_registry = nil) + @soaplet.add_rpc_request_servant(factory, namespace, mapping_registry) + end + + def add_rpc_servant(obj, namespace = @default_namespace) + @soaplet.add_rpc_servant(obj, namespace) + end + + def add_rpc_request_headerhandler(factory) + @soaplet.add_rpc_request_headerhandler(factory) + end + + def add_rpc_headerhandler(obj) + @soaplet.add_rpc_headerhandler(obj) + end + + def add_method(obj, name, *param) + add_method_as(obj, name, name, *param) + end + + def add_method_as(obj, name, name_as, *param) + qname = XSD::QName.new(@default_namespace, name_as) + soapaction = nil + method = obj.method(name) + param_def = if param.size == 1 and param[0].is_a?(Array) + param[0] + elsif param.empty? + ::SOAP::RPC::SOAPMethod.create_param_def( + (1..method.arity.abs).collect { |i| "p#{ i }" }) + else + SOAP::RPC::SOAPMethod.create_param_def(param) + end + @soaplet.app_scope_router.add_method(obj, qname, soapaction, name, param_def) + end + +private + + def run + @server = WEBrick::HTTPServer.new(@webrick_config) + @server.mount('/', @soaplet) + @server.start + end +end + + +end +end diff --git a/lib/soap/rpc/proxy.rb b/lib/soap/rpc/proxy.rb index 7d9dbe519e..355bf2e81a 100644 --- a/lib/soap/rpc/proxy.rb +++ b/lib/soap/rpc/proxy.rb @@ -1,5 +1,5 @@ # SOAP4R - RPC Proxy library. -# Copyright (C) 2000, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2000, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -112,9 +112,9 @@ public unmarshal(conn_data, opt) end - def call(headers, name, *values) + def call(req_header, name, *values) req = create_request(name, *values) - invoke(headers, req.method, req.method.soapaction || @soapaction) + invoke(req_header, req.method, req.method.soapaction || @soapaction) end def check_fault(body) diff --git a/lib/soap/rpc/router.rb b/lib/soap/rpc/router.rb index 12622c72b1..9d8d1c8da6 100644 --- a/lib/soap/rpc/router.rb +++ b/lib/soap/rpc/router.rb @@ -13,6 +13,7 @@ require 'soap/rpc/rpc' require 'soap/rpc/element' require 'soap/streamHandler' require 'soap/mimemessage' +require 'soap/header/handlerset' module SOAP @@ -26,6 +27,7 @@ class Router attr_accessor :allow_unqualified_element attr_accessor :default_encodingstyle attr_accessor :mapping_registry + attr_reader :headerhandler def initialize(actor) @actor = actor @@ -35,6 +37,7 @@ class Router @allow_unqualified_element = false @default_encodingstyle = nil @mapping_registry = nil + @headerhandler = Header::HandlerSet.new end def add_method(receiver, qname, soapaction, name, param_def) @@ -44,12 +47,6 @@ class Router @method[fqname] = RPC::SOAPMethodRequest.new(qname, param_def, soapaction) end - def add_header_handler - raise NotImplementedError.new - end - - # Routing... - #def route(soap_string, charset = nil) def route(conn_data) soap_response = nil begin @@ -57,7 +54,7 @@ class Router if env.nil? raise ArgumentError.new("Illegal SOAP marshal format.") end - # So far, header is omitted... + receive_headers(env.header) soap_request = env.body.request unless soap_request.is_a?(SOAPStruct) raise RPCRoutingError.new("Not an RPC style.") @@ -70,7 +67,7 @@ class Router opt = options opt[:external_content] = nil - header = SOAPHeader.new + header = call_headers body = SOAPBody.new(soap_response) env = SOAPEnvelope.new(header, body) response_string = Processor.marshal(env, opt) @@ -114,13 +111,30 @@ class Router private + def call_headers + headers = @headerhandler.on_outbound + if headers.empty? + nil + else + h = ::SOAP::SOAPHeader.new + headers.each do |header| + h.add(header.elename.name, header) + end + h + end + end + + def receive_headers(headers) + @headerhandler.on_inbound(headers) if headers + end + def unmarshal(conn_data) opt = options contenttype = conn_data.receive_contenttype if /#{MIMEMessage::MultipartContentType}/i =~ contenttype opt[:external_content] = {} mime = MIMEMessage.parse("Content-Type: " + contenttype, - conn_data.receive_string) + conn_data.receive_string) mime.parts.each do |part| value = Attachment.new(part.content) value.contentid = part.contentid diff --git a/lib/soap/rpc/soaplet.rb b/lib/soap/rpc/soaplet.rb index 7cb5e32375..0c1427acf5 100644 --- a/lib/soap/rpc/soaplet.rb +++ b/lib/soap/rpc/soaplet.rb @@ -1,5 +1,5 @@ # SOAP4R - SOAP handler servlet for WEBrick -# Copyright (C) 2001, 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2001, 2002, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -22,20 +22,28 @@ public def initialize @router_map = {} @app_scope_router = ::SOAP::RPC::Router.new(self.class.name) + @headerhandlerfactory = [] + @app_scope_headerhandler = nil end - # Add servant klass whose object has request scope. A servant object is - # instantiated for each request. + # Add servant factory whose object has request scope. A servant object is + # instanciated for each request. # - # Bare in mind that servant klasses are distinguished by HTTP SOAPAction + # Bear in mind that servant factories are distinguished by HTTP SOAPAction # header in request. Client which calls request-scoped servant must have a - # SOAPAction header which is a namespace of the servant klass. + # SOAPAction header which is a namespace of the servant factory. # I mean, use Driver#add_method_with_soapaction instead of Driver#add_method # at client side. # - def add_rpc_request_servant(klass, namespace, mapping_registry = nil) - router = RequestRouter.new(klass, namespace, mapping_registry) - add_router(namespace, router) + # A factory must respond to :create. + # + def add_rpc_request_servant(factory, namespace, mapping_registry = nil) + unless factory.respond_to?(:create) + raise TypeError.new("factory must respond to 'create'") + end + router = setup_request_router(namespace) + router.factory = factory + router.mapping_registry = mapping_registry end # Add servant object which has application scope. @@ -46,6 +54,17 @@ public end alias add_servant add_rpc_servant + def add_rpc_request_headerhandler(factory) + unless factory.respond_to?(:create) + raise TypeError.new("factory must respond to 'create'") + end + @headerhandlerfactory << factory + end + + def add_rpc_headerhandler(obj) + @app_scope_headerhandler = obj + end + alias add_headerhandler add_rpc_headerhandler ### ## Servlet interfaces for WEBrick. @@ -67,21 +86,23 @@ public def do_POST(req, res) namespace = parse_soapaction(req.meta_vars['HTTP_SOAPACTION']) router = lookup_router(namespace) - begin - conn_data = ::SOAP::StreamHandler::ConnectionData.new - conn_data.receive_string = req.body - conn_data.receive_contenttype = req['content-type'] - conn_data = router.route(conn_data) - if conn_data.is_fault + with_headerhandler(router) do |router| + begin + conn_data = ::SOAP::StreamHandler::ConnectionData.new + conn_data.receive_string = req.body + conn_data.receive_contenttype = req['content-type'] + conn_data = router.route(conn_data) + if conn_data.is_fault + res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR + end + res.body = conn_data.send_string + res['content-type'] = conn_data.send_contenttype + rescue Exception => e + conn_data = router.create_fault_response(e) res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR + res.body = conn_data.send_string + res['content-type'] = conn_data.send_contenttype || "text/xml" end - res.body = conn_data.send_string - res['content-type'] = conn_data.send_contenttype - rescue Exception => e - conn_data = router.create_fault_response(e) - res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR - res.body = conn_data.send_string - res['content-type'] = conn_data.send_contenttype || "text/xml" end if res.body.is_a?(IO) @@ -92,17 +113,16 @@ public private class RequestRouter < ::SOAP::RPC::Router - def initialize(klass, namespace, mapping_registry = nil) + attr_accessor :factory + + def initialize(namespace = nil) super(namespace) - if mapping_registry - self.mapping_registry = mapping_registry - end - @klass = klass @namespace = namespace + @factory = nil end def route(soap_string) - obj = @klass.new + obj = @factory.create namespace = self.actor router = ::SOAP::RPC::Router.new(@namespace) SOAPlet.add_servant_to_router(router, obj, namespace) @@ -110,6 +130,12 @@ private end end + def setup_request_router(namespace) + router = @router_map[namespace] || RequestRouter.new(namespace) + add_router(namespace, router) + router + end + def add_router(namespace, router) @router_map[namespace] = router end @@ -132,11 +158,29 @@ private end end + def with_headerhandler(router) + if @app_scope_headerhandler and + !router.headerhandler.include?(@app_scope_headerhandler) + router.headerhandler.add(@app_scope_headerhandler) + end + handlers = @headerhandlerfactory.collect { |f| f.create } + begin + handlers.each { |h| router.headerhandler.add(h) } + yield(router) + ensure + handlers.each { |h| router.headerhandler.delete(h) } + end + end + class << self public def add_servant_to_router(router, obj, namespace) ::SOAP::RPC.defined_methods(obj).each do |name| - add_servant_method_to_router(router, obj, namespace, name) + begin + add_servant_method_to_router(router, obj, namespace, name) + rescue SOAP::RPC::MethodDefinitionError => e + p e if $DEBUG + end end end @@ -145,7 +189,7 @@ private soapaction = nil method = obj.method(name) param_def = ::SOAP::RPC::SOAPMethod.create_param_def( - (1..method.arity.abs).collect { |i| "p#{ i }" }) + (1..method.arity.abs).collect { |i| "p#{ i }" }) router.add_method(obj, qname, soapaction, name, param_def) end end diff --git a/lib/soap/rpc/standaloneServer.rb b/lib/soap/rpc/standaloneServer.rb index 42a566e088..080343ba33 100644 --- a/lib/soap/rpc/standaloneServer.rb +++ b/lib/soap/rpc/standaloneServer.rb @@ -6,111 +6,35 @@ # either the dual license version in 2003, or any later version. -require 'logger' -require 'soap/rpc/soaplet' -require 'soap/streamHandler' - -# require 'webrick' -require 'webrick/compat.rb' -require 'webrick/version.rb' -require 'webrick/config.rb' -require 'webrick/log.rb' -require 'webrick/server.rb' -require 'webrick/utils.rb' -require 'webrick/accesslog' -# require 'webrick/htmlutils.rb' -require 'webrick/httputils.rb' -# require 'webrick/cookie.rb' -require 'webrick/httpversion.rb' -require 'webrick/httpstatus.rb' -require 'webrick/httprequest.rb' -require 'webrick/httpresponse.rb' -require 'webrick/httpserver.rb' -# require 'webrick/httpservlet.rb' -# require 'webrick/httpauth.rb' +require 'soap/rpc/httpserver' module SOAP module RPC -class StandaloneServer < Logger::Application - attr_reader :server - - def initialize(app_name, namespace, host = "0.0.0.0", port = 8080) - super(app_name) - self.level = Logger::Severity::INFO - @namespace = namespace +class StandaloneServer < HTTPServer + def initialize(appname, default_namespace, host = "0.0.0.0", port = 8080) + @appname = appname + @default_namespace = default_namespace @host = host @port = port - @server = nil - @soaplet = ::SOAP::RPC::SOAPlet.new - on_init - end - - def on_init - # define extra methods in derived class. - end - - def status - if @server - @server.status - else - nil - end - end - - def shutdown - @server.shutdown - end - - def add_rpc_request_servant(klass, namespace = @namespace, mapping_registry = nil) - @soaplet.add_rpc_request_servant(klass, namespace, mapping_registry) + super(create_config) end - def add_rpc_servant(obj, namespace = @namespace) - @soaplet.add_rpc_servant(obj, namespace) - end alias add_servant add_rpc_servant - - def mapping_registry - @soaplet.app_scope_router.mapping_registry - end - - def mapping_registry=(mapping_registry) - @soaplet.app_scope_router.mapping_registry = mapping_registry - end - - def add_method(obj, name, *param) - add_method_as(obj, name, name, *param) - end - - def add_method_as(obj, name, name_as, *param) - qname = XSD::QName.new(@namespace, name_as) - soapaction = nil - method = obj.method(name) - param_def = if param.size == 1 and param[0].is_a?(Array) - param[0] - elsif param.empty? - ::SOAP::RPC::SOAPMethod.create_param_def( - (1..method.arity.abs).collect { |i| "p#{ i }" }) - else - SOAP::RPC::SOAPMethod.create_param_def(param) - end - @soaplet.app_scope_router.add_method(obj, qname, soapaction, name, param_def) - end + alias add_headerhandler add_rpc_headerhandler private - def run - @server = WEBrick::HTTPServer.new( + def create_config + { :BindAddress => @host, - :Logger => @log, + :Port => @port, :AccessLog => [], - :Port => @port - ) - @server.mount('/', @soaplet) - @server.start + :SOAPDefaultNamespace => @default_namespace, + :SOAPHTTPServerApplicationName => @appname, + } end end diff --git a/lib/soap/soap.rb b/lib/soap/soap.rb index d00d89b05b..02b26e4246 100644 --- a/lib/soap/soap.rb +++ b/lib/soap/soap.rb @@ -1,5 +1,5 @@ # soap/soap.rb: SOAP4R - Base definitions. -# Copyright (C) 2000, 2001, 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2000-2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can # redistribute it and/or modify it under the same terms of Ruby's license; @@ -48,6 +48,7 @@ EleFaultStringName = XSD::QName.new(nil, EleFaultString) EleFaultActorName = XSD::QName.new(nil, EleFaultActor) EleFaultCodeName = XSD::QName.new(nil, EleFaultCode) EleFaultDetailName = XSD::QName.new(nil, EleFaultDetail) +AttrMustUnderstandName = XSD::QName.new(EnvelopeNamespace, AttrMustUnderstand) AttrEncodingStyleName = XSD::QName.new(EnvelopeNamespace, AttrEncodingStyle) AttrRootName = XSD::QName.new(EncodingNamespace, AttrRoot) AttrArrayTypeName = XSD::QName.new(EncodingNamespace, AttrArrayType) @@ -75,6 +76,8 @@ class ArrayStoreError < Error; end class RPCRoutingError < Error; end +class UnhandledMustUnderstandHeaderError < Error; end + class FaultError < Error attr_reader :faultcode attr_reader :faultstring diff --git a/lib/soap/streamHandler.rb b/lib/soap/streamHandler.rb index 0d08a00bd8..efadf21e07 100644 --- a/lib/soap/streamHandler.rb +++ b/lib/soap/streamHandler.rb @@ -83,6 +83,11 @@ public @options = options set_options @client.debug_dev = @wiredump_dev + @cookie_store = nil + end + + def test_loopback_response + @client.test_loopback_response end def inspect @@ -95,6 +100,7 @@ public def reset @client.reset(@endpoint_url) + @client.save_cookie_store if @cookie_store end private @@ -118,10 +124,6 @@ private @options.add_hook("cookie_store_file") do |key, value| set_cookie_store_file(value) end - set_ssl_config(@options["ssl_config"]) - @options.add_hook("ssl_config") do |key, value| - set_ssl_config(@options["ssl_config"]) - end @charset = @options["charset"] || XSD::Charset.charset_label($KCODE) @options.add_hook("charset") do |key, value| @charset = value @@ -131,12 +133,18 @@ private @wiredump_dev = value @client.debug_dev = @wiredump_dev end + ssl_config = @options["ssl_config"] ||= ::SOAP::Property.new + set_ssl_config(ssl_config) + ssl_config.add_hook(true) do |key, value| + set_ssl_config(ssl_config) + end basic_auth = @options["basic_auth"] ||= ::SOAP::Property.new set_basic_auth(basic_auth) basic_auth.add_hook do |key, value| set_basic_auth(basic_auth) end @options.lock(true) + ssl_config.unlock basic_auth.unlock end @@ -147,13 +155,62 @@ private end def set_cookie_store_file(value) - return unless value - raise NotImplementedError.new + @cookie_store = value + @client.set_cookie_store(@cookie_store) if @cookie_store + end + + def set_ssl_config(ssl_config) + ssl_config.each do |key, value| + cfg = @client.ssl_config + case key + when 'client_cert' + cfg.client_cert = cert_from_file(value) + when 'client_key' + cfg.client_key = key_from_file(value) + when 'client_ca' + cfg.client_ca = value + when 'ca_path' + cfg.set_trust_ca(value) + when 'ca_file' + cfg.set_trust_ca(value) + when 'crl' + cfg.set_crl(value) + when 'verify_mode' + cfg.verify_mode = ssl_config_int(value) + when 'verify_depth' + cfg.verify_depth = ssl_config_int(value) + when 'options' + cfg.options = value + when 'ciphers' + cfg.ciphers = value + when 'verify_callback' + cfg.verify_callback = value + when 'cert_store' + cfg.cert_store = value + else + raise ArgumentError.new("unknown ssl_config property #{key}") + end + end + end + + def ssl_config_int(value) + if value.nil? or value.empty? + nil + else + begin + Integer(value) + rescue ArgumentError + ::SOAP::Property::Util.const_from_name(value) + end + end + end + + def cert_from_file(filename) + OpenSSL::X509::Certificate.new(File.open(filename) { |f| f.read }) end - def set_ssl_config(value) - return unless value - raise NotImplementedError.new + def key_from_file(filename) + OpenSSL::PKey::RSA.new(File.open(filename) { |f| f.read }) end def send_post(conn_data, soapaction, charset) diff --git a/lib/soap/wsdlDriver.rb b/lib/soap/wsdlDriver.rb index 4b36bd08b8..af868ea886 100644 --- a/lib/soap/wsdlDriver.rb +++ b/lib/soap/wsdlDriver.rb @@ -18,6 +18,7 @@ require 'soap/mapping/wsdlRegistry' require 'soap/rpc/rpc' require 'soap/rpc/element' require 'soap/processor' +require 'soap/header/handlerset' require 'logger' @@ -92,6 +93,8 @@ class WSDLDriver end __attr_proxy :options + __attr_proxy :headerhandler + __attr_proxy :test_loopback_response __attr_proxy :endpoint_url, true __attr_proxy :mapping_registry, true # for RPC unmarshal __attr_proxy :wsdl_mapping_registry, true # for RPC marshal @@ -152,6 +155,7 @@ class WSDLDriver attr_reader :options attr_reader :streamhandler + attr_reader :headerhandler attr_reader :port attr_accessor :mapping_registry @@ -176,7 +180,7 @@ class WSDLDriver @mandatorycharset = nil @wsdl_elements = @wsdl.collect_elements - @wsdl_types = @wsdl.collect_complextypes + @wsdl_types = @wsdl.collect_complextypes + @wsdl.collect_simpletypes @rpc_decode_typemap = @wsdl_types + @wsdl.soap_rpc_complextypes(port.find_binding) @wsdl_mapping_registry = Mapping::WSDLRegistry.new(@rpc_decode_typemap) @@ -184,6 +188,7 @@ class WSDLDriver endpoint_url = @port.soap_address.location @streamhandler = HTTPPostStreamHandler.new(endpoint_url, @options["protocol.http"] ||= Property.new) + @headerhandler = Header::HandlerSet.new # Convert a map which key is QName, to a Hash which key is String. @operations = {} @port.inputoperation_map.each do |op_name, op_info| @@ -201,13 +206,17 @@ class WSDLDriver @streamhandler.reset end + def test_loopback_response + @streamhandler.test_loopback_response + end + def rpc_send(method_name, *params) log(INFO) { "call: calling method '#{ method_name }'." } log(DEBUG) { "call: parameters '#{ params.inspect }'." } op_info = @operations[method_name] method = create_method_struct(op_info, params) - req_header = nil + req_header = call_headers req_body = SOAPBody.new(method) req_env = SOAPEnvelope.new(req_header, req_body) @@ -220,10 +229,11 @@ class WSDLDriver opt = create_options opt[:decode_typemap] = @rpc_decode_typemap res_env = invoke(req_env, op_info, opt) + receive_headers(res_env.header) if res_env.body.fault - raise SOAP::FaultError.new(res_env.body.fault) + raise ::SOAP::FaultError.new(res_env.body.fault) end - rescue SOAP::FaultError => e + rescue ::SOAP::FaultError => e Mapping.fault2exception(e) end @@ -251,7 +261,7 @@ class WSDLDriver opt = create_options res_env = invoke(req_env, op_info, opt) if res_env.body.fault - raise SOAP::FaultError.new(res_env.body.fault) + raise ::SOAP::FaultError.new(res_env.body.fault) end res_body_obj = res_env.body.response ? Mapping.soap2obj(res_env.body.response, @mapping_registry) : nil @@ -260,6 +270,23 @@ class WSDLDriver private + def call_headers + headers = @headerhandler.on_outbound + if headers.empty? + nil + else + h = ::SOAP::SOAPHeader.new + headers.each do |header| + h.add(header.elename.name, header) + end + h + end + end + + def receive_headers(headers) + @headerhandler.on_inbound(headers) if headers + end + def create_method_struct(op_info, params) parts_names = op_info.bodyparts.collect { |part| part.name } obj = create_method_obj(parts_names, params) @@ -349,9 +376,9 @@ class WSDLDriver else header = SOAPHeader.new() op_info.headerparts.each do |part| - child = obj[part.elename.name] + child = Mapper.find_attribute(obj, part.name) ele = headeritem_from_obj(child, part.element || part.eletype) - header.add(ele) + header.add(part.name, ele) end header end @@ -383,7 +410,7 @@ class WSDLDriver else body = SOAPBody.new op_info.bodyparts.each do |part| - child = obj[part.elename.name] + child = Mapper.find_attribute(obj, part.name) ele = bodyitem_from_obj(child, part.element || part.type) body.add(ele.elename.name, ele) end @@ -461,6 +488,7 @@ class WSDLDriver opt end + class MappingError < StandardError; end class Mapper def initialize(elements, types) @elements = elements @@ -473,7 +501,7 @@ class WSDLDriver elsif type = @types[name] obj2type(obj, type) else - raise RuntimeError.new("Cannot find name #{name} in schema.") + raise MappingError.new("Cannot find name #{name} in schema.") end end @@ -481,6 +509,16 @@ class WSDLDriver raise NotImplementedError.new end + def Mapper.find_attribute(obj, attr_name) + if obj.respond_to?(attr_name) + obj.__send__(attr_name) + elsif obj.is_a?(Hash) + obj[attr_name] || obj[attr_name.intern] + else + obj.instance_eval("@#{ attr_name }") + end + end + private def _obj2ele(obj, ele) @@ -491,25 +529,47 @@ class WSDLDriver elsif type = TypeMap[ele.type] o = base2soap(obj, type) else - raise RuntimeError.new("Cannot find type #{ele.type}.") + raise MappingError.new("Cannot find type #{ele.type}.") end o.elename = ele.name elsif ele.local_complextype o = SOAPElement.new(ele.name) - ele.local_complextype.each_element do |child_name, child_ele| - o.add(_obj2ele(find_attribute(obj, child_name.name), child_ele)) + ele.local_complextype.each_element do |child_ele| + o.add(_obj2ele(Mapper.find_attribute(obj, child_ele.name.name), + child_ele)) end else - raise RuntimeError.new("Illegal schema?") + raise MappingError.new("Illegal schema?") end o end def obj2type(obj, type) - o = SOAPElement.new(type.name) - type.each_element do |child_name, child_ele| - o.add(_obj2ele(find_attribute(obj, child_name.name), child_ele)) - end + if type.is_a?(::WSDL::XMLSchema::SimpleType) + simple2soap(obj, type) + else + complex2soap(obj, type) + end + end + + def simple2soap(obj, type) + o = base2soap(obj, TypeMap[type.base]) + if type.restriction.enumeration.empty? + STDERR.puts("#{type.name}: simpleType which is not enum type not supported.") + return o + end + if type.restriction.enumeration.include?(o) + raise MappingError.new("#{o} is not allowed for #{type.name}") + end + o + end + + def complex2soap(obj, type) + o = SOAPElement.new(type.name) + type.each_element do |child_ele| + o.add(_obj2ele(Mapper.find_attribute(obj, child_ele.name.name), + child_ele)) + end o end @@ -521,22 +581,13 @@ class WSDLDriver soap_obj = nil if type <= XSD::XSDString soap_obj = type.new(XSD::Charset.is_ces(obj, $KCODE) ? - XSD::Charset.encoding_conv(obj, $KCODE, XSD::Charset.encoding) : obj) + XSD::Charset.encoding_conv(obj, $KCODE, XSD::Charset.encoding) : + obj) else soap_obj = type.new(obj) end soap_obj end - - def find_attribute(obj, attr_name) - if obj.respond_to?(attr_name) - obj.__send__(attr_name) - elsif obj.is_a?(Hash) - obj[attr_name] || obj[attr_name.intern] - else - obj.instance_eval("@#{ attr_name }") - end - end end end end |