| 1 | ''' |
|---|
| 2 | Provides an abstract Servlet baseclass for Cheetah's Template class |
|---|
| 3 | ''' |
|---|
| 4 | |
|---|
| 5 | import sys |
|---|
| 6 | import os.path |
|---|
| 7 | |
|---|
| 8 | isWebwareInstalled = False |
|---|
| 9 | try: |
|---|
| 10 | try: |
|---|
| 11 | from ds.appserver.Servlet import Servlet as BaseServlet |
|---|
| 12 | except: |
|---|
| 13 | from WebKit.Servlet import Servlet as BaseServlet |
|---|
| 14 | isWebwareInstalled = True |
|---|
| 15 | |
|---|
| 16 | if not issubclass(BaseServlet, object): |
|---|
| 17 | class NewStyleBaseServlet(BaseServlet, object): |
|---|
| 18 | pass |
|---|
| 19 | BaseServlet = NewStyleBaseServlet |
|---|
| 20 | except: |
|---|
| 21 | class BaseServlet(object): |
|---|
| 22 | _reusable = 1 |
|---|
| 23 | _threadSafe = 0 |
|---|
| 24 | |
|---|
| 25 | def awake(self, transaction): |
|---|
| 26 | pass |
|---|
| 27 | |
|---|
| 28 | def sleep(self, transaction): |
|---|
| 29 | pass |
|---|
| 30 | |
|---|
| 31 | def shutdown(self): |
|---|
| 32 | pass |
|---|
| 33 | |
|---|
| 34 | ################################################## |
|---|
| 35 | ## CLASSES |
|---|
| 36 | |
|---|
| 37 | class Servlet(BaseServlet): |
|---|
| 38 | |
|---|
| 39 | """This class is an abstract baseclass for Cheetah.Template.Template. |
|---|
| 40 | |
|---|
| 41 | It wraps WebKit.Servlet and provides a few extra convenience methods that |
|---|
| 42 | are also found in WebKit.Page. It doesn't do any of the HTTP method |
|---|
| 43 | resolution that is done in WebKit.HTTPServlet |
|---|
| 44 | """ |
|---|
| 45 | |
|---|
| 46 | transaction = None |
|---|
| 47 | application = None |
|---|
| 48 | request = None |
|---|
| 49 | session = None |
|---|
| 50 | |
|---|
| 51 | def __init__(self, *args, **kwargs): |
|---|
| 52 | super(Servlet, self).__init__(*args, **kwargs) |
|---|
| 53 | |
|---|
| 54 | # this default will be changed by the .awake() method |
|---|
| 55 | self._CHEETAH__isControlledByWebKit = False |
|---|
| 56 | |
|---|
| 57 | ## methods called by Webware during the request-response |
|---|
| 58 | |
|---|
| 59 | def awake(self, transaction): |
|---|
| 60 | super(Servlet, self).awake(transaction) |
|---|
| 61 | |
|---|
| 62 | # a hack to signify that the servlet is being run directly from WebKit |
|---|
| 63 | self._CHEETAH__isControlledByWebKit = True |
|---|
| 64 | |
|---|
| 65 | self.transaction = transaction |
|---|
| 66 | #self.application = transaction.application |
|---|
| 67 | self.response = response = transaction.response |
|---|
| 68 | self.request = transaction.request |
|---|
| 69 | |
|---|
| 70 | # Temporary hack to accomodate bug in |
|---|
| 71 | # WebKit.Servlet.Servlet.serverSidePath: it uses |
|---|
| 72 | # self._request even though this attribute does not exist. |
|---|
| 73 | # This attribute WILL disappear in the future. |
|---|
| 74 | self._request = transaction.request() |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | self.session = transaction.session |
|---|
| 78 | self.write = response().write |
|---|
| 79 | #self.writeln = response.writeln |
|---|
| 80 | |
|---|
| 81 | def respond(self, trans=None): |
|---|
| 82 | raise NotImplementedError("""\ |
|---|
| 83 | couldn't find the template's main method. If you are using #extends |
|---|
| 84 | without #implements, try adding '#implements respond' to your template |
|---|
| 85 | definition.""") |
|---|
| 86 | |
|---|
| 87 | def sleep(self, transaction): |
|---|
| 88 | super(Servlet, self).sleep(transaction) |
|---|
| 89 | self.session = None |
|---|
| 90 | self.request = None |
|---|
| 91 | self._request = None |
|---|
| 92 | self.response = None |
|---|
| 93 | self.transaction = None |
|---|
| 94 | |
|---|
| 95 | def shutdown(self): |
|---|
| 96 | pass |
|---|
| 97 | |
|---|
| 98 | def serverSidePath(self, path=None, |
|---|
| 99 | normpath=os.path.normpath, |
|---|
| 100 | abspath=os.path.abspath |
|---|
| 101 | ): |
|---|
| 102 | |
|---|
| 103 | if self._CHEETAH__isControlledByWebKit: |
|---|
| 104 | return super(Servlet, self).serverSidePath(path) |
|---|
| 105 | elif path: |
|---|
| 106 | return normpath(abspath(path.replace("\\",'/'))) |
|---|
| 107 | elif hasattr(self, '_filePath') and self._filePath: |
|---|
| 108 | return normpath(abspath(self._filePath)) |
|---|
| 109 | else: |
|---|
| 110 | return None |
|---|
| 111 | |
|---|
| 112 | # vim: shiftwidth=4 tabstop=4 expandtab |
|---|