1 #!/usr/bin/env python 2 3 """ 4 Twisted adapter. 5 """ 6 7 import WebStack.Twisted 8 import twisted.web.resource 9 10 class Dispatcher(twisted.web.resource.Resource): 11 12 "A class dispatching requests to WebStack resources." 13 14 isLeaf = 1 15 16 def __init__(self, resource, authenticator=None): 17 18 """ 19 Initialise the root application-specific 'resource' and optional 20 'authenticator'. 21 """ 22 23 twisted.web.resource.Resource.__init__(self) 24 self.webstack_resource = resource 25 self.webstack_authenticator = authenticator 26 27 def getChild(self, path, request): 28 raise NotImplementedError, "getChild" 29 30 def render(self, request): 31 32 "Dispatch the given 'request' to the root application-specific resource." 33 34 trans = WebStack.Twisted.Transaction(request) 35 if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans): 36 self.webstack_resource.respond(trans) 37 else: 38 trans.set_response_code(401) # Unauthorized 39 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 40 self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) 41 42 trans.commit() 43 request.finish() 44 return twisted.web.server.NOT_DONE_YET 45 46 # vim: tabstop=4 expandtab shiftwidth=4