Class: SOAP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl-reader/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wsdl, binding = nil) ⇒ Request

:nodoc:



8
9
10
11
12
# File 'lib/wsdl-reader/request.rb', line 8

def initialize(wsdl, binding = nil) #:nodoc:
  @wsdl = wsdl
  @binding = binding
  @request = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

Call a method for the current Request and get a SOAP::Response

Example:

wsdl = SOAP::LC.new.wsdl("http://...")
request = wsdl.request
response = request.myMethod(:param1 => "hello")
  # => #<SOAP::Response:0xNNNNNN>


21
22
23
# File 'lib/wsdl-reader/request.rb', line 21

def method_missing(id, *args)
  call(id.id2name, args[0])
end

Class Method Details

.request(envelope, uri, headers = {}) ⇒ Object

Create a new SOAP::Request with the given envelope, uri and headers

Example:

e = '<SOAP-ENV:Envelope 
                 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
       <SOAP-ENV:Header/>
         <SOAP-ENV:Body>
           <HelloWorld xmlns="urn:MyWebService">
             <from>Greg</from>
           </HelloWorld>
         </SOAP-ENV:Body>
       </SOAP-ENV:Envelope>'
r = SOAP::Request.request(e, "http://localhost:3000/hello/wsdl", "SOAPAction" => "my.soap.action")


42
43
44
45
46
# File 'lib/wsdl-reader/request.rb', line 42

def self.request(envelope, uri, headers = {})
  req = new(nil, nil)
  req.r(envelope, uri, headers)
  return req
end

Instance Method Details

#call(method_name, args) ⇒ Object

Call a method for the current Request

Example:

wsdl = SOAP::LC.new.wsdl("http://...")
request = wsdl.request
response = request.call("myMethod", :param1 => "hello")
  # => #<SOAP::Response:0xNNNNNN>


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
160
161
# File 'lib/wsdl-reader/request.rb', line 71

def call(method_name, args)
  args = (args || {}).keys_to_sym!

  # Get Binding
  binding = @wsdl.bindings.get_binding_for_operation_name(@binding, method_name)
  if binding.size == 0
    raise SOAP::LCNoMethodError, "Undefined method `#{method_name}'"
  elsif binding.size > 1
    raise SOAP::LCError, "Ambigous method name `#{method_name}', please, specify a binding name"
  else
    binding = binding[0]
    @binding = binding.name
  end

  # Get Binding Operation
  binding_operation = binding.operations[method_name]

  # Get PortType
  port_type = @wsdl.port_types[binding.type.nns]
  port_type_operation = port_type.operations[method_name]

  # Get message for input operation
  input_message = @wsdl.messages[port_type_operation[:input][:message].nns]

  # Create method
  soap_method = "<#{method_name} xmlns=\"#{@wsdl.target_namespace}\">\n"
  input_message.parts.each do |_, attrs|
    case attrs[:mode]
      when :type
        if SOAP::XSD::ANY_SIMPLE_TYPE.include?(attrs[attrs[:mode]].nns)
          # Part refer to a builtin SimpleType
          soap_method << SOAP::XSD.displayBuiltinType(attrs[:name], args, 1, 1)
        else
          # Part refer to an XSD simpleType or complexType defined in types
          element = @wsdl.types[attrs[attrs[:mode]].nns][:value]
          case element[:type]
            when :simpleType
              soap_method << "<#{attrs[:name]}>\n#{element.display(@wsdl.types, args)}\n</#{attrs[:name]}>\n" # MAYBE ##########
            when :complexType
              soap_method << "<#{attrs[:name]}>\n#{element.display(@wsdl.types, args)}\n</#{attrs[:name]}>\n" # MAYBE ##########
            else
              raise SOAP::LCWSDLError, "Malformated part #{attrs[:name]}"
          end
        end
      when :element
        # Part refer to an XSD element
        element = @wsdl.types[attrs[attrs[:mode]].nns][:value]
        case @wsdl.types[attrs[attrs[:mode]].nns][:type]
          when :simpleType
            soap_method << element[element[:type]].display(@wsdl.types, args)
          when :complexType
            soap_method << element[element[:type]].display(@wsdl.types, args)
          else
            raise SOAP::LCWSDLError, "Malformed element `#{attrs[attrs[:mode]]}'"
        end
    
        ## TODO ---------- USE element[:key]
      else 
        raise SOAP::LCWSDLError, "Malformed part #{attrs[:name]}"
    end
  end
  soap_method += "</#{method_name}>\n"

  # Create SOAP Envelope
  envelope = soap_envelop { soap_header + soap_body(soap_method) }

  # Create headers
  headers = Hash.new

  # Add SOAPAction to headers (if exist)
  action = binding_operation[:soapAction] rescue nil

  headers['SOAPAction'] = action unless action.nil? || action.length == 0

  # Search URI
  service_port = @wsdl.services.getServicePortForBindingName(binding.name)
  address = service_port[:address]

  # Complete request
  @request = {
    :headers  => make_header(envelope, headers),
    :envelope => envelope,
    :uri      => address,
    :wsdl     => @wsdl,
    :response => @wsdl.messages[port_type_operation[:output][:message].nns].name,
    :binding  => @binding,
    :method   => method_name
  }
  
  self
end

#envelopeObject

Get the SOAP Envelope for the request



175
176
177
# File 'lib/wsdl-reader/request.rb', line 175

def envelope
  @request[:envelope] || nil
end

#headersObject

Get request headers



180
181
182
# File 'lib/wsdl-reader/request.rb', line 180

def headers
  @request[:headers] || nil
end

#operationsObject

Return available SOAP actions



60
61
62
# File 'lib/wsdl-reader/request.rb', line 60

def operations
  @wsdl.bindings.get_operations(@binding)
end

#r(envelope, uri, headers) ⇒ Object

:nodoc:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wsdl-reader/request.rb', line 47

def r(envelope, uri, headers) #:nodoc:
  @request = {
    :headers  => make_header(envelope, headers),
    :envelope => envelope,
    :uri      => uri,
    :wsdl     => nil,
    :response => nil,
    :binding  => @binding,
    :method   => nil
  }
end

#responseObject Also known as: result

Send request to the server and get a response (SOAP::Response)



169
170
171
# File 'lib/wsdl-reader/request.rb', line 169

def response
  SOAP::Response.new(@request)
end

#soap_body(soap_method) ⇒ Object

Get the SOAP Body for the request



164
165
166
# File 'lib/wsdl-reader/request.rb', line 164

def soap_body(soap_method)
  "<SOAP-ENV:Body>\n" + soap_method + "</SOAP-ENV:Body>\n"
end

#uriObject

Get request URI



185
186
187
# File 'lib/wsdl-reader/request.rb', line 185

def uri
  @request[:uri] || nil
end