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