1
# SOAP4R - RPC Routing library
2
# Copyright (C) 2001, 2002, 2004, 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
4
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
# redistribute it and/or modify it under the same terms of Ruby's license;
6
# either the dual license version in 2003, or any later version.
10
require 'soap/processor'
11
require 'soap/mapping'
12
require 'soap/mapping/wsdlliteralregistry'
13
require 'soap/rpc/rpc'
14
require 'soap/rpc/element'
15
require 'soap/streamHandler'
16
require 'soap/mimemessage'
17
require 'soap/header/handlerset'
28
attr_accessor :mapping_registry
29
attr_accessor :literal_mapping_registry
30
attr_accessor :generate_explicit_type
31
attr_accessor :external_ces
35
@mapping_registry = nil
36
@headerhandler = Header::HandlerSet.new
37
@literal_mapping_registry = ::SOAP::Mapping::WSDLLiteralRegistry.new
38
@generate_explicit_type = true
40
@operation_by_soapaction = {}
41
@operation_by_qname = {}
42
@headerhandlerfactory = []
46
## header handler interface
48
def add_request_headerhandler(factory)
49
unless factory.respond_to?(:create)
50
raise TypeError.new("factory must respond to 'create'")
52
@headerhandlerfactory << factory
55
def add_headerhandler(handler)
56
@headerhandler.add(handler)
60
## servant definition interface
62
def add_rpc_request_servant(factory, namespace)
63
unless factory.respond_to?(:create)
64
raise TypeError.new("factory must respond to 'create'")
66
obj = factory.create # a dummy instance for introspection
67
::SOAP::RPC.defined_methods(obj).each do |name|
69
qname = XSD::QName.new(namespace, name)
70
param_def = ::SOAP::RPC::SOAPMethod.derive_rpc_param_def(obj, name)
71
opt = create_styleuse_option(:rpc, :encoded)
72
add_rpc_request_operation(factory, qname, nil, name, param_def, opt)
73
rescue SOAP::RPC::MethodDefinitionError => e
79
def add_rpc_servant(obj, namespace)
80
::SOAP::RPC.defined_methods(obj).each do |name|
82
qname = XSD::QName.new(namespace, name)
83
param_def = ::SOAP::RPC::SOAPMethod.derive_rpc_param_def(obj, name)
84
opt = create_styleuse_option(:rpc, :encoded)
85
add_rpc_operation(obj, qname, nil, name, param_def, opt)
86
rescue SOAP::RPC::MethodDefinitionError => e
91
alias add_servant add_rpc_servant
94
## operation definition interface
96
def add_rpc_operation(receiver, qname, soapaction, name, param_def, opt = {})
97
ensure_styleuse_option(opt, :rpc, :encoded)
98
opt[:request_qname] = qname
99
op = ApplicationScopeOperation.new(soapaction, receiver, name, param_def,
101
if opt[:request_style] != :rpc
102
raise RPCRoutingError.new("illegal request_style given")
104
assign_operation(soapaction, qname, op)
106
alias add_method add_rpc_operation
107
alias add_rpc_method add_rpc_operation
109
def add_rpc_request_operation(factory, qname, soapaction, name, param_def, opt = {})
110
ensure_styleuse_option(opt, :rpc, :encoded)
111
opt[:request_qname] = qname
112
op = RequestScopeOperation.new(soapaction, factory, name, param_def, opt)
113
if opt[:request_style] != :rpc
114
raise RPCRoutingError.new("illegal request_style given")
116
assign_operation(soapaction, qname, op)
119
def add_document_operation(receiver, soapaction, name, param_def, opt = {})
121
# adopt workaround for doc/lit wrapper method
122
# (you should consider to simply use rpc/lit service)
125
# raise RPCRoutingError.new("soapaction is a must for document method")
127
ensure_styleuse_option(opt, :document, :literal)
128
op = ApplicationScopeOperation.new(soapaction, receiver, name, param_def,
130
if opt[:request_style] != :document
131
raise RPCRoutingError.new("illegal request_style given")
133
assign_operation(soapaction, first_input_part_qname(param_def), op)
135
alias add_document_method add_document_operation
137
def add_document_request_operation(factory, soapaction, name, param_def, opt = {})
139
# adopt workaround for doc/lit wrapper method
140
# (you should consider to simply use rpc/lit service)
143
# raise RPCRoutingError.new("soapaction is a must for document method")
145
ensure_styleuse_option(opt, :document, :literal)
146
op = RequestScopeOperation.new(soapaction, receiver, name, param_def, opt)
147
if opt[:request_style] != :document
148
raise RPCRoutingError.new("illegal request_style given")
150
assign_operation(soapaction, first_input_part_qname(param_def), op)
154
# we cannot set request_default_encodingsyle before parsing the content.
155
env = unmarshal(conn_data)
157
raise ArgumentError.new("illegal SOAP marshal format")
159
op = lookup_operation(conn_data.soapaction, env.body)
160
headerhandler = @headerhandler.dup
161
@headerhandlerfactory.each do |f|
162
headerhandler.add(f.create)
164
receive_headers(headerhandler, env.header)
165
soap_response = default_encodingstyle = nil
168
op.call(env.body, @mapping_registry, @literal_mapping_registry,
170
default_encodingstyle = op.response_default_encodingstyle
172
soap_response = fault($!)
173
default_encodingstyle = nil
175
conn_data.is_fault = true if soap_response.is_a?(SOAPFault)
176
header = call_headers(headerhandler)
177
body = SOAPBody.new(soap_response)
178
env = SOAPEnvelope.new(header, body)
179
marshal(conn_data, env, default_encodingstyle)
182
# Create fault response string.
183
def create_fault_response(e)
184
env = SOAPEnvelope.new(SOAPHeader.new, SOAPBody.new(fault(e)))
186
opt[:external_content] = nil
187
response_string = Processor.marshal(env, opt)
188
conn_data = StreamHandler::ConnectionData.new(response_string)
189
conn_data.is_fault = true
190
if ext = opt[:external_content]
191
mimeize(conn_data, ext)
198
def first_input_part_qname(param_def)
199
param_def.each do |inout, paramname, typeinfo|
200
if inout == SOAPMethod::IN
201
klass, nsdef, namedef = typeinfo
202
return XSD::QName.new(nsdef, namedef)
208
def create_styleuse_option(style, use)
210
opt[:request_style] = opt[:response_style] = style
211
opt[:request_use] = opt[:response_use] = use
215
def ensure_styleuse_option(opt, style, use)
216
opt[:request_style] ||= style
217
opt[:response_style] ||= style
218
opt[:request_use] ||= use
219
opt[:response_use] ||= use
222
def assign_operation(soapaction, qname, op)
224
if soapaction and !soapaction.empty?
225
@operation_by_soapaction[soapaction] = op
229
@operation_by_qname[qname] = op
233
raise RPCRoutingError.new("cannot assign operation")
237
def lookup_operation(soapaction, body)
238
if op = @operation_by_soapaction[soapaction]
241
qname = body.root_node.elename
242
if op = @operation_by_qname[qname]
246
raise RPCRoutingError.new(
247
"operation: #{soapaction} #{qname} not supported")
249
raise RPCRoutingError.new("operation: #{qname} not supported")
253
def call_headers(headerhandler)
254
headers = headerhandler.on_outbound
258
h = ::SOAP::SOAPHeader.new
259
headers.each do |header|
260
h.add(header.elename.name, header)
266
def receive_headers(headerhandler, headers)
267
headerhandler.on_inbound(headers) if headers
270
def unmarshal(conn_data)
272
contenttype = conn_data.receive_contenttype
273
if /#{MIMEMessage::MultipartContentType}/i =~ contenttype
274
opt[:external_content] = {}
275
mime = MIMEMessage.parse("Content-Type: " + contenttype,
276
conn_data.receive_string)
277
mime.parts.each do |part|
278
value = Attachment.new(part.content)
279
value.contentid = part.contentid
280
obj = SOAPAttachment.new(value)
281
opt[:external_content][value.contentid] = obj if value.contentid
284
StreamHandler.parse_media_type(mime.root.headers['content-type'].str)
285
env = Processor.unmarshal(mime.root.content, opt)
287
opt[:charset] = ::SOAP::StreamHandler.parse_media_type(contenttype)
288
env = Processor.unmarshal(conn_data.receive_string, opt)
290
charset = opt[:charset]
291
conn_data.send_contenttype = "text/xml; charset=\"#{charset}\""
295
def marshal(conn_data, env, default_encodingstyle = nil)
297
opt[:external_content] = nil
298
opt[:default_encodingstyle] = default_encodingstyle
299
opt[:generate_explicit_type] = @generate_explicit_type
300
response_string = Processor.marshal(env, opt)
301
conn_data.send_string = response_string
302
if ext = opt[:external_content]
303
mimeize(conn_data, ext)
308
def mimeize(conn_data, ext)
309
mime = MIMEMessage.new
311
mime.add_attachment(v.data)
313
mime.add_part(conn_data.send_string + "\r\n")
315
conn_data.send_string = mime.content_str
316
conn_data.send_contenttype = mime.headers['content-type'].str
320
# Create fault response.
322
detail = Mapping::SOAPException.new(e)
324
SOAPString.new('Server'),
325
SOAPString.new(e.to_s),
326
SOAPString.new(@actor),
327
Mapping.obj2soap(detail, @mapping_registry))
330
def create_mapping_opt
331
{ :external_ces => @external_ces }
336
attr_reader :soapaction
337
attr_reader :request_style
338
attr_reader :response_style
339
attr_reader :request_use
340
attr_reader :response_use
342
def initialize(soapaction, name, param_def, opt)
343
@soapaction = soapaction
345
@request_style = opt[:request_style]
346
@response_style = opt[:response_style]
347
@request_use = opt[:request_use]
348
@response_use = opt[:response_use]
349
check_style(@request_style)
350
check_style(@response_style)
351
check_use(@request_use)
352
check_use(@response_use)
353
if @response_style == :rpc
354
request_qname = opt[:request_qname] or raise
355
@rpc_method_factory =
356
RPC::SOAPMethodRequest.new(request_qname, param_def, @soapaction)
357
@rpc_response_qname = opt[:response_qname]
359
@doc_request_qnames = []
360
@doc_request_qualified = []
361
@doc_response_qnames = []
362
@doc_response_qualified = []
363
param_def.each do |inout, paramname, typeinfo, eleinfo|
364
klass, nsdef, namedef = typeinfo
368
@doc_request_qnames << XSD::QName.new(nsdef, namedef)
369
@doc_request_qualified << qualified
371
@doc_response_qnames << XSD::QName.new(nsdef, namedef)
372
@doc_response_qualified << qualified
374
raise ArgumentError.new(
375
"illegal inout definition for document style: #{inout}")
381
def request_default_encodingstyle
382
(@request_use == :encoded) ? EncodingNamespace : LiteralNamespace
385
def response_default_encodingstyle
386
(@response_use == :encoded) ? EncodingNamespace : LiteralNamespace
389
def call(body, mapping_registry, literal_mapping_registry, opt)
390
if @request_style == :rpc
391
values = request_rpc(body, mapping_registry, literal_mapping_registry,
394
values = request_document(body, mapping_registry,
395
literal_mapping_registry, opt)
397
result = receiver.method(@name.intern).call(*values)
398
return result if result.is_a?(SOAPFault)
399
if @response_style == :rpc
400
response_rpc(result, mapping_registry, literal_mapping_registry, opt)
402
response_doc(result, mapping_registry, literal_mapping_registry, opt)
409
raise NotImplementedError.new('must be defined in derived class')
412
def request_rpc(body, mapping_registry, literal_mapping_registry, opt)
413
request = body.request
414
unless request.is_a?(SOAPStruct)
415
raise RPCRoutingError.new("not an RPC style")
417
if @request_use == :encoded
418
request_rpc_enc(request, mapping_registry, opt)
420
request_rpc_lit(request, literal_mapping_registry, opt)
424
def request_document(body, mapping_registry, literal_mapping_registry, opt)
425
# ToDo: compare names with @doc_request_qnames
426
if @request_use == :encoded
427
request_doc_enc(body, mapping_registry, opt)
429
request_doc_lit(body, literal_mapping_registry, opt)
433
def request_rpc_enc(request, mapping_registry, opt)
434
param = Mapping.soap2obj(request, mapping_registry, nil, opt)
435
request.collect { |key, value|
440
def request_rpc_lit(request, mapping_registry, opt)
441
request.collect { |key, value|
442
Mapping.soap2obj(value, mapping_registry, nil, opt)
446
def request_doc_enc(body, mapping_registry, opt)
447
body.collect { |key, value|
448
Mapping.soap2obj(value, mapping_registry, nil, opt)
452
def request_doc_lit(body, mapping_registry, opt)
453
body.collect { |key, value|
454
Mapping.soap2obj(value, mapping_registry, nil, opt)
458
def response_rpc(result, mapping_registry, literal_mapping_registry, opt)
459
if @response_use == :encoded
460
response_rpc_enc(result, mapping_registry, opt)
462
response_rpc_lit(result, literal_mapping_registry, opt)
466
def response_doc(result, mapping_registry, literal_mapping_registry, opt)
467
if @doc_response_qnames.size == 1 and !result.is_a?(Array)
470
if result.size != @doc_response_qnames.size
471
raise "required #{@doc_response_qnames.size} responses " +
472
"but #{result.size} given"
474
if @response_use == :encoded
475
response_doc_enc(result, mapping_registry, opt)
477
response_doc_lit(result, literal_mapping_registry, opt)
481
def response_rpc_enc(result, mapping_registry, opt)
483
@rpc_method_factory.create_method_response(@rpc_response_qname)
484
if soap_response.have_outparam?
485
unless result.is_a?(Array)
486
raise RPCRoutingError.new("out parameter was not returned")
490
soap_response.output_params.each do |outparam|
491
outparams[outparam] = Mapping.obj2soap(result[i], mapping_registry,
495
soap_response.set_outparam(outparams)
496
soap_response.retval = Mapping.obj2soap(result[0], mapping_registry,
499
soap_response.retval = Mapping.obj2soap(result, mapping_registry, nil,
505
def response_rpc_lit(result, mapping_registry, opt)
507
@rpc_method_factory.create_method_response(@rpc_response_qname)
508
if soap_response.have_outparam?
509
unless result.is_a?(Array)
510
raise RPCRoutingError.new("out parameter was not returned")
514
soap_response.output_params.each do |outparam|
515
outparams[outparam] = Mapping.obj2soap(result[i], mapping_registry,
516
XSD::QName.new(nil, outparam), opt)
519
soap_response.set_outparam(outparams)
520
soap_response.retval = Mapping.obj2soap(result[0], mapping_registry,
521
XSD::QName.new(nil, soap_response.elename), opt)
523
soap_response.retval = Mapping.obj2soap(result, mapping_registry,
524
XSD::QName.new(nil, soap_response.elename), opt)
529
def response_doc_enc(result, mapping_registry, opt)
530
(0...result.size).collect { |idx|
531
ele = Mapping.obj2soap(result[idx], mapping_registry, nil, opt)
532
ele.elename = @doc_response_qnames[idx]
537
def response_doc_lit(result, mapping_registry, opt)
538
(0...result.size).collect { |idx|
539
ele = Mapping.obj2soap(result[idx], mapping_registry,
540
@doc_response_qnames[idx])
541
ele.encodingstyle = LiteralNamespace
542
if ele.respond_to?(:qualified)
543
ele.qualified = @doc_response_qualified[idx]
549
def check_style(style)
550
unless [:rpc, :document].include?(style)
551
raise ArgumentError.new("unknown style: #{style}")
556
unless [:encoded, :literal].include?(use)
557
raise ArgumentError.new("unknown use: #{use}")
562
class ApplicationScopeOperation < Operation
563
def initialize(soapaction, receiver, name, param_def, opt)
564
super(soapaction, name, param_def, opt)
575
class RequestScopeOperation < Operation
576
def initialize(soapaction, receiver_factory, name, param_def, opt)
577
super(soapaction, name, param_def, opt)
578
unless receiver_factory.respond_to?(:create)
579
raise TypeError.new("factory must respond to 'create'")
581
@receiver_factory = receiver_factory
587
@receiver_factory.create