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.

ServletPortletHelper.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.io.Serializable;
  18. import java.util.Locale;
  19. import java.util.Properties;
  20. import com.vaadin.shared.ApplicationConstants;
  21. import com.vaadin.ui.Component;
  22. import com.vaadin.ui.UI;
  23. import com.vaadin.util.ReflectTools;
  24. /**
  25. * Contains helper methods shared by {@link VaadinServlet} and
  26. * {@link VaadinPortlet}.
  27. *
  28. * @deprecated As of 7.1. Will be removed or refactored in the future.
  29. */
  30. @Deprecated
  31. public class ServletPortletHelper implements Serializable {
  32. public static final String UPLOAD_URL_PREFIX = "APP/UPLOAD/";
  33. /**
  34. * The default SystemMessages (read-only).
  35. */
  36. static final SystemMessages DEFAULT_SYSTEM_MESSAGES = new SystemMessages();
  37. static Class<? extends LegacyApplication> getLegacyApplicationClass(
  38. VaadinService vaadinService) throws ServiceException {
  39. Properties initParameters = vaadinService.getDeploymentConfiguration()
  40. .getInitParameters();
  41. String applicationParameter = initParameters.getProperty("application");
  42. ClassLoader classLoader = vaadinService.getClassLoader();
  43. if (applicationParameter == null) {
  44. throw new ServiceException(
  45. "No \"application\" init parameter found");
  46. }
  47. try {
  48. return classLoader.loadClass(applicationParameter)
  49. .asSubclass(LegacyApplication.class);
  50. } catch (final ClassNotFoundException e) {
  51. throw new ServiceException(
  52. "Failed to load application class: " + applicationParameter,
  53. e);
  54. }
  55. }
  56. private static void verifyUIClass(String className, ClassLoader classLoader)
  57. throws ServiceException {
  58. if (className == null) {
  59. throw new ServiceException(
  60. VaadinSession.UI_PARAMETER + " init parameter not defined");
  61. }
  62. // Check that the UI layout class can be found
  63. try {
  64. Class<?> uiClass = classLoader.loadClass(className);
  65. if (!UI.class.isAssignableFrom(uiClass)) {
  66. throw new ServiceException(
  67. className + " does not implement UI");
  68. }
  69. // Try finding a default constructor, else throw exception
  70. uiClass.getConstructor();
  71. } catch (ClassNotFoundException e) {
  72. throw new ServiceException(className + " could not be loaded", e);
  73. } catch (SecurityException e) {
  74. throw new ServiceException(
  75. "Could not access " + className + " class", e);
  76. } catch (NoSuchMethodException e) {
  77. throw new ServiceException(
  78. className + " doesn't have a public no-args constructor");
  79. }
  80. }
  81. private static boolean hasPathPrefix(VaadinRequest request, String prefix) {
  82. String pathInfo = request.getPathInfo();
  83. if (pathInfo == null) {
  84. return false;
  85. }
  86. if (!prefix.startsWith("/")) {
  87. prefix = '/' + prefix;
  88. }
  89. if (!pathInfo.endsWith("/") && prefix.endsWith("/")) {
  90. pathInfo += '/';
  91. }
  92. if (pathInfo.startsWith(prefix)) {
  93. return true;
  94. }
  95. return false;
  96. }
  97. private static boolean isPathInfo(VaadinRequest request, String string) {
  98. String pathInfo = request.getPathInfo();
  99. if (pathInfo == null) {
  100. return false;
  101. }
  102. if (!string.startsWith("/")) {
  103. string = '/' + string;
  104. }
  105. if (pathInfo.equals(string)) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. public static boolean isFileUploadRequest(VaadinRequest request) {
  111. return hasPathPrefix(request, UPLOAD_URL_PREFIX);
  112. }
  113. public static boolean isPublishedFileRequest(VaadinRequest request) {
  114. return hasPathPrefix(request,
  115. ApplicationConstants.PUBLISHED_FILE_PATH + "/");
  116. }
  117. public static boolean isUIDLRequest(VaadinRequest request) {
  118. return hasPathPrefix(request, ApplicationConstants.UIDL_PATH + '/');
  119. }
  120. public static boolean isAppRequest(VaadinRequest request) {
  121. return hasPathPrefix(request, ApplicationConstants.APP_PATH + '/');
  122. }
  123. public static boolean isHeartbeatRequest(VaadinRequest request) {
  124. return hasPathPrefix(request,
  125. ApplicationConstants.HEARTBEAT_PATH + '/');
  126. }
  127. public static boolean isPushRequest(VaadinRequest request) {
  128. return isPathInfo(request, ApplicationConstants.PUSH_PATH);
  129. }
  130. public static void initDefaultUIProvider(VaadinSession session,
  131. VaadinService vaadinService) throws ServiceException {
  132. String uiProperty = vaadinService.getDeploymentConfiguration()
  133. .getUIClassName();
  134. // Add provider for UI parameter first to give it lower priority
  135. // (providers are FILO)
  136. if (uiProperty != null) {
  137. verifyUIClass(uiProperty, vaadinService.getClassLoader());
  138. session.addUIProvider(new DefaultUIProvider());
  139. }
  140. String uiProviderProperty = vaadinService.getDeploymentConfiguration()
  141. .getUIProviderClassName();
  142. // Then add custom UI provider if defined
  143. if (uiProviderProperty != null) {
  144. UIProvider uiProvider = getUIProvider(uiProviderProperty,
  145. vaadinService.getClassLoader());
  146. session.addUIProvider(uiProvider);
  147. }
  148. }
  149. private static UIProvider getUIProvider(String uiProviderProperty,
  150. ClassLoader classLoader) throws ServiceException {
  151. try {
  152. Class<?> providerClass = classLoader.loadClass(uiProviderProperty);
  153. Class<? extends UIProvider> subclass = providerClass
  154. .asSubclass(UIProvider.class);
  155. return ReflectTools.createInstance(subclass);
  156. } catch (ClassNotFoundException e) {
  157. throw new ServiceException(
  158. "Could not load UIProvider class " + uiProviderProperty, e);
  159. } catch (ClassCastException e) {
  160. throw new ServiceException("UIProvider class " + uiProviderProperty
  161. + " does not extend UIProvider", e);
  162. }
  163. }
  164. public static void checkUiProviders(VaadinSession session,
  165. VaadinService vaadinService) throws ServiceException {
  166. if (session.getUIProviders().isEmpty()) {
  167. throw new ServiceException(
  168. "No UIProvider has been added and there is no \""
  169. + VaadinSession.UI_PARAMETER
  170. + "\" init parameter.");
  171. }
  172. }
  173. /**
  174. * Helper to find the most most suitable Locale. These potential sources are
  175. * checked in order until a Locale is found:
  176. * <ol>
  177. * <li>The passed component (or UI) if not null</li>
  178. * <li>{@link UI#getCurrent()} if defined</li>
  179. * <li>The passed session if not null</li>
  180. * <li>{@link VaadinSession#getCurrent()} if defined</li>
  181. * <li>The passed request if not null</li>
  182. * <li>{@link VaadinService#getCurrentRequest()} if defined</li>
  183. * <li>{@link Locale#getDefault()}</li>
  184. * </ol>
  185. */
  186. public static Locale findLocale(Component component, VaadinSession session,
  187. VaadinRequest request) {
  188. if (component == null) {
  189. component = UI.getCurrent();
  190. }
  191. if (component != null) {
  192. Locale locale = component.getLocale();
  193. if (locale != null) {
  194. return locale;
  195. }
  196. }
  197. if (session == null) {
  198. session = VaadinSession.getCurrent();
  199. }
  200. if (session != null) {
  201. Locale locale = session.getLocale();
  202. if (locale != null) {
  203. return locale;
  204. }
  205. }
  206. if (request == null) {
  207. request = VaadinService.getCurrentRequest();
  208. }
  209. if (request != null) {
  210. Locale locale = request.getLocale();
  211. if (locale != null) {
  212. return locale;
  213. }
  214. }
  215. return Locale.getDefault();
  216. }
  217. private ServletPortletHelper() {
  218. }
  219. }