paulb@92 | 1 | #!/usr/bin/env python |
paulb@92 | 2 | |
paulb@92 | 3 | """ |
paulb@92 | 4 | Java Servlet adapter. |
paulb@403 | 5 | |
paulb@562 | 6 | Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@92 | 21 | """ |
paulb@92 | 22 | |
paulb@96 | 23 | import WebStack.JavaServlet |
paulb@297 | 24 | from WebStack.Generic import EndOfResponse |
paulb@554 | 25 | from javax.servlet.http import HttpServlet |
paulb@554 | 26 | |
paulb@554 | 27 | def deploy(resource, authenticator=None, address=None, handle_errors=1): |
paulb@554 | 28 | |
paulb@554 | 29 | """ |
paulb@554 | 30 | Deploy the given 'resource' and optional 'authenticator'. |
paulb@554 | 31 | The optional 'handle_errors' flag, if set to false, causes tracebacks to be |
paulb@554 | 32 | displayed in the browser. |
paulb@554 | 33 | |
paulb@554 | 34 | The optional 'address' parameter is deliberately ignored. |
paulb@554 | 35 | """ |
paulb@554 | 36 | |
paulb@554 | 37 | # NOTE: Since jython 2.1 does not support the nested scope mechanisms |
paulb@554 | 38 | # NOTE: required to just drop the above parameters into methods even as |
paulb@554 | 39 | # NOTE: default parameters, we employ class attributes. |
paulb@554 | 40 | |
paulb@554 | 41 | class _Deploy(HttpServlet): |
paulb@554 | 42 | def __init__(self): |
paulb@554 | 43 | HttpServlet.__init__(self) |
paulb@554 | 44 | self.dispatcher = Dispatcher(self.resource, self.authenticator, self.handle_errors) |
paulb@554 | 45 | |
paulb@554 | 46 | def service(self, request, response): |
paulb@554 | 47 | self.dispatcher.service(request, response) |
paulb@554 | 48 | |
paulb@554 | 49 | _Deploy.resource = resource |
paulb@554 | 50 | _Deploy.authenticator = authenticator |
paulb@554 | 51 | _Deploy.handle_errors = handle_errors |
paulb@554 | 52 | |
paulb@554 | 53 | return _Deploy |
paulb@92 | 54 | |
paulb@92 | 55 | class Dispatcher: |
paulb@92 | 56 | |
paulb@92 | 57 | "A servlet helper class dispatching requests to WebStack resources." |
paulb@92 | 58 | |
paulb@310 | 59 | def __init__(self, resource, authenticator=None, handle_errors=1): |
paulb@92 | 60 | |
paulb@92 | 61 | """ |
paulb@92 | 62 | Initialise the root application-specific 'resource' and optional |
paulb@310 | 63 | 'authenticator'. The optional 'handle_errors' parameter (if true) |
paulb@310 | 64 | causes handlers to deal with uncaught exceptions cleanly. |
paulb@92 | 65 | """ |
paulb@92 | 66 | |
paulb@92 | 67 | self.webstack_resource = resource |
paulb@92 | 68 | self.webstack_authenticator = authenticator |
paulb@310 | 69 | self.handle_errors = handle_errors |
paulb@92 | 70 | |
paulb@92 | 71 | def service(self, request, response): |
paulb@92 | 72 | |
paulb@92 | 73 | """ |
paulb@92 | 74 | Handle the 'request' and 'response' presented by the servlet. |
paulb@92 | 75 | """ |
paulb@92 | 76 | |
paulb@92 | 77 | trans = WebStack.JavaServlet.Transaction(request, response) |
paulb@510 | 78 | try: |
paulb@510 | 79 | if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): |
paulb@510 | 80 | try: |
paulb@510 | 81 | self.webstack_resource.respond(trans) |
paulb@510 | 82 | except EndOfResponse: |
paulb@510 | 83 | pass |
paulb@510 | 84 | except: |
paulb@510 | 85 | if self.handle_errors: |
paulb@510 | 86 | trans.set_response_code(500) # Internal error |
paulb@510 | 87 | else: |
paulb@510 | 88 | raise |
paulb@510 | 89 | else: |
paulb@510 | 90 | trans.set_response_code(401) # Unauthorized |
paulb@510 | 91 | #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@510 | 92 | # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) |
paulb@92 | 93 | |
paulb@510 | 94 | finally: |
paulb@510 | 95 | trans.commit() |
paulb@92 | 96 | |
paulb@92 | 97 | # vim: tabstop=4 expandtab shiftwidth=4 |