Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CommunicationManager.java 4.3KB

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