1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Defines a web application context for Muntjac applications."""
17
18 from muntjac.terminal.gwt.server.abstract_web_application_context import \
19 AbstractWebApplicationContext
20
21 from muntjac.util import clsname
22
23
24 -class WebApplicationContext(AbstractWebApplicationContext):
25 """Web application context for Muntjac applications.
26
27 This is automatically added as a L{HttpSessionBindingListener}
28 when added to a L{HttpSession}.
29
30 @author: Vaadin Ltd.
31 @author: Richard Lincoln
32 @version: 1.1.2
33 """
34
36 """Creates a new Web Application Context."""
37 super(WebApplicationContext, self).__init__()
38
39 self.session = None
40 self._reinitializingSession = False
41
42
43
44 self._currentRequest = None
45
46
47 - def __getstate__(self):
48 result = self.__dict__.copy()
49 del result['session']
50 del result['_reinitializingSession']
51 del result['_currentRequest']
52 return result
53
54
55 - def __setstate__(self, d):
56 self.__dict__ = d
57 self.session = None
58 self._reinitializingSession = False
59 self._currentRequest = None
60
61
62 - def startTransaction(self, application, request):
63 self._currentRequest = request
64 super(WebApplicationContext, self).startTransaction(application,
65 request)
66
67
68 - def endTransaction(self, application, request):
69 super(WebApplicationContext, self).endTransaction(application,
70 request)
71 self._currentRequest = None
72
73
74 - def valueUnbound(self, event):
75 if not self._reinitializingSession:
76
77
78
79
80 super(WebApplicationContext, self).valueUnbound(event)
81
82
84 """Discards the current session and creates a new session with
85 the same contents. The purpose of this is to introduce a new
86 session key in order to avoid session fixation attacks.
87 """
88 oldSession = self.getHttpSession()
89
90
91 attrs = dict()
92 attrs.update(oldSession.values)
93
94
95
96 self._reinitializingSession = True
97 oldSession.invalidate()
98 self._reinitializingSession = False
99
100
101 newSession = self._currentRequest.session()
102
103
104
105 for name, val in attrs.iteritems():
106 newSession.setValue(name, val)
107
108
109 self.session = newSession
110
111
113 """Gets the application context base directory.
114
115 @see: L{ApplicationContext.getBaseDirectory}
116 """
117 realPath = self.getResourcePath(self.session, '/')
118 if realPath is None:
119 return None
120 return realPath
121
122
123 - def getHttpSession(self):
124 """Gets the http-session application is running in.
125
126 @return: HttpSession this application context resides in.
127 """
128 return self.session
129
130
131 @classmethod
132 - def getApplicationContext(cls, session, servlet):
133 """Gets the application context for an HttpSession.
134
135 @param session:
136 the HTTP session.
137 @return: the application context for HttpSession.
138 """
139 cx = servlet.getSessionAttribute(session,
140 clsname(WebApplicationContext), None)
141
142 if cx is None:
143 cx = WebApplicationContext()
144 servlet.setSessionAttribute(session,
145 clsname(WebApplicationContext), cx)
146
147 if cx.session is None:
148 cx.session = session
149
150 return cx
151
152
153 - def addApplication(self, application):
154 self.applications.add(application)
155
156
157 - def getApplicationManager(self, application, servlet):
158 """Gets communication manager for an application.
159
160 If this application has not been running before, a new manager is
161 created.
162
163 @return: CommunicationManager
164 """
165 mgr = self.applicationToAjaxAppMgrMap.get(application)
166
167 if mgr is None:
168
169 mgr = servlet.createCommunicationManager(application)
170 self.applicationToAjaxAppMgrMap[application] = mgr
171
172 return mgr
173