| 1 |
=begin |
|---|
| 2 |
SOAP4R |
|---|
| 3 |
Copyright (C) 2000 NAKAMURA Hiroshi. |
|---|
| 4 |
|
|---|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
|---|
| 6 |
the terms of the GNU General Public License as published by the Free Software |
|---|
| 7 |
Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 8 |
version. |
|---|
| 9 |
|
|---|
| 10 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
|---|
| 12 |
PRATICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 13 |
|
|---|
| 14 |
You should have received a copy of the GNU General Public License along with |
|---|
| 15 |
this program; if not, write to the Free Software Foundation, Inc., 675 Mass |
|---|
| 16 |
Ave, Cambridge, MA 02139, USA. |
|---|
| 17 |
=end |
|---|
| 18 |
|
|---|
| 19 |
require 'SOAP' |
|---|
| 20 |
require 'SOAPProcessor' |
|---|
| 21 |
require 'SOAPStreamHandler' |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
class SOAPProxy |
|---|
| 25 |
include SOAPProcessor |
|---|
| 26 |
|
|---|
| 27 |
public |
|---|
| 28 |
|
|---|
| 29 |
attr_reader :namespace |
|---|
| 30 |
|
|---|
| 31 |
def initialize( namespace, streamHandler ) |
|---|
| 32 |
@namespace = namespace |
|---|
| 33 |
@handler = streamHandler |
|---|
| 34 |
@method = {} |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
class Request |
|---|
| 38 |
public |
|---|
| 39 |
|
|---|
| 40 |
attr_reader :method |
|---|
| 41 |
attr_reader :namespace |
|---|
| 42 |
attr_reader :name |
|---|
| 43 |
|
|---|
| 44 |
def initialize( modelMethod, values ) |
|---|
| 45 |
@method = SOAPMethod.new( modelMethod.namespace, modelMethod.name, |
|---|
| 46 |
modelMethod.paramDef ) |
|---|
| 47 |
@namespace = @method.namespace |
|---|
| 48 |
@name = @method.name |
|---|
| 49 |
|
|---|
| 50 |
params = {} |
|---|
| 51 |
|
|---|
| 52 |
if (( values.size == 1 ) and ( values[0].is_a?( Hash ))) |
|---|
| 53 |
params = values[ 0 ] |
|---|
| 54 |
else |
|---|
| 55 |
0.upto( values.size - 1 ) do | i | |
|---|
| 56 |
params[ @method.paramNames[ i ]] = values[ i ] || SOAPNull.new() |
|---|
| 57 |
end |
|---|
| 58 |
end |
|---|
| 59 |
@method.setParams( params ) |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
# Method definition. |
|---|
| 64 |
def addMethod( methodName, paramDef ) |
|---|
| 65 |
@method[ methodName ] = SOAPMethod.new( @namespace, methodName, paramDef ) |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
# Create new request. |
|---|
| 69 |
def createRequest( methodName, *values ) |
|---|
| 70 |
if ( @method.has_key?( methodName )) |
|---|
| 71 |
method = @method[ methodName ] |
|---|
| 72 |
else |
|---|
| 73 |
raise MethodDefinitionError.new( 'Method: ' << methodName << ' not defined.' ) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
Request.new( method, values ) |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
# Method calling. |
|---|
| 80 |
def call( ns, headers, methodName, *values ) |
|---|
| 81 |
|
|---|
| 82 |
# Create new request |
|---|
| 83 |
req = createRequest( methodName, *values ) |
|---|
| 84 |
|
|---|
| 85 |
# SOAP tree construction. |
|---|
| 86 |
tree = createTree( ns, headers, req ) |
|---|
| 87 |
|
|---|
| 88 |
# Send request. |
|---|
| 89 |
receiveString = sendRequest( req, tree ) |
|---|
| 90 |
|
|---|
| 91 |
# SOAP tree parsing. |
|---|
| 92 |
ns, header, body = parseTree( req, receiveString ) |
|---|
| 93 |
|
|---|
| 94 |
# Check Fault. |
|---|
| 95 |
checkFault( ns, body ) |
|---|
| 96 |
|
|---|
| 97 |
# Used namespaces, header element, and body element. |
|---|
| 98 |
return ns, header, body |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
# SOAP tree construction. |
|---|
| 102 |
def createTree( ns, headers, request ) |
|---|
| 103 |
# Preparing headers. |
|---|
| 104 |
header = SOAPHeader.new() |
|---|
| 105 |
if headers |
|---|
| 106 |
headers.each do | namespace, elem, content, mustUnderstand, encodingStyle | |
|---|
| 107 |
header.add( SOAPHeaderItem.new( namespace, elem, content, mustUnderstand, encodingStyle )) |
|---|
| 108 |
end |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
# Preparing body. |
|---|
| 112 |
body = SOAPBody.new( request.method ) |
|---|
| 113 |
|
|---|
| 114 |
# Tree construction. |
|---|
| 115 |
soapTree = marshal( ns, header, body ) |
|---|
| 116 |
|
|---|
| 117 |
return soapTree |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
# Send the request. |
|---|
| 121 |
def sendRequest( request, tree ) |
|---|
| 122 |
# Serialize. |
|---|
| 123 |
sendString = tree.to_s |
|---|
| 124 |
|
|---|
| 125 |
# Send request. |
|---|
| 126 |
receiveString = @handler.send( request.namespace, request.name, sendString ) |
|---|
| 127 |
|
|---|
| 128 |
receiveString |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
# SOAP tree parsing. |
|---|
| 132 |
def parseTree( request, receiveString ) |
|---|
| 133 |
# SOAP tree parsing. |
|---|
| 134 |
ns, header, body = unmarshal( request.method, receiveString ) |
|---|
| 135 |
|
|---|
| 136 |
# Used namespaces, header element, and body element. |
|---|
| 137 |
return ns, header, body |
|---|
| 138 |
end |
|---|
| 139 |
|
|---|
| 140 |
# SOAP Fault checking. |
|---|
| 141 |
def checkFault( ns, body ) |
|---|
| 142 |
if ( body.isFault ) |
|---|
| 143 |
raise FaultError.new( body.data ) |
|---|
| 144 |
end |
|---|
| 145 |
end |
|---|
| 146 |
end |
|---|