Class: ArchivesSpaceService::RequestWrappingMiddleware

Inherits:
Object
  • Object
show all
Defined in:
backend/app/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestWrappingMiddleware

Returns a new instance of RequestWrappingMiddleware.



284
285
286
# File 'backend/app/main.rb', line 284

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'backend/app/main.rb', line 288

def call(env)
  start_time = Time.now
  session_token = env["HTTP_X_ARCHIVESSPACE_SESSION"]

  session = nil

  if session_token
    session = Session.find(session_token)

    if session.nil?
      return [412,
              {"Content-Type" => "application/json"},
              [{
                 :code => "SESSION_GONE",
                 :error => "No session found for #{session_token}"
               }.to_json]]
    end

    if session[:expirable] &&
        AppConfig[:session_expire_after_seconds].to_i >= 0 &&
        session.age > AppConfig[:session_expire_after_seconds].to_i

      Session.expire(session_token)
      session = nil
      return [412,
              {"Content-Type" => "application/json"},
              [{
                 :code => "SESSION_EXPIRED",
                 :error => "Session timed out for #{session_token}"
               }.to_json]]
    else
      session.touch
    end
  end


  env[:aspace_user] = ANONYMOUS_USER

  if session
    env[:aspace_session] = session
    env[:aspace_user] = ((session && session[:user] && User.find(:username => session[:user])) ||
                         ANONYMOUS_USER)
  end

  querystring = env['QUERY_STRING'].empty? ? "" : "?#{Log.filter_passwords(env['QUERY_STRING'])}"

  Log.debug("#{env['REQUEST_METHOD']} #{env['PATH_INFO']}#{querystring} [session: #{session.inspect}]")
  result = @app.call(env)

  end_time = Time.now

  Log.debug("Responded with #{result.to_s[0..512]}... in #{((end_time - start_time) * 1000).to_i}ms")

  result
end