File: proxy.rb

package info (click to toggle)
soap4r 1.4.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,472 kB
  • ctags: 2,206
  • sloc: ruby: 19,295; makefile: 58; sh: 41; perl: 10
file content (159 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
=begin
SOAP4R - Proxy library.
Copyright (C) 2000 NAKAMURA Hiroshi.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PRATICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
=end

require 'soap/soap'
require 'soap/processor'
require 'soap/rpcUtils'
require 'soap/streamHandler'


module SOAP


class SOAPProxy
  include SOAP
  include RPCUtils

  public

  attr_reader :namespace
  attr_accessor :soapAction
  attr_accessor :allowUnqualifiedElement, :defaultEncodingStyle
  attr_reader :method

  def initialize( namespace, streamHandler, soapAction = nil )
    @namespace = namespace
    @handler = streamHandler
    @soapAction = soapAction
    @method = {}
    @allowUnqualifiedElement = false
    @defaultEncodingStyle = nil
  end

  class Request
    include RPCUtils

    public

    attr_reader :method
    attr_reader :namespace
    attr_reader :name

    def initialize( modelMethod, values )
      @method = modelMethod.dup
      @namespace = @method.elementName.namespace
      @name = @method.elementName.name

      params = {}
    
      if (( values.size == 1 ) and ( values[ 0 ].is_a?( Hash )))
	params = values[ 0 ]
      else
	i = 0
	@method.eachParamName( SOAPMethod::IN, SOAPMethod::INOUT ) do | paramName |
	  params[ paramName ] = values[ i ] || SOAPNil.new
	  i += 1
	end
      end
      @method.setParams( params )
    end
  end

  def addMethod( methodName, paramDef, soapAction = nil, namespace = nil )
    addMethodAs( methodName, methodName, paramDef, soapAction, namespace )
  end

  def addMethodAs( methodNameAs, methodName, paramDef, soapAction = nil,
      namespace = nil )
    @method[ methodName ] = SOAPMethodRequest.new( namespace || @namespace,
      methodNameAs, paramDef, soapAction )
  end

  def createRequest( methodName, *values )
    if ( @method.has_key?( methodName ))
      method = @method[ methodName ]
      method.encodingStyle = @defaultEncodingStyle if @defaultEncodingStyle
    else
      raise SOAP::RPCUtils::MethodDefinitionError.new( 'Method: ' <<
	methodName << ' not defined.' )
    end

    Request.new( method, values )
  end

  def invoke( headers, body, soapAction = nil )
    # Get sending string.
    sendString = marshal( headers, body )

    # Send request.
    data = @handler.send( sendString, soapAction )
    return data
  end

  def call( headers, methodName, *values )
    req = createRequest( methodName, *values )
    data = invoke( headers, req.method, req.method.soapAction || @soapAction )
    if data.receiveString.empty?
      return nil, nil
    end

    # Received charset might be different from request.
    receiveCharset = StreamHandler.parseMediaType( data.receiveContentType )
    opt = getOpt
    opt[ :charset ] = receiveCharset
    header, body = Processor.unmarshal( data.receiveString, opt )
    return header, body
  end

  def marshal( headers, body )
    # Preparing headers.
    header = SOAPHeader.new()
    if headers
      headers.each do | content, mustUnderstand, encodingStyle |
        header.add( SOAPHeaderItem.new( content, mustUnderstand,
	  encodingStyle ))
      end
    end

    # Preparing body.
    body = SOAPBody.new( body )

    # Marshal.
    marshalledString = Processor.marshal( header, body, getOpt )

    return marshalledString
  end

  def checkFault( body )
    if ( body.fault )
      raise SOAP::FaultError.new( body.fault )
    end
  end

  def getOpt
    opt = {}
    opt[ :defaultEncodingStyle ] = @defaultEncodingStyle
    if @allowUnqualifiedElement
      opt[ :allowUnqualifiedElement ] = true
    end
    opt
  end
end


end