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.

ApplicationRunnerServlet.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.vaadin.terminal.gwt.server;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import javax.servlet.ServletConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import com.vaadin.Application;
  10. @SuppressWarnings("serial")
  11. public class ApplicationRunnerServlet extends AbstractApplicationServlet {
  12. /**
  13. * The name of the application class currently used. Only valid within one
  14. * request.
  15. */
  16. String applicationClassName = "";
  17. @Override
  18. public void init(ServletConfig servletConfig) throws ServletException {
  19. super.init(servletConfig);
  20. }
  21. @Override
  22. protected void service(HttpServletRequest request,
  23. HttpServletResponse response) throws ServletException, IOException {
  24. applicationClassName = getApplicationRunnerApplicationClassName(request);
  25. super.service(request, response);
  26. applicationClassName = "";
  27. }
  28. @Override
  29. URL getApplicationUrl(HttpServletRequest request)
  30. throws MalformedURLException {
  31. URL url = super.getApplicationUrl(request);
  32. String path = url.toString();
  33. path += applicationClassName;
  34. path += "/";
  35. return new URL(path);
  36. }
  37. @Override
  38. protected Application getNewApplication(HttpServletRequest request)
  39. throws ServletException {
  40. // Creates a new application instance
  41. try {
  42. Class<?> applicationClass = getClassLoader().loadClass(
  43. applicationClassName);
  44. final Application application = (Application) applicationClass
  45. .newInstance();
  46. return application;
  47. } catch (final IllegalAccessException e) {
  48. throw new ServletException(e);
  49. } catch (final InstantiationException e) {
  50. throw new ServletException(e);
  51. } catch (final ClassNotFoundException e) {
  52. throw new ServletException(
  53. new InstantiationException(
  54. "Failed to load application class: "
  55. + applicationClassName));
  56. }
  57. }
  58. private String getApplicationRunnerApplicationClassName(
  59. HttpServletRequest request) {
  60. return getApplicationRunnerURIs(request).applicationClassname;
  61. }
  62. private static class URIS {
  63. String widgetsetPath;
  64. String applicationURI;
  65. String context;
  66. String runner;
  67. String applicationClassname;
  68. }
  69. /**
  70. * Parses application runner URIs.
  71. *
  72. * If request URL is e.g.
  73. * http://localhost:8080/vaadin/run/com.vaadin.demo.Calc then
  74. * <ul>
  75. * <li>context=vaadin</li>
  76. * <li>Runner servlet=run</li>
  77. * <li>Vaadin application=com.vaadin.demo.Calc</li>
  78. * </ul>
  79. *
  80. * @param request
  81. * @return string array containing widgetset URI, application URI and
  82. * context, runner, application classname
  83. */
  84. private static URIS getApplicationRunnerURIs(HttpServletRequest request) {
  85. final String[] urlParts = request.getRequestURI().toString().split(
  86. "\\/");
  87. String context = null;
  88. String runner = null;
  89. URIS uris = new URIS();
  90. String applicationClassname = null;
  91. String contextPath = request.getContextPath();
  92. if (urlParts[1].equals(contextPath.replaceAll("\\/", ""))) {
  93. // class name comes after web context and runner application
  94. context = urlParts[1];
  95. runner = urlParts[2];
  96. if (urlParts.length == 3) {
  97. throw new IllegalArgumentException("No application specified");
  98. }
  99. applicationClassname = urlParts[3];
  100. uris.widgetsetPath = "/" + context;
  101. uris.applicationURI = "/" + context + "/" + runner + "/"
  102. + applicationClassname;
  103. uris.context = context;
  104. uris.runner = runner;
  105. uris.applicationClassname = applicationClassname;
  106. } else {
  107. // no context
  108. context = "";
  109. runner = urlParts[1];
  110. if (urlParts.length == 2) {
  111. throw new IllegalArgumentException("No application specified");
  112. }
  113. applicationClassname = urlParts[2];
  114. uris.widgetsetPath = "/";
  115. uris.applicationURI = "/" + runner + "/" + applicationClassname;
  116. uris.context = context;
  117. uris.runner = runner;
  118. uris.applicationClassname = applicationClassname;
  119. }
  120. return uris;
  121. }
  122. // @Override
  123. @Override
  124. protected Class getApplicationClass() throws ClassNotFoundException {
  125. // TODO use getClassLoader() ?
  126. return getClass().getClassLoader().loadClass(applicationClassName);
  127. }
  128. @Override
  129. String getRequestPathInfo(HttpServletRequest request) {
  130. String path = request.getPathInfo();
  131. if (path == null) {
  132. return null;
  133. }
  134. path = path.substring(1 + applicationClassName.length());
  135. return path;
  136. }
  137. @Override
  138. String getWidgetsetLocation(HttpServletRequest request) {
  139. URIS uris = getApplicationRunnerURIs(request);
  140. String widgetsetPath = uris.widgetsetPath;
  141. if (widgetsetPath.equals("/")) {
  142. widgetsetPath = "";
  143. }
  144. return widgetsetPath;
  145. }
  146. }