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.

CommunicationManager.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.InputStream;
  6. import java.net.URL;
  7. import javax.servlet.ServletContext;
  8. import com.vaadin.Application;
  9. import com.vaadin.external.json.JSONException;
  10. import com.vaadin.terminal.PaintException;
  11. import com.vaadin.terminal.WrappedRequest;
  12. import com.vaadin.ui.Root;
  13. /**
  14. * Application manager processes changes and paints for single application
  15. * instance.
  16. *
  17. * This class handles applications running as servlets.
  18. *
  19. * @see AbstractCommunicationManager
  20. *
  21. * @author Vaadin Ltd.
  22. * @since 5.0
  23. */
  24. @SuppressWarnings("serial")
  25. public class CommunicationManager extends AbstractCommunicationManager {
  26. /**
  27. * @deprecated use {@link #CommunicationManager(Application)} instead
  28. * @param application
  29. * @param applicationServlet
  30. */
  31. @Deprecated
  32. public CommunicationManager(Application application,
  33. AbstractApplicationServlet applicationServlet) {
  34. super(application);
  35. }
  36. /**
  37. * TODO New constructor - document me!
  38. *
  39. * @param application
  40. */
  41. public CommunicationManager(Application application) {
  42. super(application);
  43. }
  44. @Override
  45. protected BootstrapHandler createBootstrapHandler() {
  46. return new BootstrapHandler() {
  47. @Override
  48. protected String getApplicationId(BootstrapContext context) {
  49. String appUrl = getAppUri(context);
  50. String appId = appUrl;
  51. if ("".equals(appUrl)) {
  52. appId = "ROOT";
  53. }
  54. appId = appId.replaceAll("[^a-zA-Z0-9]", "");
  55. // Add hashCode to the end, so that it is still (sort of)
  56. // predictable, but indicates that it should not be used in CSS
  57. // and
  58. // such:
  59. int hashCode = appId.hashCode();
  60. if (hashCode < 0) {
  61. hashCode = -hashCode;
  62. }
  63. appId = appId + "-" + hashCode;
  64. return appId;
  65. }
  66. @Override
  67. protected String getAppUri(BootstrapContext context) {
  68. /* Fetch relative url to application */
  69. // don't use server and port in uri. It may cause problems with
  70. // some
  71. // virtual server configurations which lose the server name
  72. Application application = context.getApplication();
  73. URL url = application.getURL();
  74. String appUrl = url.getPath();
  75. if (appUrl.endsWith("/")) {
  76. appUrl = appUrl.substring(0, appUrl.length() - 1);
  77. }
  78. return appUrl;
  79. }
  80. @Override
  81. public String getThemeName(BootstrapContext context) {
  82. String themeName = context.getRequest().getParameter(
  83. AbstractApplicationServlet.URL_PARAMETER_THEME);
  84. if (themeName == null) {
  85. themeName = super.getThemeName(context);
  86. }
  87. return themeName;
  88. }
  89. @Override
  90. protected String getInitialUIDL(WrappedRequest request, Root root)
  91. throws PaintException, JSONException {
  92. return CommunicationManager.this.getInitialUIDL(request, root);
  93. }
  94. };
  95. }
  96. @Override
  97. protected InputStream getThemeResourceAsStream(Root root, String themeName,
  98. String resource) {
  99. WebApplicationContext context = (WebApplicationContext) root
  100. .getApplication().getContext();
  101. ServletContext servletContext = context.getHttpSession()
  102. .getServletContext();
  103. return servletContext.getResourceAsStream("/"
  104. + AbstractApplicationServlet.THEME_DIRECTORY_PATH + themeName
  105. + "/" + resource);
  106. }
  107. }