Module: ActionService::Router::Wsdl::ClassMethods

Defined in:
lib/action_service/router/wsdl.rb

Constant Summary collapse

XsdNs =
'http://www.w3.org/2001/XMLSchema'
WsdlNs =
'http://schemas.xmlsoap.org/wsdl/'
SoapNs =
'http://schemas.xmlsoap.org/wsdl/soap/'
SoapEncodingNs =
'http://schemas.xmlsoap.org/soap/encoding/'
SoapHttpTransport =
'http://schemas.xmlsoap.org/soap/http'

Instance Method Summary collapse

Instance Method Details

#to_wsdl(container, uri, soap_action_base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/action_service/router/wsdl.rb', line 16

def to_wsdl(container, uri, soap_action_base)
  wsdl = ""

  namespace = container.class.soap_mapper.custom_namespace
  wsdl_service_name = namespace.split(/:/)[1]

  mapper = ActionService::Protocol::Soap::SoapMapper.new(namespace)
  services = {}
  mapper.map_container_services(container) do |name, klass, exports|
    services[name] = [klass, exports]
  end
  custom_types = mapper.custom_types


  xm = Builder::XmlMarkup.new(:target => wsdl, :indent => 2)
  xm.instruct!

  xm.definitions('name' => wsdl_service_name,
                 'targetNamespace' => namespace,
                 'xmlns:typens'    => namespace,
                 'xmlns:xsd'       => XsdNs,
                 'xmlns:soap'      => SoapNs,
                 'xmlns:soapenc'   => SoapEncodingNs,
                 'xmlns:wsdl'      => WsdlNs,
                 'xmlns'           => WsdlNs) do

    # Custom type XSD generation
    if custom_types.size > 0
      xm.types do
        xm.xsd(:schema, 'xmlns' => XsdNs, 'targetNamespace' => namespace) do
          custom_types.each do |klass, mapping|
            case
            when mapping.is_a?(ActionService::Protocol::Soap::SoapArrayMapping)
              xm.xsd(:complexType, 'name' => mapping.type_name) do
                xm.xsd(:complexContent) do
                  xm.xsd(:restriction, 'base' => 'soapenc:Array') do
                    xm.xsd(:attribute, 'ref' => 'soapenc:arrayType',
                                       'wsdl:arrayType' => mapping.element_mapping.qualified_type_name + '[]')
                  end
                end
              end
            when mapping.is_a?(ActionService::Protocol::Soap::SoapMapping)
              xm.xsd(:complexType, 'name' => mapping.type_name) do
                xm.xsd(:all) do
                  mapping.each_attribute do |name, type_name|
                    xm.xsd(:element, 'name' => name, 'type' => type_name)
                  end
                end
              end
            else
              raise(WsdlError, "unsupported mapping type #{mapping.class.name}")
            end
          end
        end
      end
    end

    services.each do |service_name, service_values|
      service_klass, service_exports = service_values
      # Parameter list message definitions
      service_exports.each do |export_name, export_signature|
        gen = lambda do |msg_name, direction|
          xm.message('name' => msg_name) do
            sym = nil
            if direction == :out
              if export_signature[:returns]
                xm.part('name' => 'return', 'type' => export_signature[:returns][0].qualified_type_name)
              end
            else
              mapping_list = export_signature[:expects]
              i = 1
              mapping_list.each do |mapping|
                xm.part('name' => "param#{i}", 'type' => mapping.qualified_type_name)
                i += 1
              end if mapping_list
            end
          end
        end
        public_name = service_klass.public_export_name(export_name)
        gen.call(public_name, :in)
        gen.call("#{public_name}Response", :out)
      end

      # Declare the port
      port_name = port_name_for(wsdl_service_name, service_name)
      xm.portType('name' => port_name) do
        service_exports.each do |export_name, export_signature|
          public_name = service_klass.public_export_name(export_name)
          xm.operation('name' => public_name) do
            xm.input('message' => "typens:#{public_name}")
            xm.output('message' => "typens:#{public_name}Response")
          end
        end
      end

      # Bind the port to SOAP
      binding_name = binding_name_for(wsdl_service_name, service_name)
      xm.binding('name' => binding_name, 'type' => "typens:#{port_name}") do
        xm.soap(:binding, 'style' => 'rpc', 'transport' => SoapHttpTransport)
        service_exports.each do |export_name, export_signature|
          public_name = service_klass.public_export_name(export_name)
          xm.operation('name' => public_name) do
            soap_action = soap_action_base \
                        + "/" + service_name.to_s \
                        + "/" + public_name
            xm.soap(:operation, 'soapAction' => soap_action)
            xm.input do
              xm.soap(:body,
                      'use'           => 'encoded',
                      'namespace'     => namespace,
                      'encodingStyle' => SoapEncodingNs)
            end
            xm.output do
              xm.soap(:body,
                      'use'           => 'encoded',
                      'namespace'     => namespace,
                      'encodingStyle' => SoapEncodingNs)
            end
          end
        end
      end
    end

    # Define the service
    xm.service('name' => "#{wsdl_service_name}Service") do
      services.each do |service_name, service_values|
        service_klass, service_exports = service_values
        port_name = port_name_for(wsdl_service_name, service_name)
        binding_name = binding_name_for(wsdl_service_name,  service_name)
        xm.port('name' => port_name, 'binding' => "typens:#{binding_name}") do
          xm.soap(:address, 'location' => "#{uri}#{service_name.to_s}")
        end
      end
    end
  end
end