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.

CommErrorEmulatorServlet.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2000-2014 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.tests.application;
  17. import java.io.IOException;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.vaadin.server.DeploymentConfiguration;
  23. import com.vaadin.server.RequestHandler;
  24. import com.vaadin.server.ServiceException;
  25. import com.vaadin.server.VaadinRequest;
  26. import com.vaadin.server.VaadinResponse;
  27. import com.vaadin.server.VaadinServlet;
  28. import com.vaadin.server.VaadinServletService;
  29. import com.vaadin.server.VaadinSession;
  30. import com.vaadin.server.communication.HeartbeatHandler;
  31. import com.vaadin.server.communication.UidlRequestHandler;
  32. import com.vaadin.ui.UI;
  33. public class CommErrorEmulatorServlet extends VaadinServlet {
  34. private Map<UI, Integer> uidlResponseCode = Collections
  35. .synchronizedMap(new HashMap<UI, Integer>());
  36. private Map<UI, Integer> heartbeatResponseCode = Collections
  37. .synchronizedMap(new HashMap<UI, Integer>());
  38. private final CommErrorUIDLRequestHandler uidlHandler = new CommErrorUIDLRequestHandler();
  39. private final CommErrorHeartbeatHandler heartbeatHandler = new CommErrorHeartbeatHandler();
  40. public class CommErrorUIDLRequestHandler extends UidlRequestHandler {
  41. @Override
  42. public boolean synchronizedHandleRequest(VaadinSession session,
  43. VaadinRequest request, VaadinResponse response)
  44. throws IOException {
  45. UI ui = session.getService().findUI(request);
  46. if (ui != null && uidlResponseCode.containsKey(ui)) {
  47. response.sendError(uidlResponseCode.get(ui), "Error set in UI");
  48. return true;
  49. }
  50. return super.synchronizedHandleRequest(session, request, response);
  51. }
  52. }
  53. public class CommErrorHeartbeatHandler extends HeartbeatHandler {
  54. @Override
  55. public boolean synchronizedHandleRequest(VaadinSession session,
  56. VaadinRequest request, VaadinResponse response)
  57. throws IOException {
  58. UI ui = session.getService().findUI(request);
  59. if (ui != null && heartbeatResponseCode.containsKey(ui)) {
  60. response.sendError(heartbeatResponseCode.get(ui),
  61. "Error set in UI");
  62. return true;
  63. }
  64. return super.synchronizedHandleRequest(session, request, response);
  65. }
  66. }
  67. public class CommErrorEmulatorService extends VaadinServletService {
  68. public CommErrorEmulatorService(VaadinServlet servlet,
  69. DeploymentConfiguration deploymentConfiguration)
  70. throws ServiceException {
  71. super(servlet, deploymentConfiguration);
  72. }
  73. @Override
  74. protected List<RequestHandler> createRequestHandlers()
  75. throws ServiceException {
  76. List<RequestHandler> handlers = super.createRequestHandlers();
  77. handlers.add(uidlHandler);
  78. handlers.add(heartbeatHandler);
  79. return handlers;
  80. }
  81. }
  82. @Override
  83. protected VaadinServletService createServletService(
  84. DeploymentConfiguration deploymentConfiguration)
  85. throws ServiceException {
  86. CommErrorEmulatorService s = new CommErrorEmulatorService(this,
  87. deploymentConfiguration);
  88. s.init();
  89. return s;
  90. }
  91. public void setUIDLResponseCode(final UI ui, int responseCode,
  92. final int delay) {
  93. uidlResponseCode.put(ui, responseCode);
  94. System.out.println("Responding with " + responseCode
  95. + " to UIDL requests for " + ui + " for the next " + delay
  96. + "s");
  97. new Thread(new Runnable() {
  98. @Override
  99. public void run() {
  100. try {
  101. Thread.sleep(delay * 1000);
  102. } catch (InterruptedException e) {
  103. e.printStackTrace();
  104. }
  105. System.out.println("Handing UIDL requests normally again");
  106. uidlResponseCode.remove(ui);
  107. }
  108. }).start();
  109. }
  110. public void setHeartbeatResponseCode(final UI ui, int responseCode,
  111. final int delay) {
  112. heartbeatResponseCode.put(ui, responseCode);
  113. System.out.println("Responding with " + responseCode
  114. + " to heartbeat requests for " + ui + " for the next " + delay
  115. + "s");
  116. new Thread(new Runnable() {
  117. @Override
  118. public void run() {
  119. try {
  120. Thread.sleep(delay * 1000);
  121. } catch (InterruptedException e) {
  122. e.printStackTrace();
  123. }
  124. System.out.println("Handing heartbeat requests normally again");
  125. heartbeatResponseCode.remove(ui);
  126. }
  127. }).start();
  128. }
  129. }