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