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.

HeartbeatHandler.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2000-2018 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.communication;
  17. import java.io.IOException;
  18. import javax.servlet.http.HttpServletResponse;
  19. import com.vaadin.server.ServletPortletHelper;
  20. import com.vaadin.server.SessionExpiredHandler;
  21. import com.vaadin.server.SynchronizedRequestHandler;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.server.VaadinResponse;
  24. import com.vaadin.server.VaadinSession;
  25. import com.vaadin.shared.ui.ui.UIConstants;
  26. import com.vaadin.ui.UI;
  27. /**
  28. * Handles heartbeat requests. Heartbeat requests are periodically sent by the
  29. * client-side to inform the server that the UI sending the heartbeat is still
  30. * alive (the browser window is open, the connection is up) even when there are
  31. * no UIDL requests for a prolonged period of time. UIs that do not receive
  32. * either heartbeat or UIDL requests are eventually removed from the session and
  33. * garbage collected.
  34. *
  35. * @author Vaadin Ltd
  36. * @since 7.1
  37. */
  38. public class HeartbeatHandler extends SynchronizedRequestHandler
  39. implements SessionExpiredHandler {
  40. @Override
  41. protected boolean canHandleRequest(VaadinRequest request) {
  42. return ServletPortletHelper.isHeartbeatRequest(request);
  43. }
  44. /**
  45. * Handles a heartbeat request for the given session. Reads the GET
  46. * parameter named {@link UIConstants#UI_ID_PARAMETER} to identify the UI.
  47. * If the UI is found in the session, sets it
  48. * {@link UI#getLastHeartbeatTimestamp() heartbeat timestamp} to the current
  49. * time. Otherwise, writes a HTTP Not Found error to the response.
  50. */
  51. @Override
  52. public boolean synchronizedHandleRequest(VaadinSession session,
  53. VaadinRequest request, VaadinResponse response) throws IOException {
  54. UI ui = session.getService().findUI(request);
  55. if (ui != null) {
  56. ui.setLastHeartbeatTimestamp(System.currentTimeMillis());
  57. // Ensure that the browser does not cache heartbeat responses.
  58. // iOS 6 Safari requires this (#10370)
  59. response.setHeader("Cache-Control", "no-cache");
  60. // If Content-Type is not set, browsers assume text/html and may
  61. // complain about the empty response body (#12182)
  62. response.setHeader("Content-Type", "text/plain");
  63. } else {
  64. response.sendError(HttpServletResponse.SC_NOT_FOUND,
  65. "UI not found");
  66. }
  67. return true;
  68. }
  69. /*
  70. * (non-Javadoc)
  71. *
  72. * @see
  73. * com.vaadin.server.SessionExpiredHandler#handleSessionExpired(com.vaadin
  74. * .server.VaadinRequest, com.vaadin.server.VaadinResponse)
  75. */
  76. @Override
  77. public boolean handleSessionExpired(VaadinRequest request,
  78. VaadinResponse response) throws IOException {
  79. if (!ServletPortletHelper.isHeartbeatRequest(request)) {
  80. return false;
  81. }
  82. // Ensure that the browser does not cache expired response.
  83. // iOS 6 Safari requires this (#10370)
  84. response.setHeader("Cache-Control", "no-cache");
  85. // If Content-Type is not set, browsers assume text/html and may
  86. // complain about the empty response body (#12182)
  87. response.setHeader("Content-Type", "text/plain");
  88. response.sendError(HttpServletResponse.SC_NOT_FOUND, "Session expired");
  89. return true;
  90. }
  91. }