Class: ActionWebService::Protocol::Soap::SoapMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/action_web_service/protocol/soap_protocol.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_namespace) ⇒ SoapMapper

Returns a new instance of SoapMapper.



201
202
203
204
205
206
207
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 201

def initialize(custom_namespace)
  @custom_namespace = custom_namespace
  @registry = SOAP::Mapping::Registry.new
  @klass2map = {}
  @custom_types = {}
  @ar2klass = {}
end

Instance Attribute Details

#custom_namespaceObject (readonly)

Returns the value of attribute custom_namespace.



198
199
200
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 198

def custom_namespace
  @custom_namespace
end

#custom_typesObject (readonly)

Returns the value of attribute custom_types.



199
200
201
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 199

def custom_types
  @custom_types
end

#registryObject (readonly)

Returns the value of attribute registry.



197
198
199
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 197

def registry
  @registry
end

Instance Method Details

#lookup(klass) ⇒ Object Also known as: map



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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 209

def lookup(klass)
  lookup_klass = klass.is_a?(Array) ? klass[0] : klass
  generated_klass = nil
  unless lookup_klass.respond_to?(:ancestors)
    raise(ProtocolError, "expected parameter type definition to be a Class")
  end
  if lookup_klass.ancestors.include?(ActiveRecord::Base)
    generated_klass = @ar2klass.has_key?(klass) ? @ar2klass[klass] : nil
    klass = generated_klass if generated_klass
  end
  return @klass2map[klass] if @klass2map.has_key?(klass)
  
  custom_type = false
  
  ruby_klass = select_class(lookup_klass)
  generated_klass = @ar2klass[lookup_klass] if @ar2klass.has_key?(lookup_klass)
  type_name = ruby_klass.name
  
  # Array signatures generate a double-mapping and require generation
  # of an Array subclass to represent the mapping in the SOAP
  # registry
  array_klass = nil
  if klass.is_a?(Array)
    array_klass = Class.new(Array) do
      module_eval <<-END
      def self.name
        "#{type_name}Array"
      end
      END
    end
  end
  
  mapping = @registry.find_mapped_soap_class(ruby_klass) rescue nil
  unless mapping
    # Custom structured type, generate a mapping
    info = { :type => XSD::QName.new(@custom_namespace, type_name) }
    @registry.add(ruby_klass,
                  SOAP::SOAPStruct, 
                  SOAP::Mapping::Registry::TypedStructFactory,
                  info)
    mapping = ensure_mapped(ruby_klass)
    custom_type = true
  end
  
  array_mapping = nil
  if array_klass
    # Typed array always requires a custom type. The info of the array
    # is the info of its element type (in mapping[2]), falling back
    # to SOAP base types.
    info = mapping[2]
    info ||= {}
    info[:type] ||= soap_base_type_qname(mapping[0])
    @registry.add(array_klass,
                  SOAP::SOAPArray,
                  SOAP::Mapping::Registry::TypedArrayFactory,
                  info)
    array_mapping = ensure_mapped(array_klass)
  end
  
  if array_mapping
    @klass2map[ruby_klass] = SoapMapping.new(self,
                                             type_name,
                                             ruby_klass,
                                             generated_klass,
                                             mapping[0],
                                             mapping,
                                             custom_type)
    @klass2map[klass] = SoapArrayMapping.new(self,
                                             type_name,
                                             array_klass,
                                             array_mapping[0],
                                             array_mapping,
                                             @klass2map[ruby_klass])
    @custom_types[klass] = @klass2map[klass]
    @custom_types[ruby_klass] = @klass2map[ruby_klass] if custom_type
  else
    @klass2map[klass] = SoapMapping.new(self,
                                        type_name,
                                        ruby_klass,
                                        generated_klass,
                                        mapping[0],
                                        mapping,
                                        custom_type)
    @custom_types[klass] = @klass2map[klass] if custom_type
  end
  
  @klass2map[klass]
end

#map_api(api) {|api_methods| ... } ⇒ Object

Yields:

  • (api_methods)


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 332

def map_api(api, &block)
  lookup_proc = lambda do |klass|
    mapping = lookup(klass)
    custom_mapping = nil
    if mapping.respond_to?(:element_mapping)
      custom_mapping = mapping.element_mapping
    else
      custom_mapping = mapping
    end
    if custom_mapping && custom_mapping.custom_type?
      # What gives? This is required so that structure types
      # referenced only by structures (and not signatures) still
      # have a custom type mapping in the registry (needed for WSDL
      # generation).
      custom_mapping.each_attribute{}
    end
    mapping 
  end
  api_methods = block.nil?? nil : {}
  api.api_methods.each do |method_name, method_info|
    expects = method_info[:expects]
    expects_signature = nil
    if expects
      expects_signature = block ? [] : nil
      expects.each do |klass|
        lookup_klass = nil
        if klass.is_a?(Hash)
          lookup_klass = lookup_proc.call(klass.values[0])
          expects_signature << {klass.keys[0]=>lookup_klass} if block
        else
          lookup_klass = lookup_proc.call(klass)
          expects_signature << lookup_klass if block
        end
      end
    end
    returns = method_info[:returns]
    returns_signature = returns ? returns.map{|klass| lookup_proc.call(klass)} : nil
    if block
      api_methods[method_name] = {
        :expects => expects_signature,
        :returns => returns_signature
      }
    end
  end
  yield api_methods if block
end

#map_container_services(container, &block) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 299

def map_container_services(container, &block)
  dispatching_mode = container.web_service_dispatching_mode
  web_services = nil
  case dispatching_mode
  when :direct
    api = container.class.web_service_api
    if container.respond_to?(:controller_class_name)
      web_service_name = container.controller_class_name.sub(/Controller$/, '').underscore
    else
      web_service_name = container.class.name.demodulize.underscore
    end
    web_services = { web_service_name => api }
  when :delegated
    web_services = {}
    container.class.web_services.each do |web_service_name, web_service_info|
      begin
        object = container.web_service_object(web_service_name)
      rescue Exception => e
        raise(ProtocolError, "failed to retrieve web service object for web service '#{web_service_name}': #{e.message}")
      end
      web_services[web_service_name] = object.class.web_service_api
    end
  end
  web_services.each do |web_service_name, api|
    if api.nil?
      raise(ProtocolError, "no web service API set while in :#{dispatching_mode} mode")
    end
    map_api(api) do |api_methods|
      yield web_service_name, api, api_methods if block_given?
    end
  end
end