Package muntjac :: Package terminal :: Package gwt :: Package server :: Module web_application_context
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.gwt.server.web_application_context

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 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
35 - def __init__(self):
36 """Creates a new Web Application Context.""" 37 super(WebApplicationContext, self).__init__() 38 39 self.session = None 40 self._reinitializingSession = False 41 42 # Stores a reference to the currentRequest. None it not inside 43 # a request. 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 # Avoid closing the application if we are only reinitializing 77 # the session. Closing the application would cause the state 78 # to be lost and a new application to be created, which is not 79 # what we want. 80 super(WebApplicationContext, self).valueUnbound(event)
81 82
83 - def reinitializeSession(self):
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 # Stores all attributes (security key, reference to this context 90 # instance) so they can be added to the new session 91 attrs = dict() 92 attrs.update(oldSession.values) 93 94 # Invalidate the current session, set flag to avoid call to 95 # valueUnbound 96 self._reinitializingSession = True 97 oldSession.invalidate() 98 self._reinitializingSession = False 99 100 # Create a new session 101 newSession = self._currentRequest.session() 102 103 # Restores all attributes (security key, reference to this context 104 # instance) 105 for name, val in attrs.iteritems(): 106 newSession.setValue(name, val) 107 108 # Update the "current session" variable 109 self.session = newSession
110 111
112 - def getBaseDirectory(self):
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 # Creates new manager 169 mgr = servlet.createCommunicationManager(application) 170 self.applicationToAjaxAppMgrMap[application] = mgr 171 172 return mgr
173