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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2000-2016 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. /**
  24. * Contains helper methods shared by {@link VaadinServlet} and
  25. * {@link VaadinPortlet}.
  26. *
  27. * @deprecated As of 7.1. Will be removed or refactored in the future.
  28. */
  29. @Deprecated
  30. public class ServletPortletHelper implements Serializable {
  31. public static final String UPLOAD_URL_PREFIX = "APP/UPLOAD/";
  32. /**
  33. * The default SystemMessages (read-only).
  34. */
  35. static final SystemMessages DEFAULT_SYSTEM_MESSAGES = new SystemMessages();
  36. static Class<? extends LegacyApplication> getLegacyApplicationClass(
  37. VaadinService vaadinService) throws ServiceException {
  38. Properties initParameters = vaadinService.getDeploymentConfiguration()
  39. .getInitParameters();
  40. String applicationParameter = initParameters.getProperty("application");
  41. ClassLoader classLoader = vaadinService.getClassLoader();
  42. if (applicationParameter == null) {
  43. throw new ServiceException(
  44. "No \"application\" init parameter found");
  45. }
  46. try {
  47. return classLoader.loadClass(applicationParameter)
  48. .asSubclass(LegacyApplication.class);
  49. } catch (final ClassNotFoundException e) {
  50. throw new ServiceException(
  51. "Failed to load application class: " + applicationParameter,
  52. e);
  53. }
  54. }
  55. private static void verifyUIClass(String className, ClassLoader classLoader)
  56. throws ServiceException {
  57. if (className == null) {
  58. throw new ServiceException(
  59. VaadinSession.UI_PARAMETER + " init parameter not defined");
  60. }
  61. // Check that the UI layout class can be found
  62. try {
  63. Class<?> uiClass = classLoader.loadClass(className);
  64. if (!UI.class.isAssignableFrom(uiClass)) {
  65. throw new ServiceException(
  66. className + " does not implement UI");
  67. }
  68. // Try finding a default constructor, else throw exception
  69. uiClass.getConstructor();
  70. } catch (ClassNotFoundException e) {
  71. throw new ServiceException(className + " could not be loaded", e);
  72. } catch (SecurityException e) {
  73. throw new ServiceException(
  74. "Could not access " + className + " class", e);
  75. } catch (NoSuchMethodException e) {
  76. throw new ServiceException(
  77. className + " doesn't have a public no-args constructor");
  78. }
  79. }
  80. private static boolean hasPathPrefix(VaadinRequest request, String prefix) {
  81. String pathInfo = request.getPathInfo();
  82. if (pathInfo == null) {
  83. return false;
  84. }
  85. if (!prefix.startsWith("/")) {
  86. prefix = '/' + prefix;
  87. }
  88. if (pathInfo.startsWith(prefix)) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. private static boolean isPathInfo(VaadinRequest request, String string) {
  94. String pathInfo = request.getPathInfo();
  95. if (pathInfo == null) {
  96. return false;
  97. }
  98. if (!string.startsWith("/")) {
  99. string = '/' + string;
  100. }
  101. if (pathInfo.equals(string)) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. public static boolean isFileUploadRequest(VaadinRequest request) {
  107. return hasPathPrefix(request, UPLOAD_URL_PREFIX);
  108. }
  109. public static boolean isPublishedFileRequest(VaadinRequest request) {
  110. return hasPathPrefix(request,
  111. ApplicationConstants.PUBLISHED_FILE_PATH + "/");
  112. }
  113. public static boolean isUIDLRequest(VaadinRequest request) {
  114. return hasPathPrefix(request, ApplicationConstants.UIDL_PATH + '/');
  115. }
  116. public static boolean isAppRequest(VaadinRequest request) {
  117. return hasPathPrefix(request, ApplicationConstants.APP_PATH + '/');
  118. }
  119. public static boolean isHeartbeatRequest(VaadinRequest request) {
  120. return hasPathPrefix(request,
  121. ApplicationConstants.HEARTBEAT_PATH + '/');
  122. }
  123. public static boolean isPushRequest(VaadinRequest request) {
  124. return isPathInfo(request, ApplicationConstants.PUSH_PATH);
  125. }
  126. public static void initDefaultUIProvider(VaadinSession session,
  127. VaadinService vaadinService) throws ServiceException {
  128. String uiProperty = vaadinService.getDeploymentConfiguration()
  129. .getUIClassName();
  130. // Add provider for UI parameter first to give it lower priority
  131. // (providers are FILO)
  132. if (uiProperty != null) {
  133. verifyUIClass(uiProperty, vaadinService.getClassLoader());
  134. session.addUIProvider(new DefaultUIProvider());
  135. }
  136. String uiProviderProperty = vaadinService.getDeploymentConfiguration()
  137. .getUIProviderClassName();
  138. // Then add custom UI provider if defined
  139. if (uiProviderProperty != null) {
  140. UIProvider uiProvider = getUIProvider(uiProviderProperty,
  141. vaadinService.getClassLoader());
  142. session.addUIProvider(uiProvider);
  143. }
  144. }
  145. private static UIProvider getUIProvider(String uiProviderProperty,
  146. ClassLoader classLoader) throws ServiceException {
  147. try {
  148. Class<?> providerClass = classLoader.loadClass(uiProviderProperty);
  149. Class<? extends UIProvider> subclass = providerClass
  150. .asSubclass(UIProvider.class);
  151. return subclass.newInstance();
  152. } catch (ClassNotFoundException e) {
  153. throw new ServiceException(
  154. "Could not load UIProvider class " + uiProviderProperty, e);
  155. } catch (ClassCastException e) {
  156. throw new ServiceException("UIProvider class " + uiProviderProperty
  157. + " does not extend UIProvider", e);
  158. } catch (InstantiationException | IllegalAccessException e) {
  159. throw new ServiceException(
  160. "Could not instantiate UIProvider " + uiProviderProperty,
  161. 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. }