1 #!/usr/bin/env python 2 3 """ 4 Zope adapter. 5 6 Copyright (C) 2004, 2005 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.Zope 24 from WebStack.Generic import EndOfResponse 25 26 from Globals import Persistent 27 from OFS.SimpleItem import Item 28 from AccessControl import ClassSecurityInfo 29 import Acquisition 30 #from ZPublisher.HTTPResponse import status_reasons 31 32 class WebStackAdapterProduct(Persistent, Acquisition.Implicit, Item): 33 34 "A WebStack adapter product superclass." 35 36 security = ClassSecurityInfo() 37 security.declareObjectProtected("View") 38 security.declareProtected("View", "index_html") 39 40 def __init__(self, id, resource, authenticator=None, handle_errors=1): 41 42 """ 43 Initialise with an 'id', a WebStack 'resource', and an optional 44 'authenticator'. The optional 'handle_errors' parameter (if true) causes 45 handlers to deal with uncaught exceptions cleanly. 46 """ 47 48 self.id = id 49 self.webstack_resource = resource 50 self.webstack_authenticator = authenticator 51 self.handle_errors = handle_errors 52 53 def __bobo_traverse__(self, request, entry_name): 54 if entry_name == "index_html": 55 return getattr(self, "index_html") 56 return self 57 58 def index_html(self, REQUEST=None): 59 60 """ 61 Dispatch the given 'REQUEST' to the root application-specific WebStack 62 resource. 63 """ 64 65 # Provide the adapter so that "path info" can be determined correctly. 66 67 if REQUEST is not None: 68 trans = WebStack.Zope.Transaction(REQUEST, self) 69 else: 70 raise "Internal Error" 71 72 try: 73 if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): 74 try: 75 self.webstack_resource.respond(trans) 76 except EndOfResponse: 77 pass 78 except: 79 if self.handle_errors: 80 trans.set_response_code(500) # Internal error 81 else: 82 raise 83 else: 84 #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 85 # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) 86 raise "Unauthorized" 87 88 finally: 89 trans.commit() 90 #raise status_reasons[trans.get_response_code()] 91 92 # vim: tabstop=4 expandtab shiftwidth=4