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.

LegacyApplicationUIProvider.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2000-2018 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.net.MalformedURLException;
  18. import java.net.URL;
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21. import com.vaadin.ui.LegacyWindow;
  22. import com.vaadin.ui.UI;
  23. /**
  24. *
  25. * @author Vaadin Ltd
  26. * @since 7.0.0
  27. *
  28. * @deprecated As of 7.0. Used only to support LegacyApplication - will be
  29. * removed when LegacyApplication support is removed.
  30. */
  31. @Deprecated
  32. public abstract class LegacyApplicationUIProvider extends UIProvider {
  33. /**
  34. * Ignore initial / and then get everything up to the next /
  35. */
  36. private static final Pattern WINDOW_NAME_PATTERN = Pattern
  37. .compile("^/?([^/]+).*");
  38. @Override
  39. public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
  40. UI uiInstance = getUIInstance(event);
  41. if (uiInstance != null) {
  42. return uiInstance.getClass();
  43. }
  44. return null;
  45. }
  46. @Override
  47. public UI createInstance(UICreateEvent event) {
  48. return getUIInstance(event);
  49. }
  50. @Override
  51. public String getTheme(UICreateEvent event) {
  52. LegacyApplication application = getApplication();
  53. if (application != null) {
  54. return application.getTheme();
  55. } else {
  56. return null;
  57. }
  58. }
  59. @Override
  60. public String getPageTitle(UICreateEvent event) {
  61. UI uiInstance = getUIInstance(event);
  62. if (uiInstance != null) {
  63. return uiInstance.getCaption();
  64. } else {
  65. return super.getPageTitle(event);
  66. }
  67. }
  68. private UI getUIInstance(UIProviderEvent event) {
  69. VaadinRequest request = event.getRequest();
  70. String pathInfo = request.getPathInfo();
  71. String name = null;
  72. if (pathInfo != null && !pathInfo.isEmpty()) {
  73. Matcher matcher = WINDOW_NAME_PATTERN.matcher(pathInfo);
  74. if (matcher.matches()) {
  75. // Skip the initial slash
  76. name = matcher.group(1);
  77. }
  78. }
  79. LegacyApplication application = getApplication();
  80. if (application == null) {
  81. return null;
  82. }
  83. LegacyWindow window = application.getWindow(name);
  84. if (window != null) {
  85. return window;
  86. }
  87. return application.getMainWindow();
  88. }
  89. /**
  90. * Hack used to return existing LegacyWindow instances without regard for
  91. * out-of-sync problems.
  92. *
  93. * @param event
  94. * @return
  95. */
  96. public UI getExistingUI(UIClassSelectionEvent event) {
  97. UI uiInstance = getUIInstance(event);
  98. if (uiInstance == null || uiInstance.getUIId() == -1) {
  99. // Not initialized -> Let go through createUIInstance to make it
  100. // initialized
  101. return null;
  102. } else {
  103. UI.setCurrent(uiInstance);
  104. return uiInstance;
  105. }
  106. }
  107. private LegacyApplication getApplication() {
  108. LegacyApplication application = VaadinSession.getCurrent()
  109. .getAttribute(LegacyApplication.class);
  110. if (application == null) {
  111. application = createApplication();
  112. if (application == null) {
  113. return null;
  114. }
  115. VaadinSession.getCurrent().setAttribute(LegacyApplication.class,
  116. application);
  117. URL applicationUrl;
  118. try {
  119. applicationUrl = VaadinService.getCurrent()
  120. .getApplicationUrl(VaadinService.getCurrentRequest());
  121. } catch (MalformedURLException e) {
  122. throw new RuntimeException(e);
  123. }
  124. application.doInit(applicationUrl);
  125. }
  126. if (application != null && !application.isRunning()) {
  127. VaadinSession.getCurrent().setAttribute(LegacyApplication.class,
  128. null);
  129. // Run again without a current application
  130. return getApplication();
  131. }
  132. return application;
  133. }
  134. protected abstract LegacyApplication createApplication();
  135. }