Class: TorqueBox::Session::ServletStore

Inherits:
Object
  • Object
show all
Defined in:
web/lib/torquebox/session/servlet_store.rb

Direct Known Subclasses

ActionController::Session::TorqueBoxStore, ActionDispatch::Session::TorqueBoxStore

Constant Summary

RAILS_SESSION_KEY =
'__current_rails_session'

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ServletStore) initialize(app, options = {})

Returns a new instance of ServletStore



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'web/lib/torquebox/session/servlet_store.rb', line 24

def initialize(app, options={})
  @app = app

  if ServletStore.torquebox_available?
    web_context = TorqueBox::MSC.java_web_context
    web_context.session_timeout = options[:timeout] / 60 if options[:timeout]

    cookie_config = web_context.session_cookie
    cookie_config.name = options[:key] if options[:key]
    cookie_config.domain = options[:domain] if options[:domain]
    cookie_config.path = options[:path] if options[:path]
    cookie_config.http_only = !!options[:httponly]
    cookie_config.secure = !!options[:secure]
    cookie_config.max_age = options[:max_age] if options[:max_age]
  else
    # Fallback to just storing session data in a hash if we're
    # running outside of TorqueBox
    @test_session_data = SessionData.new
  end
end

Class Method Details

+ (Object) commit_session(env, status, headers, body)



66
67
68
69
70
71
72
73
74
# File 'web/lib/torquebox/session/servlet_store.rb', line 66

def self.commit_session(env, status, headers, body)
  session_data = env['rack.session' ]
  if torquebox_available?
    ServletStore.store_session_data(env['java.servlet_request'].getSession(true),
                                    session_data) unless session_data.empty?
  else
    @test_session_data = session_data
  end
end

+ (Object) load_session(env)



56
57
58
59
60
61
62
63
64
# File 'web/lib/torquebox/session/servlet_store.rb', line 56

def self.load_session(env)
  if torquebox_available?
    session_data = load_session_data(env['java.servlet_request'].getSession(false))
  else
    session_data = @test_session_data
  end
  env['rack.session'] = session_data
  env['rack.session.options' ] = {}
end

+ (Object) load_session_data(session)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'web/lib/torquebox/session/servlet_store.rb', line 76

def self.load_session_data(session)
  session_data = SessionData.new
  if session
    session_data.java_session = session
    session.getAttributeNames.each do |key|
      if ( key == RAILS_SESSION_KEY )
        marshalled_bytes = session.getAttribute(RAILS_SESSION_KEY)
        if ( marshalled_bytes )
          data = Marshal.load( String.from_java_bytes( marshalled_bytes ) )
          session_data.update( data ) if Hash === data
        end
      else
        session_data[key] = session.getAttribute(key)
      end
    end
    initial_keys = session_data.keys
    session_data[:session_id] = session.getId()
    session_data['TORQUEBOX_INITIAL_KEYS'] = initial_keys
  end
  session_data
end

+ (Object) store_session_data(session, session_data)



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
# File 'web/lib/torquebox/session/servlet_store.rb', line 98

def self.store_session_data(session, session_data)
  hash = session_data.to_hash
  # java session shouldn't be marshalled
  hash.java_session = nil if hash.respond_to?(:java_session=)
  initial_keys = hash['TORQUEBOX_INITIAL_KEYS'] || []
  removed_keys = initial_keys - hash.keys
  hash.delete('TORQUEBOX_INITIAL_KEYS')
  hash.delete_if do |key,value|
    # I don't think this guard is really necessary
    if ( Symbol === key or String === key)
      key = key.to_s
      case value
        when String, Numeric, true, false, nil
          session.setAttribute( key, value )
          true
      else
        if value.respond_to?(:java_object)
          session.setAttribute( key, value )
          true
        else
          false
        end
      end
    end
  end
  unless hash.empty?
    marshalled_string = Marshal.dump(hash)
    marshalled_bytes = marshalled_string.to_java_bytes
    session.setAttribute(RAILS_SESSION_KEY, marshalled_bytes)
  end
  removed_keys.each do |k|
    session.removeAttribute( k.to_s )
  end
end

+ (Boolean) torquebox_available?

Returns:

  • (Boolean)


52
53
54
# File 'web/lib/torquebox/session/servlet_store.rb', line 52

def self.torquebox_available?
  defined? TORQUEBOX_APP_NAME
end

Instance Method Details

- (Object) call(env)



45
46
47
48
49
50
# File 'web/lib/torquebox/session/servlet_store.rb', line 45

def call(env)
  ServletStore.load_session(env)
  status, headers, body = @app.call(env)
  ServletStore.commit_session(env, status, headers, body)
  return [ status, headers, body ]
end