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.

LegacyVaadinServlet.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 javax.servlet.ServletConfig;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServletRequest;
  20. public class LegacyVaadinServlet extends VaadinServlet {
  21. private static final UIProvider provider = new LegacyApplicationUIProvider() {
  22. @Override
  23. protected LegacyApplication createApplication() {
  24. VaadinServlet servlet = VaadinServlet.getCurrent();
  25. if (servlet instanceof LegacyVaadinServlet) {
  26. LegacyVaadinServlet legacyServlet = (LegacyVaadinServlet) servlet;
  27. HttpServletRequest request = VaadinServletService
  28. .getCurrentServletRequest();
  29. try {
  30. if (legacyServlet.shouldCreateApplication(request)) {
  31. return legacyServlet.getNewApplication(request);
  32. }
  33. } catch (ServletException e) {
  34. throw new RuntimeException(e);
  35. }
  36. }
  37. return null;
  38. }
  39. };
  40. @Override
  41. public void init(ServletConfig servletConfig) throws ServletException {
  42. super.init(servletConfig);
  43. getService().addSessionInitListener((SessionInitEvent event) -> {
  44. try {
  45. onVaadinSessionStarted(event.getRequest(), event.getSession());
  46. } catch (ServletException e) {
  47. throw new ServiceException(e);
  48. }
  49. });
  50. }
  51. protected Class<? extends LegacyApplication> getApplicationClass()
  52. throws ClassNotFoundException {
  53. try {
  54. return ServletPortletHelper.getLegacyApplicationClass(getService());
  55. } catch (ServiceException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. protected LegacyApplication getNewApplication(HttpServletRequest request)
  60. throws ServletException {
  61. try {
  62. Class<? extends LegacyApplication> applicationClass = getApplicationClass();
  63. return applicationClass.newInstance();
  64. } catch (Exception e) {
  65. throw new ServletException(e);
  66. }
  67. }
  68. protected boolean shouldCreateApplication(HttpServletRequest request)
  69. throws ServletException {
  70. return true;
  71. }
  72. private void onVaadinSessionStarted(VaadinRequest request,
  73. VaadinSession session) throws ServletException {
  74. session.addUIProvider(provider);
  75. }
  76. }