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.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, resource, authenticator=None, handle_errors=1): 60 61 """ 62 Initialise the root application-specific 'resource' and optional 63 'authenticator'. The optional 'handle_errors' parameter (if true) 64 causes handlers to deal with uncaught exceptions cleanly. 65 """ 66 67 self.webstack_resource = resource 68 self.webstack_authenticator = authenticator 69 self.handle_errors = handle_errors 70 71 def service(self, request, response): 72 73 """ 74 Handle the 'request' and 'response' presented by the servlet. 75 """ 76 77 trans = WebStack.JavaServlet.Transaction(request, response) 78 try: 79 if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): 80 try: 81 self.webstack_resource.respond(trans) 82 except EndOfResponse: 83 pass 84 except: 85 if self.handle_errors: 86 trans.set_response_code(500) # Internal error 87 else: 88 raise 89 else: 90 trans.set_response_code(401) # Unauthorized 91 #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 92 # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) 93 94 finally: 95 trans.commit() 96 97 # vim: tabstop=4 expandtab shiftwidth=4