summaryrefslogtreecommitdiff
path: root/lib/soap/rpc
diff options
context:
space:
mode:
authornahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-03 15:29:32 +0000
committernahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-03 15:29:32 +0000
commitab31bf0d4d44942e46d98d8848b788ac6df32a46 (patch)
tree303d964b43831f02c94f3970c9df2aca6e851edc /lib/soap/rpc
parentdf2066555d03b919be6f25fb7569abde8296d2f5 (diff)
* added files
* lib/soap/attachment.rb * lib/soap/header * lib/soap/mimemessage.rb * lib/soap/rpc/httpserver.rb * lib/wsdl/soap/cgiStubCreator.rb * lib/wsdl/soap/classDefCreator.rb * lib/wsdl/soap/classDefCreatorSupport.rb * lib/wsdl/soap/clientSkeltonCreator.rb * lib/wsdl/soap/driverCreator.rb * lib/wsdl/soap/mappingRegistryCreator.rb * lib/wsdl/soap/methodDefCreator.rb * lib/wsdl/soap/servantSkeltonCreator.rb * lib/wsdl/soap/standaloneServerStubCreator.rb * lib/wsdl/xmlSchema/enumeration.rb * lib/wsdl/xmlSchema/simpleRestriction.rb * lib/wsdl/xmlSchema/simpleType.rb * lib/xsd/codegen * lib/xsd/codegen.rb * sample/soap/authheader * sample/soap/raa2.4 * sample/soap/ssl * sample/soap/swa * sample/soap/whois.rb * sample/soap/calc/samplehttpd.conf * sample/soap/exchange/samplehttpd.conf * sample/soap/sampleStruct/samplehttpd.conf * sample/wsdl/raa2.4 * sample/wsdl/googleSearch/samplehttpd.conf * test/openssl/_test_ssl.rb * test/soap/header * test/soap/ssl * test/soap/struct * test/soap/swa * test/soap/wsdlDriver * test/wsdl/multiplefault.wsdl * test/wsdl/simpletype * test/wsdl/test_multiplefault.rb * modified files * lib/soap/baseData.rb * lib/soap/element.rb * lib/soap/generator.rb * lib/soap/marshal.rb * lib/soap/netHttpClient.rb * lib/soap/parser.rb * lib/soap/processor.rb * lib/soap/property.rb * lib/soap/soap.rb * lib/soap/streamHandler.rb * lib/soap/wsdlDriver.rb * lib/soap/encodingstyle/handler.rb * lib/soap/encodingstyle/literalHandler.rb * lib/soap/encodingstyle/soapHandler.rb * lib/soap/mapping/factory.rb * lib/soap/mapping/mapping.rb * lib/soap/mapping/registry.rb * lib/soap/mapping/rubytypeFactory.rb * lib/soap/mapping/wsdlRegistry.rb * lib/soap/rpc/cgistub.rb * lib/soap/rpc/driver.rb * lib/soap/rpc/element.rb * lib/soap/rpc/proxy.rb * lib/soap/rpc/router.rb * lib/soap/rpc/soaplet.rb * lib/soap/rpc/standaloneServer.rb * lib/wsdl/data.rb * lib/wsdl/definitions.rb * lib/wsdl/operation.rb * lib/wsdl/parser.rb * lib/wsdl/soap/definitions.rb * lib/wsdl/xmlSchema/complexContent.rb * lib/wsdl/xmlSchema/complexType.rb * lib/wsdl/xmlSchema/data.rb * lib/wsdl/xmlSchema/parser.rb * lib/wsdl/xmlSchema/schema.rb * lib/xsd/datatypes.rb * lib/xsd/qname.rb * sample/soap/calc/httpd.rb * sample/soap/exchange/httpd.rb * sample/soap/sampleStruct/httpd.rb * sample/soap/sampleStruct/server.rb * sample/wsdl/amazon/AmazonSearch.rb * sample/wsdl/amazon/AmazonSearchDriver.rb * sample/wsdl/googleSearch/httpd.rb * test/soap/test_basetype.rb * test/soap/test_property.rb * test/soap/test_streamhandler.rb * test/soap/calc/test_calc.rb * test/soap/calc/test_calc2.rb * test/soap/calc/test_calc_cgi.rb * test/soap/helloworld/test_helloworld.rb * test/wsdl/test_emptycomplextype.rb * test/wsdl/axisArray/test_axisarray.rb * test/wsdl/datetime/test_datetime.rb * test/wsdl/raa/test_raa.rb * test/xsd/test_xmlschemaparser.rb * test/xsd/test_xsd.rb * summary * add SOAP Header mustUnderstand support. * add HTTP client SSL configuration and Cookies support (works completely with http-access2). * add header handler for handling sending/receiving SOAP Header. * map Ruby's anonymous Struct to common SOAP Struct in SOAP Object Model. it caused error. * add WSDL simpleType support to restrict lexical value space. * add SOAP with Attachment support. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6567 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/soap/rpc')
-rw-r--r--lib/soap/rpc/cgistub.rb52
-rw-r--r--lib/soap/rpc/driver.rb57
-rw-r--r--lib/soap/rpc/element.rb5
-rw-r--r--lib/soap/rpc/proxy.rb56
-rw-r--r--lib/soap/rpc/router.rb110
-rw-r--r--lib/soap/rpc/soaplet.rb108
-rw-r--r--lib/soap/rpc/standaloneServer.rb102
7 files changed, 302 insertions, 188 deletions
diff --git a/lib/soap/rpc/cgistub.rb b/lib/soap/rpc/cgistub.rb
index 2377f343b5..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;
@@ -40,7 +40,6 @@ class CGIStub < Logger::Application
@method = ENV['REQUEST_METHOD']
@size = ENV['CONTENT_LENGTH'].to_i || 0
@contenttype = ENV['CONTENT_TYPE']
- @charset = nil
@soapaction = ENV['HTTP_SOAPAction']
@source = stream
@body = nil
@@ -48,7 +47,6 @@ class CGIStub < Logger::Application
def init
validate
- @charset = StreamHandler.parse_media_type(@contenttype)
@body = @source.read(@size)
self
end
@@ -61,8 +59,8 @@ class CGIStub < Logger::Application
@soapaction
end
- def charset
- @charset
+ def contenttype
+ @contenttype
end
def to_s
@@ -96,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.
@@ -142,8 +146,8 @@ class CGIStub < Logger::Application
@router.add_method(receiver, qname, nil, name, param_def)
end
- def route(request_string, charset)
- @router.route(request_string, charset)
+ def route(conn_data)
+ @router.route(conn_data)
end
def create_fault_response(e)
@@ -157,32 +161,30 @@ private
httpversion = WEBrick::HTTPVersion.new('1.0')
@response = WEBrick::HTTPResponse.new({:HTTPVersion => httpversion})
+ conn_data = nil
begin
log(INFO) { "Received a request from '#{ @remote_user }@#{ @remote_host }'." }
# SOAP request parsing.
@request = SOAPRequest.new.init
@response['Status'] = 200
- req_charset = @request.charset
- req_string = @request.dump
- log(DEBUG) { "XML Request: #{req_string}" }
- res_string, is_fault = route(req_string, req_charset)
- log(DEBUG) { "XML Response: #{res_string}" }
-
- @response['Cache-Control'] = 'private'
- if req_charset
- @response['content-type'] = "#{@mediatype}; charset=\"#{req_charset}\""
- else
- @response['content-type'] = @mediatype
- end
- if is_fault
+ conn_data = ::SOAP::StreamHandler::ConnectionData.new
+ conn_data.receive_string = @request.dump
+ conn_data.receive_contenttype = @request.contenttype
+ log(DEBUG) { "XML Request: #{conn_data.receive_string}" }
+ conn_data = route(conn_data)
+ log(DEBUG) { "XML Response: #{conn_data.send_string}" }
+ if conn_data.is_fault
@response['Status'] = 500
end
- @response.body = res_string
+ @response['Cache-Control'] = 'private'
+ @response.body = conn_data.send_string
+ @response['content-type'] = conn_data.send_contenttype
rescue Exception
- res_string = create_fault_response($!)
+ conn_data = create_fault_response($!)
@response['Cache-Control'] = 'private'
- @response['content-type'] = @mediatype
@response['Status'] = 500
+ @response.body = conn_data.send_string
+ @response['content-type'] = conn_data.send_contenttype || @mediatype
ensure
buf = ''
@response.send_response(buf)
diff --git a/lib/soap/rpc/driver.rb b/lib/soap/rpc/driver.rb
index dd433ca33f..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,27 +189,37 @@ 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)
- @proxy.invoke(headers, body)
+ env = @proxy.invoke(headers, body)
+ if env.nil?
+ return nil, nil
+ else
+ return env.header, env.body
+ end
end
def call(name, *params)
set_wiredump_file_base(name)
# Convert parameters: params array => SOAPArray => members array
params = Mapping.obj2soap(params, @mapping_registry).to_a
- header, body = @proxy.call(nil, name, *params)
- raise EmptyResponseError.new("Empty response.") unless body
+ env = @proxy.call(call_headers, name, *params)
+ raise EmptyResponseError.new("Empty response.") unless env
+ 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)
@@ -227,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/element.rb b/lib/soap/rpc/element.rb
index 395823ab00..8a2f319293 100644
--- a/lib/soap/rpc/element.rb
+++ b/lib/soap/rpc/element.rb
@@ -43,7 +43,7 @@ class SOAPBody < SOAPStruct
end
def void?
- root_node.nil? # || root_node.is_a?(SOAPNil)
+ root_node.nil?
end
def fault
@@ -113,6 +113,7 @@ class SOAPMethod < SOAPStruct
params.each do |param, data|
@inparam[param] = data
data.elename.name = param
+ data.parent = self
end
end
@@ -226,6 +227,8 @@ class SOAPMethodResponse < SOAPMethod
def retval=(retval)
@retval = retval
@retval.elename = @retval.elename.dup_name(@retval_name || 'return')
+ retval.parent = self
+ retval
end
def each
diff --git a/lib/soap/rpc/proxy.rb b/lib/soap/rpc/proxy.rb
index 5825a27138..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;
@@ -12,6 +12,7 @@ require 'soap/mapping'
require 'soap/rpc/rpc'
require 'soap/rpc/element'
require 'soap/streamHandler'
+require 'soap/mimemessage'
module SOAP
@@ -79,7 +80,6 @@ public
raise SOAP::RPC::MethodDefinitionError.new(
"Method: #{ name } not defined.")
end
-
Request.new(method, values)
end
@@ -91,21 +91,30 @@ public
req_body = SOAPBody.new(req_body)
end
opt = create_options
- send_string = Processor.marshal(req_header, req_body, opt)
- data = @streamhandler.send(send_string, soapaction)
- if data.receive_string.empty?
+ opt[:external_content] = nil
+ req_env = SOAPEnvelope.new(req_header, req_body)
+ send_string = Processor.marshal(req_env, opt)
+ conn_data = StreamHandler::ConnectionData.new(send_string)
+ if ext = opt[:external_content]
+ mime = MIMEMessage.new
+ ext.each do |k, v|
+ mime.add_attachment(v.data)
+ end
+ mime.add_part(conn_data.send_string + "\r\n")
+ mime.close
+ conn_data.send_string = mime.content_str
+ conn_data.send_contenttype = mime.headers['content-type'].str
+ end
+ conn_data = @streamhandler.send(conn_data, soapaction)
+ if conn_data.receive_string.empty?
return nil, nil
end
- opt = create_options
- opt[:charset] = @mandatorycharset ||
- StreamHandler.parse_media_type(data.receive_contenttype)
- res_header, res_body = Processor.unmarshal(data.receive_string, opt)
- return res_header, res_body
+ unmarshal(conn_data, opt)
end
- def call(headers, name, *values)
+ def call(req_header, name, *values)
req = create_request(name, *values)
- return invoke(headers, req.method, req.method.soapaction || @soapaction)
+ invoke(req_header, req.method, req.method.soapaction || @soapaction)
end
def check_fault(body)
@@ -116,6 +125,29 @@ public
private
+ def unmarshal(conn_data, opt)
+ contenttype = conn_data.receive_contenttype
+ if /#{MIMEMessage::MultipartContentType}/i =~ contenttype
+ opt[:external_content] = {}
+ mime = MIMEMessage.parse("Content-Type: " + contenttype,
+ conn_data.receive_string)
+ mime.parts.each do |part|
+ value = Attachment.new(part.content)
+ value.contentid = part.contentid
+ obj = SOAPAttachment.new(value)
+ opt[:external_content][value.contentid] = obj if value.contentid
+ end
+ opt[:charset] = @mandatorycharset ||
+ StreamHandler.parse_media_type(mime.root.headers['content-type'].str)
+ env = Processor.unmarshal(mime.root.content, opt)
+ else
+ opt[:charset] = @mandatorycharset ||
+ ::SOAP::StreamHandler.parse_media_type(contenttype)
+ env = Processor.unmarshal(conn_data.receive_string, opt)
+ end
+ env
+ end
+
def create_header(headers)
header = SOAPHeader.new()
headers.each do |content, mustunderstand, encodingstyle|
diff --git a/lib/soap/rpc/router.rb b/lib/soap/rpc/router.rb
index 527ec05768..9d8d1c8da6 100644
--- a/lib/soap/rpc/router.rb
+++ b/lib/soap/rpc/router.rb
@@ -11,6 +11,9 @@ require 'soap/processor'
require 'soap/mapping'
require 'soap/rpc/rpc'
require 'soap/rpc/element'
+require 'soap/streamHandler'
+require 'soap/mimemessage'
+require 'soap/header/handlerset'
module SOAP
@@ -24,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
@@ -33,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)
@@ -42,47 +47,112 @@ 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)
- opt = options
- opt[:charset] = charset
- is_fault = false
+ def route(conn_data)
+ soap_response = nil
begin
- header, body = Processor.unmarshal(soap_string, opt)
- # So far, header is omitted...
- soap_request = body.request
+ env = unmarshal(conn_data)
+ if env.nil?
+ raise ArgumentError.new("Illegal SOAP marshal format.")
+ end
+ receive_headers(env.header)
+ soap_request = env.body.request
unless soap_request.is_a?(SOAPStruct)
raise RPCRoutingError.new("Not an RPC style.")
end
soap_response = dispatch(soap_request)
rescue Exception
soap_response = fault($!)
- is_fault = true
+ conn_data.is_fault = true
end
- header = SOAPHeader.new
+ opt = options
+ opt[:external_content] = nil
+ header = call_headers
body = SOAPBody.new(soap_response)
- response_string = Processor.marshal(header, body, opt)
-
- return response_string, is_fault
+ env = SOAPEnvelope.new(header, body)
+ response_string = Processor.marshal(env, opt)
+ conn_data.send_string = response_string
+ if ext = opt[:external_content]
+ mime = MIMEMessage.new
+ ext.each do |k, v|
+ mime.add_attachment(v.data)
+ end
+ mime.add_part(conn_data.send_string + "\r\n")
+ mime.close
+ conn_data.send_string = mime.content_str
+ conn_data.send_contenttype = mime.headers['content-type'].str
+ end
+ conn_data
end
# Create fault response string.
def create_fault_response(e, charset = nil)
header = SOAPHeader.new
- soap_response = fault(e)
- body = SOAPBody.new(soap_response)
+ body = SOAPBody.new(fault(e))
+ env = SOAPEnvelope.new(header, body)
opt = options
+ opt[:external_content] = nil
opt[:charset] = charset
- Processor.marshal(header, body, opt)
+ response_string = Processor.marshal(env, opt)
+ conn_data = StreamHandler::ConnectionData.new(response_string)
+ conn_data.is_fault = true
+ if ext = opt[:external_content]
+ mime = MIMEMessage.new
+ ext.each do |k, v|
+ mime.add_attachment(v.data)
+ end
+ mime.add_part(conn_data.send_string + "\r\n")
+ mime.close
+ conn_data.send_string = mime.content_str
+ conn_data.send_contenttype = mime.headers['content-type'].str
+ end
+ conn_data
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 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)
+ mime.parts.each do |part|
+ value = Attachment.new(part.content)
+ value.contentid = part.contentid
+ obj = SOAPAttachment.new(value)
+ opt[:external_content][value.contentid] = obj if value.contentid
+ end
+ opt[:charset] =
+ StreamHandler.parse_media_type(mime.root.headers['content-type'].str)
+ env = Processor.unmarshal(mime.root.content, opt)
+ else
+ opt[:charset] = ::SOAP::StreamHandler.parse_media_type(contenttype)
+ env = Processor.unmarshal(conn_data.receive_string, opt)
+ end
+ charset = opt[:charset]
+ conn_data.send_contenttype = "text/xml; charset=\"#{charset}\""
+ env
+ end
+
# Create new response.
def create_response(qname, result)
name = fqname(qname)
diff --git a/lib/soap/rpc/soaplet.rb b/lib/soap/rpc/soaplet.rb
index ec172fd85e..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,42 +86,43 @@ public
def do_POST(req, res)
namespace = parse_soapaction(req.meta_vars['HTTP_SOAPACTION'])
router = lookup_router(namespace)
-
- is_fault = false
-
- charset = ::SOAP::StreamHandler.parse_media_type(req['content-type'])
- begin
- response_stream, is_fault = router.route(req.body, charset)
- rescue Exception => e
- response_stream = router.create_fault_response(e)
- is_fault = true
+ 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
end
- res.body = response_stream
- res['content-type'] = "text/xml; charset=\"#{charset}\""
- if response_stream.is_a?(IO)
+ if res.body.is_a?(IO)
res.chunked = true
end
-
- if is_fault
- res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR
- end
end
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