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



187
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
# File 'lib/handsoap/parser.rb', line 187

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



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

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
# File 'lib/handsoap/parser.rb', line 131

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

#interfacesObject



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

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



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

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



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

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