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@32 | 31 | def __init__(self, resource): |
paulb@32 | 32 | |
paulb@32 | 33 | """ |
paulb@32 | 34 | Initialise the parser object with the given root application-specific |
paulb@32 | 35 | 'resource'. |
paulb@32 | 36 | """ |
paulb@32 | 37 | |
paulb@32 | 38 | self.webstack_resource = resource |
paulb@32 | 39 | |
paulb@32 | 40 | def parse(self, trans, requestPath): |
paulb@32 | 41 | |
paulb@32 | 42 | """ |
paulb@32 | 43 | For the given Webware transaction, 'trans', override the usual servlet |
paulb@32 | 44 | factory mechanism and return a servlet which will provide access to the |
paulb@32 | 45 | application-specific resources. |
paulb@32 | 46 | The 'trans' object - a Webware transaction - is not given to the servlet |
paulb@32 | 47 | since such information is available when the 'respond' method is invoked |
paulb@32 | 48 | on the servlet. |
paulb@32 | 49 | The provided 'requestPath' object is not used, since this information |
paulb@32 | 50 | should be available elsewhere. |
paulb@32 | 51 | """ |
paulb@32 | 52 | |
paulb@32 | 53 | return WebStackServlet(self.webstack_resource) |
paulb@32 | 54 | |
paulb@32 | 55 | # For Webware 0.8.1 and earlier, employ servlet factories and servlets. |
paulb@15 | 56 | |
paulb@15 | 57 | from WebKit.ServletFactory import ServletFactory |
paulb@15 | 58 | from WebKit.Servlet import Servlet |
paulb@15 | 59 | |
paulb@15 | 60 | class WebStackServletFactory(ServletFactory): |
paulb@15 | 61 | |
paulb@15 | 62 | """ |
paulb@15 | 63 | A servlet factory object producing servlets which provide access to |
paulb@15 | 64 | application-specific resources. |
paulb@15 | 65 | """ |
paulb@15 | 66 | |
paulb@15 | 67 | def __init__(self, application, resource, file_extensions): |
paulb@15 | 68 | |
paulb@15 | 69 | """ |
paulb@15 | 70 | Initialise the servlet factory with the Webware 'application' and the |
paulb@15 | 71 | WebStack root application-specific 'resource'. The 'file_extensions' |
paulb@15 | 72 | specified indicate for which files this factory is invoked. |
paulb@15 | 73 | """ |
paulb@15 | 74 | |
paulb@15 | 75 | ServletFactory.__init__(self, application) |
paulb@15 | 76 | self.webstack_resource = resource |
paulb@15 | 77 | self.file_extensions = file_extensions |
paulb@15 | 78 | |
paulb@15 | 79 | def uniqueness(self): |
paulb@15 | 80 | |
paulb@15 | 81 | """ |
paulb@15 | 82 | Return "file" uniqueness - probably the most appropriate response. |
paulb@15 | 83 | """ |
paulb@15 | 84 | |
paulb@15 | 85 | return "file" |
paulb@15 | 86 | |
paulb@15 | 87 | def extensions(self): |
paulb@15 | 88 | |
paulb@15 | 89 | """ |
paulb@15 | 90 | Return the file extensions supported by this factory. |
paulb@15 | 91 | """ |
paulb@15 | 92 | |
paulb@15 | 93 | return self.file_extensions |
paulb@15 | 94 | |
paulb@15 | 95 | def servletForTransaction(self, trans): |
paulb@15 | 96 | |
paulb@15 | 97 | """ |
paulb@15 | 98 | Return a servlet which will provide access to the application-specific |
paulb@15 | 99 | resources. The 'trans' object - a Webware transaction - is not given to |
paulb@15 | 100 | the servlet since such information is available when the 'respond' |
paulb@15 | 101 | method is invoked on the servlet. |
paulb@15 | 102 | """ |
paulb@15 | 103 | |
paulb@15 | 104 | return WebStackServlet(self.webstack_resource) |
paulb@15 | 105 | |
paulb@32 | 106 | # Servlets are common to both solutions. |
paulb@32 | 107 | |
paulb@15 | 108 | class WebStackServlet(Servlet): |
paulb@15 | 109 | |
paulb@15 | 110 | "A servlet which dispatches transactions to application-specific resources." |
paulb@15 | 111 | |
paulb@15 | 112 | def __init__(self, resource): |
paulb@15 | 113 | |
paulb@15 | 114 | "Initialise the servlet with an application-specific 'resource'." |
paulb@15 | 115 | |
paulb@32 | 116 | Servlet.__init__(self) |
paulb@15 | 117 | self.webstack_resource = resource |
paulb@15 | 118 | |
paulb@15 | 119 | def respond(self, trans): |
paulb@15 | 120 | |
paulb@15 | 121 | """ |
paulb@15 | 122 | Respond to the incoming transaction, 'trans', by dispatching to the |
paulb@15 | 123 | application-specific resource. |
paulb@15 | 124 | """ |
paulb@15 | 125 | |
paulb@15 | 126 | new_trans = WebStack.Webware.Transaction(trans) |
paulb@15 | 127 | self.webstack_resource.respond(new_trans) |
paulb@29 | 128 | new_trans.commit() |
paulb@4 | 129 | |
paulb@4 | 130 | # vim: tabstop=4 expandtab shiftwidth=4 |