paulb@92 | 1 | #!/usr/bin/env python |
paulb@92 | 2 | |
paulb@92 | 3 | """ |
paulb@92 | 4 | Java Servlet adapter. |
paulb@92 | 5 | """ |
paulb@92 | 6 | |
paulb@96 | 7 | import WebStack.JavaServlet |
paulb@297 | 8 | from WebStack.Generic import EndOfResponse |
paulb@92 | 9 | |
paulb@92 | 10 | class Dispatcher: |
paulb@92 | 11 | |
paulb@92 | 12 | "A servlet helper class dispatching requests to WebStack resources." |
paulb@92 | 13 | |
paulb@310 | 14 | def __init__(self, resource, authenticator=None, handle_errors=1): |
paulb@92 | 15 | |
paulb@92 | 16 | """ |
paulb@92 | 17 | Initialise the root application-specific 'resource' and optional |
paulb@310 | 18 | 'authenticator'. The optional 'handle_errors' parameter (if true) |
paulb@310 | 19 | causes handlers to deal with uncaught exceptions cleanly. |
paulb@92 | 20 | """ |
paulb@92 | 21 | |
paulb@92 | 22 | self.webstack_resource = resource |
paulb@92 | 23 | self.webstack_authenticator = authenticator |
paulb@310 | 24 | self.handle_errors = handle_errors |
paulb@92 | 25 | |
paulb@92 | 26 | def service(self, request, response): |
paulb@92 | 27 | |
paulb@92 | 28 | """ |
paulb@92 | 29 | Handle the 'request' and 'response' presented by the servlet. |
paulb@92 | 30 | """ |
paulb@92 | 31 | |
paulb@92 | 32 | trans = WebStack.JavaServlet.Transaction(request, response) |
paulb@92 | 33 | if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): |
paulb@297 | 34 | try: |
paulb@297 | 35 | self.webstack_resource.respond(trans) |
paulb@297 | 36 | except EndOfResponse: |
paulb@297 | 37 | pass |
paulb@310 | 38 | except: |
paulb@310 | 39 | if self.handle_errors: |
paulb@310 | 40 | trans.set_response_code(500) # Internal error |
paulb@310 | 41 | else: |
paulb@310 | 42 | raise |
paulb@92 | 43 | else: |
paulb@92 | 44 | trans.set_response_code(401) # Unauthorized |
paulb@286 | 45 | #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@286 | 46 | # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) |
paulb@92 | 47 | |
paulb@92 | 48 | trans.commit() |
paulb@92 | 49 | |
paulb@92 | 50 | # vim: tabstop=4 expandtab shiftwidth=4 |