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:
8 def self.soap_client(endpoint_uri, options={})
9 ActionWebService::Client::Soap.new self, endpoint_uri, options
14 module Protocol # :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)
22 class SoapProtocol < AbstractProtocol # :nodoc:
28 def initialize(namespace=nil)
29 namespace ||= 'urn:ActionWebService'
30 @marshaler = SoapMarshaler.new namespace
33 def self.create(controller)
34 SoapProtocol.new(controller.wsdl_namespace)
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'])
42 :soap_action => soap_action,
43 :charset => input_encoding
45 decode_request(action_pack_request.raw_post, service_name, protocol_options)
48 def encode_action_pack_request(service_name, public_method_name, raw_body, options={})
50 request.env['HTTP_SOAPACTION'] = '/soap/%s/%s' % [service_name, public_method_name]
54 def decode_request(raw_request, service_name, protocol_options={})
55 envelope = SOAP::Processor.unmarshal(raw_request, :charset => protocol_options[:charset])
57 raise ProtocolError, "Failed to parse SOAP request message"
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)
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)
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)]
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)
83 def decode_response(raw_response)
84 envelope = SOAP::Processor.unmarshal(raw_response)
86 raise ProtocolError, "Failed to parse SOAP request message"
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]
94 def encode_response(method_name, return_value, return_type, protocol_options={})
96 return_binding = marshaler.register_type(return_type)
97 marshaler.annotate_arrays(return_binding, return_value)
99 qname = XSD::QName.new(marshaler.namespace, method_name)
101 response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
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))
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)
116 response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
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)
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)
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
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!(/"$/, '')
155 return nil if soap_action.empty?
159 def create_soap_envelope(body)
160 header = SOAP::SOAPHeader.new
161 body = SOAP::SOAPBody.new(body)
162 SOAP::SOAPEnvelope.new(header, body)
165 def parse_charset(content_type)
166 return AWSEncoding if content_type.nil?
167 if /^text\/xml(?:\s*;\s*charset=([^"]+|"[^"]+"))$/i =~ content_type