You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebApplicationContext.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.File;
  6. import java.util.Enumeration;
  7. import java.util.HashMap;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpSession;
  10. import javax.servlet.http.HttpSessionBindingEvent;
  11. import javax.servlet.http.HttpSessionBindingListener;
  12. import com.vaadin.Application;
  13. /**
  14. * Web application context for Vaadin applications.
  15. *
  16. * This is automatically added as a {@link HttpSessionBindingListener} when
  17. * added to a {@link HttpSession}.
  18. *
  19. * @author Vaadin Ltd.
  20. * @since 3.1
  21. */
  22. @SuppressWarnings("serial")
  23. public class WebApplicationContext extends AbstractWebApplicationContext {
  24. protected transient HttpSession session;
  25. private transient boolean reinitializingSession = false;
  26. /**
  27. * Stores a reference to the currentRequest. Null it not inside a request.
  28. */
  29. private transient Object currentRequest = null;
  30. /**
  31. * Creates a new Web Application Context.
  32. *
  33. */
  34. protected WebApplicationContext() {
  35. }
  36. @Override
  37. protected void startTransaction(Application application, Object request) {
  38. currentRequest = request;
  39. super.startTransaction(application, request);
  40. }
  41. @Override
  42. protected void endTransaction(Application application, Object request) {
  43. super.endTransaction(application, request);
  44. currentRequest = null;
  45. }
  46. @Override
  47. public void valueUnbound(HttpSessionBindingEvent event) {
  48. if (!reinitializingSession) {
  49. // Avoid closing the application if we are only reinitializing the
  50. // session. Closing the application would cause the state to be lost
  51. // and a new application to be created, which is not what we want.
  52. super.valueUnbound(event);
  53. }
  54. }
  55. /**
  56. * Discards the current session and creates a new session with the same
  57. * contents. The purpose of this is to introduce a new session key in order
  58. * to avoid session fixation attacks.
  59. */
  60. @SuppressWarnings("unchecked")
  61. public void reinitializeSession() {
  62. HttpSession oldSession = getHttpSession();
  63. // Stores all attributes (security key, reference to this context
  64. // instance) so they can be added to the new session
  65. HashMap<String, Object> attrs = new HashMap<String, Object>();
  66. for (Enumeration<String> e = oldSession.getAttributeNames(); e
  67. .hasMoreElements();) {
  68. String name = e.nextElement();
  69. attrs.put(name, oldSession.getAttribute(name));
  70. }
  71. // Invalidate the current session, set flag to avoid call to
  72. // valueUnbound
  73. reinitializingSession = true;
  74. oldSession.invalidate();
  75. reinitializingSession = false;
  76. // Create a new session
  77. HttpSession newSession = ((HttpServletRequest) currentRequest)
  78. .getSession();
  79. // Restores all attributes (security key, reference to this context
  80. // instance)
  81. for (String name : attrs.keySet()) {
  82. newSession.setAttribute(name, attrs.get(name));
  83. }
  84. // Update the "current session" variable
  85. session = newSession;
  86. }
  87. /**
  88. * Gets the application context base directory.
  89. *
  90. * @see com.vaadin.service.ApplicationContext#getBaseDirectory()
  91. */
  92. @Override
  93. public File getBaseDirectory() {
  94. final String realPath = ApplicationServlet.getResourcePath(
  95. session.getServletContext(), "/");
  96. if (realPath == null) {
  97. return null;
  98. }
  99. return new File(realPath);
  100. }
  101. /**
  102. * Gets the http-session application is running in.
  103. *
  104. * @return HttpSession this application context resides in.
  105. */
  106. public HttpSession getHttpSession() {
  107. return session;
  108. }
  109. /**
  110. * Gets the application context for an HttpSession.
  111. *
  112. * @param session
  113. * the HTTP session.
  114. * @return the application context for HttpSession.
  115. */
  116. static public WebApplicationContext getApplicationContext(
  117. HttpSession session) {
  118. WebApplicationContext cx = (WebApplicationContext) session
  119. .getAttribute(WebApplicationContext.class.getName());
  120. if (cx == null) {
  121. cx = new WebApplicationContext();
  122. session.setAttribute(WebApplicationContext.class.getName(), cx);
  123. }
  124. if (cx.session == null) {
  125. cx.session = session;
  126. }
  127. return cx;
  128. }
  129. protected void addApplication(Application application) {
  130. applications.add(application);
  131. }
  132. /**
  133. * Gets communication manager for an application.
  134. *
  135. * If this application has not been running before, a new manager is
  136. * created.
  137. *
  138. * @param application
  139. * @return CommunicationManager
  140. */
  141. public CommunicationManager getApplicationManager(Application application,
  142. AbstractApplicationServlet servlet) {
  143. CommunicationManager mgr = (CommunicationManager) applicationToAjaxAppMgrMap
  144. .get(application);
  145. if (mgr == null) {
  146. // Creates new manager
  147. mgr = servlet.createCommunicationManager(application);
  148. applicationToAjaxAppMgrMap.put(application, mgr);
  149. }
  150. return mgr;
  151. }
  152. }