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