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