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