| 197 | | class WSDLDriver |
|---|
| 198 | | include AttrProxy |
|---|
| 199 | | |
|---|
| 200 | | attr_proxy :options |
|---|
| 201 | | attr_proxy :headerhandler |
|---|
| 202 | | attr_proxy :streamhandler |
|---|
| 203 | | attr_proxy :test_loopback_response |
|---|
| 204 | | attr_proxy :endpoint_url, true |
|---|
| 205 | | attr_proxy :mapping_registry, true # for RPC unmarshal |
|---|
| 206 | | attr_proxy :wsdl_mapping_registry, true # for RPC marshal |
|---|
| 207 | | attr_proxy :default_encodingstyle, true |
|---|
| 208 | | attr_proxy :generate_explicit_type, true |
|---|
| 209 | | attr_proxy :allow_unqualified_element, true |
|---|
| 210 | | |
|---|
| 211 | | def httpproxy |
|---|
| 212 | | @servant.options["protocol.http.proxy"] |
|---|
| 213 | | end |
|---|
| 214 | | |
|---|
| 215 | | def httpproxy=(httpproxy) |
|---|
| 216 | | @servant.options["protocol.http.proxy"] = httpproxy |
|---|
| 217 | | end |
|---|
| 218 | | |
|---|
| 219 | | def wiredump_dev |
|---|
| 220 | | @servant.options["protocol.http.wiredump_dev"] |
|---|
| 221 | | end |
|---|
| 222 | | |
|---|
| 223 | | def wiredump_dev=(wiredump_dev) |
|---|
| 224 | | @servant.options["protocol.http.wiredump_dev"] = wiredump_dev |
|---|
| 225 | | end |
|---|
| 226 | | |
|---|
| 227 | | def mandatorycharset |
|---|
| 228 | | @servant.options["protocol.mandatorycharset"] |
|---|
| 229 | | end |
|---|
| 230 | | |
|---|
| 231 | | def mandatorycharset=(mandatorycharset) |
|---|
| 232 | | @servant.options["protocol.mandatorycharset"] = mandatorycharset |
|---|
| 233 | | end |
|---|
| 234 | | |
|---|
| 235 | | def wiredump_file_base |
|---|
| 236 | | @servant.options["protocol.wiredump_file_base"] |
|---|
| 237 | | end |
|---|
| 238 | | |
|---|
| 239 | | def wiredump_file_base=(wiredump_file_base) |
|---|
| 240 | | @servant.options["protocol.wiredump_file_base"] = wiredump_file_base |
|---|
| 241 | | end |
|---|
| 242 | | |
|---|
| 243 | | def initialize(wsdl, port, logdev) |
|---|
| 244 | | @servant = Servant__.new(self, wsdl, port, logdev) |
|---|
| 245 | | end |
|---|
| 246 | | |
|---|
| 247 | | def inspect |
|---|
| 248 | | "#<#{self.class}:#{@servant.port.name}>" |
|---|
| 249 | | end |
|---|
| 250 | | |
|---|
| 251 | | def reset_stream |
|---|
| 252 | | @servant.reset_stream |
|---|
| 253 | | end |
|---|
| 254 | | |
|---|
| 255 | | private |
|---|
| 256 | | |
|---|
| 257 | | def attrproxy |
|---|
| 258 | | @servant |
|---|
| 259 | | end |
|---|
| 260 | | |
|---|
| 261 | | class Servant__ |
|---|
| 262 | | include SOAP |
|---|
| 263 | | |
|---|
| 264 | | attr_reader :options |
|---|
| 265 | | attr_reader :port |
|---|
| 266 | | |
|---|
| 267 | | attr_accessor :soapaction |
|---|
| 268 | | attr_accessor :default_encodingstyle |
|---|
| 269 | | attr_accessor :allow_unqualified_element |
|---|
| 270 | | attr_accessor :generate_explicit_type |
|---|
| 271 | | attr_accessor :mapping_registry |
|---|
| 272 | | attr_accessor :wsdl_mapping_registry |
|---|
| 273 | | |
|---|
| 274 | | def initialize(host, wsdl, port, logdev) |
|---|
| 275 | | @host = host |
|---|
| 276 | | @wsdl = wsdl |
|---|
| 277 | | @port = port |
|---|
| 278 | | @logdev = logdev |
|---|
| 279 | | @soapaction = nil |
|---|
| 280 | | @options = setup_options |
|---|
| 281 | | @default_encodingstyle = nil |
|---|
| 282 | | @allow_unqualified_element = nil |
|---|
| 283 | | @generate_explicit_type = false |
|---|
| 284 | | @mapping_registry = nil # for rpc unmarshal |
|---|
| 285 | | @wsdl_mapping_registry = nil # for rpc marshal |
|---|
| 286 | | @wiredump_file_base = nil |
|---|
| 287 | | @mandatorycharset = nil |
|---|
| 288 | | @wsdl_elements = @wsdl.collect_elements |
|---|
| 289 | | @wsdl_types = @wsdl.collect_complextypes + @wsdl.collect_simpletypes |
|---|
| 290 | | @rpc_decode_typemap = @wsdl_types + |
|---|
| 291 | | @wsdl.soap_rpc_complextypes(port.find_binding) |
|---|
| 292 | | @wsdl_mapping_registry = Mapping::WSDLEncodedRegistry.new( |
|---|
| 293 | | @rpc_decode_typemap) |
|---|
| 294 | | @doc_mapper = Mapping::WSDLLiteralRegistry.new( |
|---|
| 295 | | @wsdl_types, @wsdl_elements) |
|---|
| 296 | | endpoint_url = @port.soap_address.location |
|---|
| 297 | | # Convert a map which key is QName, to a Hash which key is String. |
|---|
| 298 | | @operation = {} |
|---|
| 299 | | @port.inputoperation_map.each do |op_name, op_info| |
|---|
| 300 | | orgname = op_name.name |
|---|
| 301 | | name = XSD::CodeGen::GenSupport.safemethodname(orgname) |
|---|
| 302 | | @operation[name] = @operation[orgname] = op_info |
|---|
| 303 | | add_method_interface(op_info) |
|---|
| 304 | | end |
|---|
| 305 | | @proxy = ::SOAP::RPC::Proxy.new(endpoint_url, @soapaction, @options) |
|---|
| 306 | | end |
|---|
| 307 | | |
|---|
| 308 | | def inspect |
|---|
| 309 | | "#<#{self.class}:#{@proxy.inspect}>" |
|---|
| 310 | | end |
|---|
| 311 | | |
|---|
| 312 | | def endpoint_url |
|---|
| 313 | | @proxy.endpoint_url |
|---|
| 314 | | end |
|---|
| 315 | | |
|---|
| 316 | | def endpoint_url=(endpoint_url) |
|---|
| 317 | | @proxy.endpoint_url = endpoint_url |
|---|
| 318 | | end |
|---|
| 319 | | |
|---|
| 320 | | def headerhandler |
|---|
| 321 | | @proxy.headerhandler |
|---|
| 322 | | end |
|---|
| 323 | | |
|---|
| 324 | | def streamhandler |
|---|
| 325 | | @proxy.streamhandler |
|---|
| 326 | | end |
|---|
| 327 | | |
|---|
| 328 | | def test_loopback_response |
|---|
| 329 | | @proxy.test_loopback_response |
|---|
| 330 | | end |
|---|
| 331 | | |
|---|
| 332 | | def reset_stream |
|---|
| 333 | | @proxy.reset_stream |
|---|
| 334 | | end |
|---|
| 335 | | |
|---|
| 336 | | def rpc_call(name, *values) |
|---|
| 337 | | set_wiredump_file_base(name) |
|---|
| 338 | | unless op_info = @operation[name] |
|---|
| 339 | | raise RuntimeError, "method: #{name} not defined" |
|---|
| 340 | | end |
|---|
| 341 | | req_header = create_request_header |
|---|
| 342 | | req_body = create_request_body(op_info, *values) |
|---|
| 343 | | reqopt = create_options({ |
|---|
| 344 | | :soapaction => op_info.soapaction || @soapaction}) |
|---|
| 345 | | resopt = create_options({ |
|---|
| 346 | | :decode_typemap => @rpc_decode_typemap}) |
|---|
| 347 | | env = @proxy.route(req_header, req_body, reqopt, resopt) |
|---|
| 348 | | raise EmptyResponseError unless env |
|---|
| 349 | | receive_headers(env.header) |
|---|
| 350 | | begin |
|---|
| 351 | | @proxy.check_fault(env.body) |
|---|
| 352 | | rescue ::SOAP::FaultError => e |
|---|
| 353 | | Mapping.fault2exception(e) |
|---|
| 354 | | end |
|---|
| 355 | | ret = env.body.response ? |
|---|
| 356 | | Mapping.soap2obj(env.body.response, @mapping_registry) : nil |
|---|
| 357 | | if env.body.outparams |
|---|
| 358 | | outparams = env.body.outparams.collect { |outparam| |
|---|
| 359 | | Mapping.soap2obj(outparam) |
|---|
| 360 | | } |
|---|
| 361 | | return [ret].concat(outparams) |
|---|
| 362 | | else |
|---|
| 363 | | return ret |
|---|
| 364 | | end |
|---|
| 365 | | end |
|---|
| 366 | | |
|---|
| 367 | | # req_header: [[element, mustunderstand, encodingstyle(QName/String)], ...] |
|---|
| 368 | | # req_body: SOAPBasetype/SOAPCompoundtype |
|---|
| 369 | | def document_send(name, header_obj, body_obj) |
|---|
| 370 | | set_wiredump_file_base(name) |
|---|
| 371 | | unless op_info = @operation[name] |
|---|
| 372 | | raise RuntimeError, "method: #{name} not defined" |
|---|
| 373 | | end |
|---|
| 374 | | req_header = header_obj ? header_from_obj(header_obj, op_info) : nil |
|---|
| 375 | | req_body = body_from_obj(body_obj, op_info) |
|---|
| 376 | | opt = create_options({ |
|---|
| 377 | | :soapaction => op_info.soapaction || @soapaction, |
|---|
| 378 | | :decode_typemap => @wsdl_types}) |
|---|
| 379 | | env = @proxy.invoke(req_header, req_body, opt) |
|---|
| 380 | | raise EmptyResponseError unless env |
|---|
| 381 | | if env.body.fault |
|---|
| 382 | | raise ::SOAP::FaultError.new(env.body.fault) |
|---|
| 383 | | end |
|---|
| 384 | | res_body_obj = env.body.response ? |
|---|
| 385 | | Mapping.soap2obj(env.body.response, @mapping_registry) : nil |
|---|
| 386 | | return env.header, res_body_obj |
|---|
| 387 | | end |
|---|
| 388 | | |
|---|
| 389 | | private |
|---|
| 390 | | |
|---|
| 391 | | def create_options(hash = nil) |
|---|
| 392 | | opt = {} |
|---|
| 393 | | opt[:default_encodingstyle] = @default_encodingstyle |
|---|
| 394 | | opt[:allow_unqualified_element] = @allow_unqualified_element |
|---|
| 395 | | opt[:generate_explicit_type] = @generate_explicit_type |
|---|
| 396 | | opt.update(hash) if hash |
|---|
| 397 | | opt |
|---|
| 398 | | end |
|---|
| 399 | | |
|---|
| 400 | | def set_wiredump_file_base(name) |
|---|
| 401 | | if @wiredump_file_base |
|---|
| 402 | | @proxy.set_wiredump_file_base(@wiredump_file_base + "_#{name}") |
|---|
| 403 | | end |
|---|
| 404 | | end |
|---|
| 405 | | |
|---|
| 406 | | def create_request_header |
|---|
| 407 | | header = SOAPHeader.new |
|---|
| 408 | | items = @proxy.headerhandler.on_outbound(header) |
|---|
| 409 | | items.each do |item| |
|---|
| 410 | | header.add(item.elename.name, item) |
|---|
| 411 | | end |
|---|
| 412 | | header |
|---|
| 413 | | end |
|---|
| 414 | | |
|---|
| 415 | | def receive_headers(header) |
|---|
| 416 | | @proxy.headerhandler.on_inbound(header) if header |
|---|
| 417 | | end |
|---|
| 418 | | |
|---|
| 419 | | def create_request_body(op_info, *values) |
|---|
| 420 | | method = create_method_struct(op_info, *values) |
|---|
| 421 | | SOAPBody.new(method) |
|---|
| 422 | | end |
|---|
| 423 | | |
|---|
| 424 | | def create_method_struct(op_info, *params) |
|---|
| 425 | | parts_names = op_info.bodyparts.collect { |part| part.name } |
|---|
| 426 | | obj = create_method_obj(parts_names, params) |
|---|
| 427 | | method = Mapping.obj2soap(obj, @wsdl_mapping_registry, op_info.op_name) |
|---|
| 428 | | if method.members.size != parts_names.size |
|---|
| 429 | | new_method = SOAPStruct.new |
|---|
| 430 | | method.each do |key, value| |
|---|
| 431 | | if parts_names.include?(key) |
|---|
| 432 | | new_method.add(key, value) |
|---|
| 433 | | end |
|---|
| 434 | | end |
|---|
| 435 | | method = new_method |
|---|
| 436 | | end |
|---|
| 437 | | method.elename = op_info.op_name |
|---|
| 438 | | method.type = XSD::QName.new # Request should not be typed. |
|---|
| 439 | | method |
|---|
| 440 | | end |
|---|
| 441 | | |
|---|
| 442 | | def create_method_obj(names, params) |
|---|
| 443 | | o = Object.new |
|---|
| 444 | | idx = 0 |
|---|
| 445 | | while idx < params.length |
|---|
| 446 | | o.instance_variable_set('@' + names[idx], params[idx]) |
|---|
| 447 | | idx += 1 |
|---|
| 448 | | end |
|---|
| 449 | | o |
|---|
| 450 | | end |
|---|
| 451 | | |
|---|
| 452 | | def header_from_obj(obj, op_info) |
|---|
| 453 | | if obj.is_a?(SOAPHeader) |
|---|
| 454 | | obj |
|---|
| 455 | | elsif op_info.headerparts.empty? |
|---|
| 456 | | if obj.nil? |
|---|
| 457 | | nil |
|---|
| 458 | | else |
|---|
| 459 | | raise RuntimeError.new("no header definition in schema: #{obj}") |
|---|
| 460 | | end |
|---|
| 461 | | elsif op_info.headerparts.size == 1 |
|---|
| 462 | | part = op_info.headerparts[0] |
|---|
| 463 | | header = SOAPHeader.new() |
|---|
| 464 | | header.add(headeritem_from_obj(obj, part.element || part.eletype)) |
|---|
| 465 | | header |
|---|
| 466 | | else |
|---|
| 467 | | header = SOAPHeader.new() |
|---|
| 468 | | op_info.headerparts.each do |part| |
|---|
| 469 | | child = Mapping.get_attribute(obj, part.name) |
|---|
| 470 | | ele = headeritem_from_obj(child, part.element || part.eletype) |
|---|
| 471 | | header.add(part.name, ele) |
|---|
| 472 | | end |
|---|
| 473 | | header |
|---|
| 474 | | end |
|---|
| 475 | | end |
|---|
| 476 | | |
|---|
| 477 | | def headeritem_from_obj(obj, name) |
|---|
| 478 | | if obj.nil? |
|---|
| 479 | | SOAPElement.new(name) |
|---|
| 480 | | elsif obj.is_a?(SOAPHeaderItem) |
|---|
| 481 | | obj |
|---|
| 482 | | else |
|---|
| 483 | | Mapping.obj2soap(obj, @doc_mapper, name) |
|---|
| 484 | | end |
|---|
| 485 | | end |
|---|
| 486 | | |
|---|
| 487 | | def body_from_obj(obj, op_info) |
|---|
| 488 | | if obj.is_a?(SOAPBody) |
|---|
| 489 | | obj |
|---|
| 490 | | elsif op_info.bodyparts.empty? |
|---|
| 491 | | if obj.nil? |
|---|
| 492 | | nil |
|---|
| 493 | | else |
|---|
| 494 | | raise RuntimeError.new("no body found in schema") |
|---|
| 495 | | end |
|---|
| 496 | | elsif op_info.bodyparts.size == 1 |
|---|
| 497 | | part = op_info.bodyparts[0] |
|---|
| 498 | | ele = bodyitem_from_obj(obj, part.element || part.type) |
|---|
| 499 | | SOAPBody.new(ele) |
|---|
| 500 | | else |
|---|
| 501 | | body = SOAPBody.new |
|---|
| 502 | | op_info.bodyparts.each do |part| |
|---|
| 503 | | child = Mapping.get_attribute(obj, part.name) |
|---|
| 504 | | ele = bodyitem_from_obj(child, part.element || part.type) |
|---|
| 505 | | body.add(ele.elename.name, ele) |
|---|
| 506 | | end |
|---|
| 507 | | body |
|---|
| 508 | | end |
|---|
| 509 | | end |
|---|
| 510 | | |
|---|
| 511 | | def bodyitem_from_obj(obj, name) |
|---|
| 512 | | if obj.nil? |
|---|
| 513 | | SOAPElement.new(name) |
|---|
| 514 | | elsif obj.is_a?(SOAPElement) |
|---|
| 515 | | obj |
|---|
| 516 | | else |
|---|
| 517 | | Mapping.obj2soap(obj, @doc_mapper, name) |
|---|
| 518 | | end |
|---|
| 519 | | end |
|---|
| 520 | | |
|---|
| 521 | | def add_method_interface(op_info) |
|---|
| 522 | | name = XSD::CodeGen::GenSupport.safemethodname(op_info.op_name.name) |
|---|
| 523 | | orgname = op_info.op_name.name |
|---|
| 524 | | parts_names = op_info.bodyparts.collect { |part| part.name } |
|---|
| 525 | | case op_info.style |
|---|
| 526 | | when :document |
|---|
| 527 | | if orgname != name and orgname.capitalize == name.capitalize |
|---|
| 528 | | add_document_method_interface(orgname, parts_names) |
|---|
| 529 | | end |
|---|
| 530 | | add_document_method_interface(name, parts_names) |
|---|
| 531 | | when :rpc |
|---|
| 532 | | if orgname != name and orgname.capitalize == name.capitalize |
|---|
| 533 | | add_rpc_method_interface(orgname, parts_names) |
|---|
| 534 | | end |
|---|
| 535 | | add_rpc_method_interface(name, parts_names) |
|---|
| 536 | | else |
|---|
| 537 | | raise RuntimeError.new("unknown style: #{op_info.style}") |
|---|
| 538 | | end |
|---|
| 539 | | end |
|---|
| 540 | | |
|---|
| 541 | | def add_rpc_method_interface(name, parts_names) |
|---|
| 542 | | param_count = parts_names.size |
|---|
| 543 | | @host.instance_eval <<-EOS |
|---|
| 544 | | def #{name}(*arg) |
|---|
| 545 | | unless arg.size == #{param_count} |
|---|
| 546 | | raise ArgumentError.new( |
|---|
| 547 | | "wrong number of arguments (\#{arg.size} for #{param_count})") |
|---|
| 548 | | end |
|---|
| 549 | | @servant.rpc_call(#{name.dump}, *arg) |
|---|
| 550 | | end |
|---|
| 551 | | EOS |
|---|
| 552 | | @host.method(name) |
|---|
| 553 | | end |
|---|
| 554 | | |
|---|
| 555 | | def add_document_method_interface(name, parts_names) |
|---|
| 556 | | @host.instance_eval <<-EOS |
|---|
| 557 | | def #{name}(h, b) |
|---|
| 558 | | @servant.document_send(#{name.dump}, h, b) |
|---|
| 559 | | end |
|---|
| 560 | | EOS |
|---|
| 561 | | @host.method(name) |
|---|
| 562 | | end |
|---|
| 563 | | |
|---|
| 564 | | def setup_options |
|---|
| 565 | | if opt = Property.loadproperty(::SOAP::PropertyName) |
|---|
| 566 | | opt = opt["client"] |
|---|
| 567 | | end |
|---|
| 568 | | opt ||= Property.new |
|---|
| 569 | | opt.add_hook("protocol.mandatorycharset") do |key, value| |
|---|
| 570 | | @mandatorycharset = value |
|---|
| 571 | | end |
|---|
| 572 | | opt.add_hook("protocol.wiredump_file_base") do |key, value| |
|---|
| 573 | | @wiredump_file_base = value |
|---|
| 574 | | end |
|---|
| 575 | | opt["protocol.http.charset"] ||= XSD::Charset.xml_encoding_label |
|---|
| 576 | | opt["protocol.http.proxy"] ||= Env::HTTP_PROXY |
|---|
| 577 | | opt["protocol.http.no_proxy"] ||= Env::NO_PROXY |
|---|
| 578 | | opt |
|---|
| 579 | | end |
|---|
| 580 | | end |
|---|