summaryrefslogtreecommitdiff
path: root/cheetah/Servlet.py
blob: e122aca09e516211b6552e577f8a110d7ad521dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'''
Provides an abstract Servlet baseclass for Cheetah's Template class
'''

import sys
import os.path

isWebwareInstalled = False
try:
    try:
        from ds.appserver.Servlet import Servlet as BaseServlet
    except:
        from WebKit.Servlet import Servlet as BaseServlet
    isWebwareInstalled = True

    if not issubclass(BaseServlet, object):
        class NewStyleBaseServlet(BaseServlet, object):
            pass
        BaseServlet = NewStyleBaseServlet
except:
    class BaseServlet(object): 
        _reusable = 1
        _threadSafe = 0
    
        def awake(self, transaction):
            pass
            
        def sleep(self, transaction):
            pass

        def shutdown(self):
            pass

##################################################
## CLASSES

class Servlet(BaseServlet):
    
    """This class is an abstract baseclass for Cheetah.Template.Template.

    It wraps WebKit.Servlet and provides a few extra convenience methods that
    are also found in WebKit.Page.  It doesn't do any of the HTTP method
    resolution that is done in WebKit.HTTPServlet
    """
    
    transaction = None
    application = None
    request = None
    session = None
    
    def __init__(self, *args, **kwargs):
        super(Servlet, self).__init__(*args, **kwargs)
       
        # this default will be changed by the .awake() method
        self._CHEETAH__isControlledByWebKit = False 
        
    ## methods called by Webware during the request-response
        
    def awake(self, transaction):
        super(Servlet, self).awake(transaction)
        
        # a hack to signify that the servlet is being run directly from WebKit
        self._CHEETAH__isControlledByWebKit = True
        
        self.transaction = transaction        
        #self.application = transaction.application
        self.response = response = transaction.response
        self.request = transaction.request

        # Temporary hack to accomodate bug in
        # WebKit.Servlet.Servlet.serverSidePath: it uses 
        # self._request even though this attribute does not exist.
        # This attribute WILL disappear in the future.
        self._request = transaction.request()

        
        self.session = transaction.session
        self.write = response().write
        #self.writeln = response.writeln
        
    def respond(self, trans=None):
        raise NotImplementedError("""\
couldn't find the template's main method.  If you are using #extends
without #implements, try adding '#implements respond' to your template
definition.""")

    def sleep(self, transaction):
        super(Servlet, self).sleep(transaction)
        self.session = None
        self.request  = None
        self._request  = None        
        self.response = None
        self.transaction = None

    def shutdown(self):
        pass

    def serverSidePath(self, path=None,
                       normpath=os.path.normpath,
                       abspath=os.path.abspath
                       ):
        
        if self._CHEETAH__isControlledByWebKit:
            return super(Servlet, self).serverSidePath(path)
        elif path:
            return normpath(abspath(path.replace("\\", '/')))
        elif hasattr(self, '_filePath') and self._filePath:
            return normpath(abspath(self._filePath))
        else:
            return None

# vim: shiftwidth=4 tabstop=4 expandtab