Class: Handsoap::Parser::Wsdl

Inherits:
Object
  • Object
show all
Defined in:
lib/handsoap/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, url = "void://") ⇒ Wsdl

Returns a new instance of Wsdl.



69
70
71
72
# File 'lib/handsoap/parser.rb', line 69

def initialize(doc, url = "void://")
  @doc = doc
  @url = url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



67
68
69
# File 'lib/handsoap/parser.rb', line 67

def url
  @url
end

Class Method Details

.read(url) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/handsoap/parser.rb', line 74

def self.read(url)
  if url =~ /^http(s?):/
    request = ::HTTPClient.new
    request.ssl_config.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
    response = request.get(url)
    xml_src = response.content
  else
    xml_src = Kernel.open(url).read
  end
  self.new(Nokogiri.XML(xml_src), url)
end

Instance Method Details

#bindingsObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/handsoap/parser.rb', line 197

def bindings
  @doc.xpath("//wsdl1:binding|//wsdl2:binding", ns).map do |binding|
    raise "WSDL 2.0 not supported" if is_wsdl2?(binding)
    soap_binding = binding.xpath("./soap11:binding|./soap12:binding|./http:binding", ns).first
    protocol = protocol_from_ns(soap_binding)
    actions = []
    style = nil
    encoding = nil
    actions = binding.xpath("./wsdl1:operation", ns).map do |operation|
      soap_operation = operation.xpath("./soap11:operation|./soap12:operation|./http:operation", ns).first
      if soap_operation[:style]
        raise "Mixed styles not supported" if style && style != soap_operation[:style]
        style = soap_operation[:style]
      end
      xquery = []
      ['soap11', 'soap12', 'http'].each do |version|
        ['input', 'output'].each do |message_name|
          ['header', 'body'].each do |part_name|
            xquery << "./wsdl1:#{message_name}/#{version}:#{part_name}"
          end
        end
      end
      operation.xpath(xquery.join('|'), ns).each do |thing|
        raise "Mixed encodings not supported" if encoding && encoding != thing[:use]
        encoding = thing[:use]
      end
      Action.new(
                 operation[:name],
                 :soap_action => soap_operation[:soapAction],
                 :location => soap_operation[:location])
    end
    Binding.new(
                binding[:name],
                :protocol => protocol,
                :interface => binding[:type],
                :transport => soap_binding[:transport],
                :style => style,
                :encoding => encoding,
                :verb => soap_binding[:verb],
                :actions => actions)
  end
end

#endpointsObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/handsoap/parser.rb', line 182

def endpoints
  @doc.xpath("//wsdl1:service/wsdl1:port|//wsdl2:service/wsdl2:endpoint", ns).map do |port|
    binding = port[:binding]
    if is_wsdl2?(port)
      location = port[:address]
      protocol = :binding
    else
      address = port.xpath("./soap11:address|./soap12:address|./http:address", ns).first
      location = address[:location]
      protocol = protocol_from_ns(address)
    end
    Endpoint.new(port[:name], protocol, binding, location)
  end
end

#interfaceObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/handsoap/parser.rb', line 131

def interface
  all_interfaces = self.interfaces
  if all_interfaces.length != 1
    # There are more than one portType, so we take a pick
    all_bindings = self.bindings
    all_interfaces.each do |interface|
      b = all_bindings.find {|binding| binding.name == interface.name }
      if [:soap11, :soap12].include? b.protocol
        return interface
      end
    end
    raise "Can't find a suitable soap 1.1 or 1.2 interface/portType in WSDL"
  end
  all_interfaces.first
end

#interfacesObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/handsoap/parser.rb', line 162

def interfaces
  @doc.xpath("//wsdl1:portType|//wsdl2:interface", ns).map do |port_type|
    operations = port_type.xpath("./wsdl1:operation|./wsdl2:operation", ns).map do |operation|
      if is_wsdl2?(operation)
        input_node = operation.xpath("./wsdl2:input", ns).first
        input = input_node ? input_node[:element] : nil
        output_node = operation.xpath("./wsdl2:output", ns).first
        output = output_node ? output_node[:element] : nil
      else
        input_node = operation.xpath("./wsdl1:input", ns).first
        input = input_node ? input_node[:message] : nil
        output_node = operation.xpath("./wsdl1:output", ns).first
        output = output_node ? output_node[:message] : nil
      end
      Operation.new(operation[:name], :input => input, :output => output)
    end
    Interface.new(port_type[:name], operations)
  end
end

#preferred_protocolObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/handsoap/parser.rb', line 151

def preferred_protocol
  e = endpoints
  if e.select { |endpoint| endpoint.protocol == :soap12 }.any?
    :soap12
  elsif e.select { |endpoint| endpoint.protocol == :soap11 }.any?
    :soap11
  else
    raise "Can't find any soap 1.1 or soap 1.2 endpoints"
  end
end

#serviceObject



125
126
127
128
129
# File 'lib/handsoap/parser.rb', line 125

def service
  services = @doc.xpath("//wsdl1:service|//wsdl2:service", ns)
  raise "Expected exactly 1 service in WSDL" if services.length != 1
  services[0][:name]
end

#target_nsObject



147
148
149
# File 'lib/handsoap/parser.rb', line 147

def target_ns
  @doc.root[:targetNamespace] || raise("Attribute targetNamespace not defined")
end