Class: Trinidad::Rack::RackServlet

Inherits:
HttpServlet
  • Object
show all
Defined in:
lib/trinidad_rack/rack_servlet.rb

Instance Method Summary collapse

Instance Method Details

#rackup(app) ⇒ Object

Sets the Rack application that handles requests sent to this servlet container.



21
22
23
# File 'lib/trinidad_rack/rack_servlet.rb', line 21

def rackup(app)
    @app = app
end

#service(request, response) ⇒ Object

Takes an incoming request (as a Java Servlet) and dispatches it to the rack application setup via [rackup]. All this really involves is translating the various bits of the Servlet API into the Rack API on the way in, and translating the response back on the way out.

Also, we implement a common extension to the Rack api for asynchronous request processing. We supply an ‘async.callback’ parameter in env to the Rack application. If we catch an :async symbol thrown by the app, we initiate a Jetty continuation.

When ‘async.callback’ gets a response with empty headers and an empty body, we declare the async response finished.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trinidad_rack/rack_servlet.rb', line 40

def service(request, response)
    # Turn the ServletRequest into a Rack env hash
    env = servlet_to_rack(request)

    # Add our own special bits to the rack environment so that
    # Rack middleware can have access to the Java internals.
    env['rack.java.servlet'] = true
    env['rack.java.servlet.request'] = request
    env['rack.java.servlet.response'] = response

    rack_response = @app.call(env)

    # For apps that don't throw :async.
    unless(rack_response[0] == -1)
        # Nope, nothing asynchronous here.
        rack_to_servlet(rack_response, response)
        return
    end
end