Method: ActionWebService::Protocol::Soap::SoapProtocol#encode_response

Defined in:
lib/action_web_service/protocol/soap_protocol.rb

#encode_response(method_name, return_value, return_type, protocol_options = {}) ⇒ Object



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
# File 'lib/action_web_service/protocol/soap_protocol.rb', line 94

def encode_response(method_name, return_value, return_type, protocol_options={})
  if return_type
    return_binding = marshaler.register_type(return_type)
    marshaler.annotate_arrays(return_binding, return_value)
  end
  qname = XSD::QName.new(marshaler.namespace, method_name)
  if return_value.nil?
    response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
  else
    if return_value.is_a?(Exception)
      detail = SOAP::Mapping::SOAPException.new(return_value)
      response = SOAP::SOAPFault.new(
        SOAP::SOAPQName.new('%s:%s' % [SOAP::SOAPNamespaceTag, 'Server']),
        SOAP::SOAPString.new(return_value.to_s),
        SOAP::SOAPString.new(self.class.name),
        marshaler.ruby_to_soap(detail))
    else
      if return_type
        param_def = [['retval', 'return', marshaler.lookup_type(return_type).mapping]]
        response = SOAP::RPC::SOAPMethodResponse.new(qname, param_def)
        response.retval = marshaler.ruby_to_soap(return_value)
      else
        response = SOAP::RPC::SOAPMethodResponse.new(qname, nil)
      end
    end
  end
  envelope = create_soap_envelope(response)

  # FIXME: This is not thread-safe, but StringFactory_ in SOAP4R only
  #        reads target encoding from the XSD::Charset.encoding variable.
  #        This is required to ensure $KCODE strings are converted
  #        correctly to UTF-8 for any values of $KCODE.
  previous_encoding = XSD::Charset.encoding
  XSD::Charset.encoding = XSDEncoding
  response_body = SOAP::Processor.marshal(envelope, :charset => AWSEncoding)
  XSD::Charset.encoding = previous_encoding

  Response.new(response_body, "text/xml; charset=#{AWSEncoding}", return_value)
end