Went through the specs and updated them to reflect the application
[lyrix.git] / vendor / rails / actionwebservice / lib / action_web_service / protocol / soap_protocol.rb
blob1bce496a7ba54ceda8daf610433472e9a6a7b7c2
1 require 'action_web_service/protocol/soap_protocol/marshaler'
2 require 'soap/streamHandler'
3 require 'action_web_service/client/soap_client'
5 module ActionWebService # :nodoc:
6   module API # :nodoc:
7     class Base # :nodoc:
8       def self.soap_client(endpoint_uri, options={})
9         ActionWebService::Client::Soap.new self, endpoint_uri, options
10       end
11     end
12   end
14   module Protocol # :nodoc:
15     module Soap # :nodoc:
16       def self.included(base)
17         base.register_protocol(SoapProtocol)
18         base.class_inheritable_option(:wsdl_service_name)
19         base.class_inheritable_option(:wsdl_namespace)
20       end
21       
22       class SoapProtocol < AbstractProtocol # :nodoc:
23         AWSEncoding = 'UTF-8'
24         XSDEncoding = 'UTF8'
26         attr :marshaler
28         def initialize(namespace=nil)
29           namespace ||= 'urn:ActionWebService'
30           @marshaler = SoapMarshaler.new namespace
31         end
33         def self.create(controller)
34           SoapProtocol.new(controller.wsdl_namespace)
35         end
37         def decode_action_pack_request(action_pack_request)
38           return nil unless soap_action = has_valid_soap_action?(action_pack_request)
39           service_name = action_pack_request.parameters['action']
40           input_encoding = parse_charset(action_pack_request.env['HTTP_CONTENT_TYPE'])
41           protocol_options = { 
42             :soap_action => soap_action,
43             :charset  => input_encoding
44           }
45           decode_request(action_pack_request.raw_post, service_name, protocol_options)
46         end
48         def encode_action_pack_request(service_name, public_method_name, raw_body, options={})
49           request = super
50           request.env['HTTP_SOAPACTION'] = '/soap/%s/%s' % [service_name, public_method_name]
51           request
52         end
54         def decode_request(raw_request, service_name, protocol_options={})
55           envelope = SOAP::Processor.unmarshal(raw_request, :charset => protocol_options[:charset])
56           unless envelope
57             raise ProtocolError, "Failed to parse SOAP request message"
58           end
59           request = envelope.body.request
60           method_name = request.elename.name
61           params = request.collect{ |k, v| marshaler.soap_to_ruby(request[k]) }
62           Request.new(self, method_name, params, service_name, nil, nil, protocol_options)
63         end
65         def encode_request(method_name, params, param_types)
66           param_types.each{ |type| marshaler.register_type(type) } if param_types
67           qname = XSD::QName.new(marshaler.namespace, method_name)
68           param_def = []
69           if param_types
70             params = param_types.zip(params).map do |type, param|
71               param_def << ['in', type.name, marshaler.lookup_type(type).mapping]
72               [type.name, marshaler.ruby_to_soap(param)]
73             end
74           else
75             params = []
76           end
77           request = SOAP::RPC::SOAPMethodRequest.new(qname, param_def)
78           request.set_param(params)
79           envelope = create_soap_envelope(request)
80           SOAP::Processor.marshal(envelope)
81         end
83         def decode_response(raw_response)
84           envelope = SOAP::Processor.unmarshal(raw_response)
85           unless envelope
86             raise ProtocolError, "Failed to parse SOAP request message"
87           end
88           method_name = envelope.body.request.elename.name
89           return_value = envelope.body.response
90           return_value = marshaler.soap_to_ruby(return_value) unless return_value.nil?
91           [method_name, return_value]
92         end
94         def encode_response(method_name, return_value, return_type, protocol_options={})
95           if return_type
96             return_binding = marshaler.register_type(return_type)
97             marshaler.annotate_arrays(return_binding, return_value)
98           end
99           qname = XSD::QName.new(marshaler.namespace, method_name)
100           if return_value.nil?
101             response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
102           else
103             if return_value.is_a?(Exception)
104               detail = SOAP::Mapping::SOAPException.new(return_value)
105               response = SOAP::SOAPFault.new(
106                 SOAP::SOAPQName.new('%s:%s' % [SOAP::SOAPNamespaceTag, 'Server']),
107                 SOAP::SOAPString.new(return_value.to_s),
108                 SOAP::SOAPString.new(self.class.name),
109                 marshaler.ruby_to_soap(detail))
110             else
111               if return_type
112                 param_def = [['retval', 'return', marshaler.lookup_type(return_type).mapping]]
113                 response = SOAP::RPC::SOAPMethodResponse.new(qname, param_def)
114                 response.retval = marshaler.ruby_to_soap(return_value)
115               else
116                 response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
117               end
118             end
119           end
120           envelope = create_soap_envelope(response)
122           # FIXME: This is not thread-safe, but StringFactory_ in SOAP4R only
123           #        reads target encoding from the XSD::Charset.encoding variable.
124           #        This is required to ensure $KCODE strings are converted
125           #        correctly to UTF-8 for any values of $KCODE.
126           previous_encoding = XSD::Charset.encoding
127           XSD::Charset.encoding = XSDEncoding
128           response_body = SOAP::Processor.marshal(envelope, :charset => AWSEncoding)
129           XSD::Charset.encoding = previous_encoding
131           Response.new(response_body, "text/xml; charset=#{AWSEncoding}", return_value)
132         end
134         def protocol_client(api, protocol_name, endpoint_uri, options={})
135           return nil unless protocol_name == :soap
136           ActionWebService::Client::Soap.new(api, endpoint_uri, options)
137         end
139         def register_api(api)
140           api.api_methods.each do |name, method|
141             method.expects.each{ |type| marshaler.register_type(type) } if method.expects
142             method.returns.each{ |type| marshaler.register_type(type) } if method.returns
143           end
144         end
146         private
147           def has_valid_soap_action?(request)
148             return nil unless request.method == :post
149             soap_action = request.env['HTTP_SOAPACTION']
150             return nil unless soap_action
151             soap_action = soap_action.dup
152             soap_action.gsub!(/^"/, '')
153             soap_action.gsub!(/"$/, '')
154             soap_action.strip!
155             return nil if soap_action.empty?
156             soap_action
157           end
159           def create_soap_envelope(body)
160             header = SOAP::SOAPHeader.new
161             body = SOAP::SOAPBody.new(body)
162             SOAP::SOAPEnvelope.new(header, body)
163           end
165           def parse_charset(content_type)
166             return AWSEncoding if content_type.nil?
167             if /^text\/xml(?:\s*;\s*charset=([^"]+|"[^"]+"))$/i =~ content_type
168               $1
169             else
170               AWSEncoding
171             end
172           end
173       end
174     end
175   end