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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.vaadin.server;
  2. import java.io.Serializable;
  3. import com.vaadin.Application;
  4. import com.vaadin.shared.ApplicationConstants;
  5. import com.vaadin.ui.UI;
  6. /*
  7. * Copyright 2011 Vaadin Ltd.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy of
  11. * the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. * License for the specific language governing permissions and limitations under
  19. * the License.
  20. */
  21. class ServletPortletHelper implements Serializable {
  22. public static final String UPLOAD_URL_PREFIX = "APP/UPLOAD/";
  23. public static class ApplicationClassException extends Exception {
  24. public ApplicationClassException(String message, Throwable cause) {
  25. super(message, cause);
  26. }
  27. public ApplicationClassException(String message) {
  28. super(message);
  29. }
  30. }
  31. static Class<? extends Application> getApplicationClass(
  32. DeploymentConfiguration deploymentConfiguration)
  33. throws ApplicationClassException {
  34. String applicationParameter = deploymentConfiguration
  35. .getInitParameters().getProperty("application");
  36. String uiParameter = deploymentConfiguration.getInitParameters()
  37. .getProperty(Application.UI_PARAMETER);
  38. ClassLoader classLoader = deploymentConfiguration.getClassLoader();
  39. if (applicationParameter == null) {
  40. // Validate the parameter value
  41. verifyUIClass(uiParameter, classLoader);
  42. // Application can be used if a valid rootLayout is defined
  43. return Application.class;
  44. }
  45. try {
  46. return (Class<? extends Application>) classLoader
  47. .loadClass(applicationParameter);
  48. } catch (final ClassNotFoundException e) {
  49. throw new ApplicationClassException(
  50. "Failed to load application class: " + applicationParameter,
  51. e);
  52. }
  53. }
  54. private static void verifyUIClass(String className, ClassLoader classLoader)
  55. throws ApplicationClassException {
  56. if (className == null) {
  57. throw new ApplicationClassException(Application.UI_PARAMETER
  58. + " init parameter not defined");
  59. }
  60. // Check that the UI layout class can be found
  61. try {
  62. Class<?> uiClass = classLoader.loadClass(className);
  63. if (!UI.class.isAssignableFrom(uiClass)) {
  64. throw new ApplicationClassException(className
  65. + " does not implement UI");
  66. }
  67. // Try finding a default constructor, else throw exception
  68. uiClass.getConstructor();
  69. } catch (ClassNotFoundException e) {
  70. throw new ApplicationClassException(className
  71. + " could not be loaded", e);
  72. } catch (SecurityException e) {
  73. throw new ApplicationClassException("Could not access " + className
  74. + " class", e);
  75. } catch (NoSuchMethodException e) {
  76. throw new ApplicationClassException(className
  77. + " doesn't have a public no-args constructor");
  78. }
  79. }
  80. private static boolean hasPathPrefix(WrappedRequest request, String prefix) {
  81. String pathInfo = request.getRequestPathInfo();
  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. public static boolean isFileUploadRequest(WrappedRequest request) {
  94. return hasPathPrefix(request, UPLOAD_URL_PREFIX);
  95. }
  96. public static boolean isConnectorResourceRequest(WrappedRequest request) {
  97. return hasPathPrefix(request,
  98. ApplicationConstants.CONNECTOR_RESOURCE_PREFIX + "/");
  99. }
  100. public static boolean isUIDLRequest(WrappedRequest request) {
  101. return hasPathPrefix(request, ApplicationConstants.UIDL_REQUEST_PATH);
  102. }
  103. public static boolean isApplicationResourceRequest(WrappedRequest request) {
  104. return hasPathPrefix(request, ApplicationConstants.APP_REQUEST_PATH);
  105. }
  106. public static boolean isHeartbeatRequest(WrappedRequest request) {
  107. return hasPathPrefix(request,
  108. ApplicationConstants.HEARTBEAT_REQUEST_PATH);
  109. }
  110. }