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