Class: RubyAMF::Envelope

Inherits:
RocketAMF::Envelope
  • Object
show all
Defined in:
lib/rubyamf/envelope.rb

Overview

Adds several important features to RocketAMF::Envelope. None of these features are dependent on Rails, and as such can be used by any Rack compliant framework. Features are credentials support, easy parameter mapping based on configured parameter mappings, mapping scope support for serialization, and error handling for method dispatch using each_method_call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mapping_scopeObject

Returns the value of attribute mapping_scope.



10
11
12
# File 'lib/rubyamf/envelope.rb', line 10

def mapping_scope
  @mapping_scope
end

Instance Method Details

#credentialsObject

Finds and returns credentials set on the request as a hash with keys username and password, with the type dependent on the hash_key_access setting. setHeader('Credentials') credentials are used first, followed by new-style DSRemoteCredentials. If no credentials are found, a hash is returned with a username and password of nil.



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
# File 'lib/rubyamf/envelope.rb', line 18

def credentials
  ds_cred_key = RubyAMF.configuration.translate_case ? "ds_remote_credentials" : "DSRemoteCredentials"
  if RubyAMF.configuration.hash_key_access == :symbol
    userid_key = :userid
    username_key = :username
    password_key = :password
    ds_cred_key = ds_cred_key.to_sym
  else
    userid_key = "userid"
    username_key = "username"
    password_key = "password"
  end

  # Old style setHeader('Credentials', CREDENTIALS_HASH)
  if @headers['Credentials']
    h = @headers['Credentials']
    return {username_key => h.data[userid_key], password_key => h.data[password_key]}
  end

  # New style DSRemoteCredentials
  messages.each do |m|
    if m.data.is_a?(RocketAMF::Values::RemotingMessage)
      if m.data.headers && m.data.headers[ds_cred_key]
        username,password = Base64.decode64(m.data.headers[ds_cred_key]).split(':')
        return {username_key => username, password_key => password}
      end
    end
  end

  # Failure case sends empty credentials, because rubyamf_plugin does it
  {username_key => nil, password_key => nil}
end

#dispatch_call(p) ⇒ Object

Extends default RocketAMF implementation to log caught exceptions and translate them into a RocketAMF::Values::ErrorMessage for return to flash after removing the backtrace (for safety).



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubyamf/envelope.rb', line 78

def dispatch_call p
  begin
    ret = p[:block].call(p[:method], p[:args])
    raise ret if ret.is_a?(Exception) # If they return FaultObject like you could in rubyamf_plugin
    ret
  rescue Exception => e
    # Log exception
    RubyAMF.logger.log_error(e)

    # Clear backtrace so that RocketAMF doesn't send back the full backtrace
    e.set_backtrace([])

    # Create ErrorMessage object using the source message as the base
    RocketAMF::Values::ErrorMessage.new(p[:source], e)
  end
end

#params_hash(controller, action, arguments) ⇒ Object

Given a controller, action, and the flash arguments array, returns a hash containing the arguments indexed by number as well as named key if a named mapping has been configured. Returned hash respects hash_key_access setting for named keys.

Example:

RubyAMF.configuration.map_params "c", "a", ["param1", "param2"]
params = envelope.params_hash "c", "a", ["asdf", "fdsa"]
params.should == {:param1 => "asdf", :param2 => "fdsa", 0 => "asdf", 1 => "fdsa"}


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubyamf/envelope.rb', line 61

def params_hash controller, action, arguments
  conf = RubyAMF.configuration
  mapped = {}
  mapping = conf.param_mappings[controller+"#"+action]
  arguments.each_with_index do |arg, i|
    mapped[i] = arg
    if mapping && mapping[i]
      mapping_key = conf.hash_key_access == :symbol ? mapping[i].to_sym : mapping[i].to_s
      mapped[mapping_key] = arg
    end
  end
  mapped
end

#serialize(class_mapper = nil) ⇒ Object

:nodoc:



95
96
97
98
99
100
# File 'lib/rubyamf/envelope.rb', line 95

def serialize class_mapper=nil #:nodoc:
  # Create a ClassMapper and set the mapping scope to pass to super implementation.
  cm = class_mapper || RubyAMF::ClassMapper.new
  cm.mapping_scope = mapping_scope if cm.respond_to?(:mapping_scope=)
  super cm
end