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.



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

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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.read(url) ⇒ Object



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

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



188
189
190
191
192
193
194
195
196
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
# File 'lib/handsoap/parser.rb', line 188

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/handsoap/parser.rb', line 173

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



132
133
134
135
136
# File 'lib/handsoap/parser.rb', line 132

def interface
  tmp = interfaces
  raise "Expected exactly 1 interface/portType in WSDL" if tmp.length != 1
  tmp[0]
end

#interfacesObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/handsoap/parser.rb', line 153

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



142
143
144
145
146
147
148
149
150
151
# File 'lib/handsoap/parser.rb', line 142

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



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

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



138
139
140
# File 'lib/handsoap/parser.rb', line 138

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