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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(),
  46. event.getSession());
  47. } catch (ServletException e) {
  48. throw new ServiceException(e);
  49. }
  50. });
  51. }
  52. protected Class<? extends LegacyApplication> getApplicationClass()
  53. throws ClassNotFoundException {
  54. try {
  55. return ServletPortletHelper.getLegacyApplicationClass(getService());
  56. } catch (ServiceException e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. protected LegacyApplication getNewApplication(HttpServletRequest request)
  61. throws ServletException {
  62. try {
  63. Class<? extends LegacyApplication> applicationClass = getApplicationClass();
  64. return applicationClass.newInstance();
  65. } catch (Exception e) {
  66. throw new ServletException(e);
  67. }
  68. }
  69. protected boolean shouldCreateApplication(HttpServletRequest request)
  70. throws ServletException {
  71. return true;
  72. }
  73. private void onVaadinSessionStarted(VaadinRequest request,
  74. VaadinSession session) throws ServletException {
  75. session.addUIProvider(provider);
  76. }
  77. }