paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | """ |
paulb@4 | 4 | Webware adapter. |
paulb@4 | 5 | """ |
paulb@4 | 6 | |
paulb@4 | 7 | import WebStack.Webware |
paulb@4 | 8 | |
paulb@32 | 9 | # For Webware releases later than 0.8.1, employ special URLParsers in contexts |
paulb@32 | 10 | # for each application in the application server; such parsers create servlets |
paulb@32 | 11 | # instead of having servlet factories do that work. |
paulb@32 | 12 | |
paulb@32 | 13 | try: |
paulb@32 | 14 | from WebKit.URLParser import URLParser |
paulb@32 | 15 | |
paulb@32 | 16 | except ImportError: |
paulb@32 | 17 | |
paulb@32 | 18 | # NOTE: Using Webware 0.8.1 or earlier. Assume that this really is the case. |
paulb@32 | 19 | |
paulb@32 | 20 | pass |
paulb@32 | 21 | |
paulb@32 | 22 | else: |
paulb@32 | 23 | class WebStackURLParser(URLParser): |
paulb@32 | 24 | |
paulb@32 | 25 | """ |
paulb@32 | 26 | A custom URL parser which provides access to application-specific resources. |
paulb@32 | 27 | Override the 'parse' method for more precise control of servlet |
paulb@32 | 28 | instantiation. |
paulb@32 | 29 | """ |
paulb@32 | 30 | |
paulb@76 | 31 | def __init__(self, resource, authenticator=None): |
paulb@32 | 32 | |
paulb@32 | 33 | """ |
paulb@32 | 34 | Initialise the parser object with the given root application-specific |
paulb@76 | 35 | 'resource' and optional 'authenticator'. |
paulb@32 | 36 | """ |
paulb@32 | 37 | |
paulb@32 | 38 | self.webstack_resource = resource |
paulb@76 | 39 | self.webstack_authenticator = authenticator |
paulb@32 | 40 | |
paulb@32 | 41 | def parse(self, trans, requestPath): |
paulb@32 | 42 | |
paulb@32 | 43 | """ |
paulb@32 | 44 | For the given Webware transaction, 'trans', override the usual servlet |
paulb@32 | 45 | factory mechanism and return a servlet which will provide access to the |
paulb@32 | 46 | application-specific resources. |
paulb@32 | 47 | The 'trans' object - a Webware transaction - is not given to the servlet |
paulb@32 | 48 | since such information is available when the 'respond' method is invoked |
paulb@32 | 49 | on the servlet. |
paulb@32 | 50 | The provided 'requestPath' object is not used, since this information |
paulb@32 | 51 | should be available elsewhere. |
paulb@32 | 52 | """ |
paulb@32 | 53 | |
paulb@76 | 54 | return WebStackServlet(self.webstack_resource, self.webstack_authenticator) |
paulb@32 | 55 | |
paulb@32 | 56 | # For Webware 0.8.1 and earlier, employ servlet factories and servlets. |
paulb@15 | 57 | |
paulb@15 | 58 | from WebKit.ServletFactory import ServletFactory |
paulb@15 | 59 | from WebKit.Servlet import Servlet |
paulb@15 | 60 | |
paulb@15 | 61 | class WebStackServletFactory(ServletFactory): |
paulb@15 | 62 | |
paulb@15 | 63 | """ |
paulb@15 | 64 | A servlet factory object producing servlets which provide access to |
paulb@15 | 65 | application-specific resources. |
paulb@15 | 66 | """ |
paulb@15 | 67 | |
paulb@76 | 68 | def __init__(self, application, resource, file_extensions, authenticator=None): |
paulb@15 | 69 | |
paulb@15 | 70 | """ |
paulb@15 | 71 | Initialise the servlet factory with the Webware 'application' and the |
paulb@15 | 72 | WebStack root application-specific 'resource'. The 'file_extensions' |
paulb@76 | 73 | specified indicate for which files this factory is invoked. An optional |
paulb@76 | 74 | 'authenticator' is used to control access to the resource. |
paulb@15 | 75 | """ |
paulb@15 | 76 | |
paulb@15 | 77 | ServletFactory.__init__(self, application) |
paulb@15 | 78 | self.webstack_resource = resource |
paulb@15 | 79 | self.file_extensions = file_extensions |
paulb@76 | 80 | self.webstack_authenticator = authenticator |
paulb@15 | 81 | |
paulb@15 | 82 | def uniqueness(self): |
paulb@15 | 83 | |
paulb@15 | 84 | """ |
paulb@15 | 85 | Return "file" uniqueness - probably the most appropriate response. |
paulb@15 | 86 | """ |
paulb@15 | 87 | |
paulb@15 | 88 | return "file" |
paulb@15 | 89 | |
paulb@15 | 90 | def extensions(self): |
paulb@15 | 91 | |
paulb@15 | 92 | """ |
paulb@15 | 93 | Return the file extensions supported by this factory. |
paulb@15 | 94 | """ |
paulb@15 | 95 | |
paulb@15 | 96 | return self.file_extensions |
paulb@15 | 97 | |
paulb@15 | 98 | def servletForTransaction(self, trans): |
paulb@15 | 99 | |
paulb@15 | 100 | """ |
paulb@15 | 101 | Return a servlet which will provide access to the application-specific |
paulb@15 | 102 | resources. The 'trans' object - a Webware transaction - is not given to |
paulb@15 | 103 | the servlet since such information is available when the 'respond' |
paulb@15 | 104 | method is invoked on the servlet. |
paulb@15 | 105 | """ |
paulb@15 | 106 | |
paulb@76 | 107 | return WebStackServlet(self.webstack_resource, self.webstack_authenticator) |
paulb@15 | 108 | |
paulb@32 | 109 | # Servlets are common to both solutions. |
paulb@32 | 110 | |
paulb@15 | 111 | class WebStackServlet(Servlet): |
paulb@15 | 112 | |
paulb@15 | 113 | "A servlet which dispatches transactions to application-specific resources." |
paulb@15 | 114 | |
paulb@76 | 115 | def __init__(self, resource, authenticator): |
paulb@15 | 116 | |
paulb@76 | 117 | """ |
paulb@76 | 118 | Initialise the servlet with an application-specific 'resource' and |
paulb@76 | 119 | 'authenticator'. |
paulb@76 | 120 | """ |
paulb@15 | 121 | |
paulb@32 | 122 | Servlet.__init__(self) |
paulb@15 | 123 | self.webstack_resource = resource |
paulb@76 | 124 | self.webstack_authenticator = authenticator |
paulb@15 | 125 | |
paulb@15 | 126 | def respond(self, trans): |
paulb@15 | 127 | |
paulb@15 | 128 | """ |
paulb@15 | 129 | Respond to the incoming transaction, 'trans', by dispatching to the |
paulb@15 | 130 | application-specific resource. |
paulb@15 | 131 | """ |
paulb@15 | 132 | |
paulb@15 | 133 | new_trans = WebStack.Webware.Transaction(trans) |
paulb@76 | 134 | if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(new_trans): |
paulb@76 | 135 | self.webstack_resource.respond(new_trans) |
paulb@76 | 136 | else: |
paulb@76 | 137 | new_trans.set_response_code(401) # Unauthorized |
paulb@76 | 138 | new_trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@76 | 139 | self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm())) |
paulb@76 | 140 | |
paulb@29 | 141 | new_trans.commit() |
paulb@4 | 142 | |
paulb@4 | 143 | # vim: tabstop=4 expandtab shiftwidth=4 |