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.

ReconnectingCommunicationProblemHandler.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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.client.communication;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.core.shared.GWT;
  19. import com.google.gwt.http.client.Request;
  20. import com.google.gwt.http.client.Response;
  21. import com.google.gwt.regexp.shared.MatchResult;
  22. import com.google.gwt.regexp.shared.RegExp;
  23. import com.google.gwt.user.client.Timer;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
  26. import com.vaadin.client.ApplicationConnection.ApplicationStoppedHandler;
  27. import com.vaadin.client.WidgetUtil;
  28. import com.vaadin.shared.ui.ui.UIState.ReconnectDialogConfigurationState;
  29. import elemental.json.JsonObject;
  30. /**
  31. * Default implementation of the communication problem handler.
  32. * <p>
  33. * Handles temporary errors by showing a reconnect dialog to the user while
  34. * trying to re-establish the connection to the server and re-send the pending
  35. * message.
  36. * <p>
  37. * Handles permanent errors by showing a critical system notification to the
  38. * user
  39. *
  40. * @since 7.6
  41. * @author Vaadin Ltd
  42. */
  43. public class ReconnectingCommunicationProblemHandler implements
  44. CommunicationProblemHandler {
  45. private ApplicationConnection connection;
  46. private ReconnectDialog reconnectDialog = GWT.create(ReconnectDialog.class);
  47. private int reconnectAttempt = 0;
  48. private Type reconnectionCause = null;
  49. private Timer scheduledReconnect;
  50. private Timer dialogShowTimer = new Timer() {
  51. @Override
  52. public void run() {
  53. showDialog();
  54. }
  55. };
  56. protected enum Type {
  57. HEARTBEAT(0), PUSH(1), XHR(2);
  58. private int priority;
  59. private Type(int priority) {
  60. this.priority = priority;
  61. }
  62. public boolean isMessage() {
  63. return this == PUSH || this == XHR;
  64. }
  65. /**
  66. * Checks if this type is of higher priority than the given type
  67. *
  68. * @param type
  69. * the type to compare to
  70. * @return true if this type has higher priority than the given type,
  71. * false otherwise
  72. */
  73. public boolean isHigherPriorityThan(Type type) {
  74. return priority > type.priority;
  75. }
  76. }
  77. @Override
  78. public void setConnection(ApplicationConnection connection) {
  79. this.connection = connection;
  80. connection.addHandler(ApplicationStoppedEvent.TYPE,
  81. new ApplicationStoppedHandler() {
  82. @Override
  83. public void onApplicationStopped(
  84. ApplicationStoppedEvent event) {
  85. if (isReconnecting()) {
  86. giveUp();
  87. }
  88. if (scheduledReconnect != null
  89. && scheduledReconnect.isRunning()) {
  90. scheduledReconnect.cancel();
  91. }
  92. }
  93. });
  94. };
  95. /**
  96. * Checks if we are currently trying to reconnect
  97. *
  98. * @return true if we have noted a problem and are trying to re-establish
  99. * server connection, false otherwise
  100. */
  101. private boolean isReconnecting() {
  102. return reconnectionCause != null;
  103. }
  104. private static Logger getLogger() {
  105. return Logger.getLogger(ReconnectingCommunicationProblemHandler.class
  106. .getName());
  107. }
  108. /**
  109. * Returns the connection this handler is connected to
  110. *
  111. * @return the connection for this handler
  112. */
  113. protected ApplicationConnection getConnection() {
  114. return connection;
  115. }
  116. @Override
  117. public void xhrException(CommunicationProblemEvent event) {
  118. debug("xhrException");
  119. handleRecoverableError(Type.XHR, event.getPayload());
  120. }
  121. @Override
  122. public void heartbeatException(Request request, Throwable exception) {
  123. getLogger().severe("Heartbeat exception: " + exception.getMessage());
  124. handleRecoverableError(Type.HEARTBEAT, null);
  125. }
  126. @Override
  127. public void heartbeatInvalidStatusCode(Request request, Response response) {
  128. int statusCode = response.getStatusCode();
  129. getLogger().warning("Heartbeat request returned " + statusCode);
  130. if (response.getStatusCode() == Response.SC_GONE) {
  131. // Session expired
  132. getConnection().showSessionExpiredError(null);
  133. stopApplication();
  134. } else if (response.getStatusCode() == Response.SC_NOT_FOUND) {
  135. // UI closed, do nothing as the UI will react to this
  136. // Should not trigger reconnect dialog as this will prevent user
  137. // input
  138. } else {
  139. handleRecoverableError(Type.HEARTBEAT, null);
  140. }
  141. }
  142. @Override
  143. public void heartbeatOk() {
  144. debug("heartbeatOk");
  145. if (isReconnecting()) {
  146. resolveTemporaryError(Type.HEARTBEAT);
  147. }
  148. }
  149. private void debug(String msg) {
  150. if (false) {
  151. getLogger().warning(msg);
  152. }
  153. }
  154. /**
  155. * Called whenever an error occurs in communication which should be handled
  156. * by showing the reconnect dialog and retrying communication until
  157. * successful again
  158. *
  159. * @param type
  160. * The type of failure detected
  161. * @param payload
  162. * The message which did not reach the server, or null if no
  163. * message was involved (heartbeat or push connection failed)
  164. */
  165. protected void handleRecoverableError(Type type, final JsonObject payload) {
  166. debug("handleTemporaryError(" + type + ")");
  167. if (!connection.isApplicationRunning()) {
  168. return;
  169. }
  170. if (!isReconnecting()) {
  171. // First problem encounter
  172. reconnectionCause = type;
  173. getLogger().warning("Reconnecting because of " + type + " failure");
  174. // Precaution only as there should never be a dialog at this point
  175. // and no timer running
  176. stopDialogTimer();
  177. if (isDialogVisible()) {
  178. hideDialog();
  179. }
  180. // Show dialog after grace period, still continue to try to
  181. // reconnect even before it is shown
  182. dialogShowTimer.schedule(getConfiguration().dialogGracePeriod);
  183. } else {
  184. // We are currently trying to reconnect
  185. // Priority is HEARTBEAT -> PUSH -> XHR
  186. // If a higher priority issues is resolved, we can assume the lower
  187. // one will be also
  188. if (type.isHigherPriorityThan(reconnectionCause)) {
  189. getLogger().warning(
  190. "Now reconnecting because of " + type + " failure");
  191. reconnectionCause = type;
  192. }
  193. }
  194. if (reconnectionCause != type) {
  195. return;
  196. }
  197. reconnectAttempt++;
  198. getLogger().info(
  199. "Reconnect attempt " + reconnectAttempt + " for " + type);
  200. if (reconnectAttempt >= getConfiguration().reconnectAttempts) {
  201. // Max attempts reached, stop trying
  202. giveUp();
  203. } else {
  204. updateDialog();
  205. scheduleReconnect(payload);
  206. }
  207. }
  208. /**
  209. * Called after a problem occurred.
  210. *
  211. * This method is responsible for re-sending the payload to the server (if
  212. * not null) or re-send a heartbeat request at some point
  213. *
  214. * @param payload
  215. * the payload that did not reach the server, null if the problem
  216. * was detected by a heartbeat
  217. */
  218. protected void scheduleReconnect(final JsonObject payload) {
  219. // Here and not in timer to avoid TB for getting in between
  220. // The request is still open at this point to avoid interference, so we
  221. // do not need to start a new one
  222. if (reconnectAttempt == 1) {
  223. // Try once immediately
  224. doReconnect(payload);
  225. } else {
  226. scheduledReconnect = new Timer() {
  227. @Override
  228. public void run() {
  229. scheduledReconnect = null;
  230. doReconnect(payload);
  231. }
  232. };
  233. scheduledReconnect.schedule(getConfiguration().reconnectInterval);
  234. }
  235. }
  236. /**
  237. * Re-sends the payload to the server (if not null) or re-sends a heartbeat
  238. * request immediately
  239. *
  240. * @param payload
  241. * the payload that did not reach the server, null if the problem
  242. * was detected by a heartbeat
  243. */
  244. protected void doReconnect(JsonObject payload) {
  245. if (!connection.isApplicationRunning()) {
  246. // This should not happen as nobody should call this if the
  247. // application has been stopped
  248. getLogger()
  249. .warning(
  250. "Trying to reconnect after application has been stopped. Giving up");
  251. return;
  252. }
  253. if (payload != null) {
  254. getLogger().info("Re-sending last message to the server...");
  255. getConnection().getServerCommunicationHandler().send(payload);
  256. } else {
  257. // Use heartbeat
  258. getLogger().info("Trying to re-establish server connection...");
  259. getConnection().getHeartbeat().send();
  260. }
  261. }
  262. /**
  263. * Called whenever a reconnect attempt fails to allow updating of dialog
  264. * contents
  265. */
  266. protected void updateDialog() {
  267. reconnectDialog.setText(getDialogText(reconnectAttempt));
  268. }
  269. /**
  270. * Called when we should give up trying to reconnect and let the user decide
  271. * how to continue
  272. *
  273. */
  274. protected void giveUp() {
  275. reconnectionCause = null;
  276. endRequest();
  277. stopDialogTimer();
  278. if (!isDialogVisible()) {
  279. // It SHOULD always be visible at this point, unless you have a
  280. // really strange configuration (grace time longer than total
  281. // reconnect time)
  282. showDialog();
  283. }
  284. reconnectDialog.setText(getDialogTextGaveUp(reconnectAttempt));
  285. reconnectDialog.setReconnecting(false);
  286. // Stopping the application stops heartbeats and push
  287. connection.setApplicationRunning(false);
  288. }
  289. /**
  290. * Ensures the reconnect dialog does not popup some time from now
  291. */
  292. private void stopDialogTimer() {
  293. if (dialogShowTimer.isRunning()) {
  294. dialogShowTimer.cancel();
  295. }
  296. }
  297. /**
  298. * Checks if the reconnect dialog is visible to the user
  299. *
  300. * @return true if the user can see the dialog, false otherwise
  301. */
  302. protected boolean isDialogVisible() {
  303. return reconnectDialog.isVisible();
  304. }
  305. /**
  306. * Called when the reconnect dialog should be shown. This is typically when
  307. * N seconds has passed since a problem with the connection has been
  308. * detected
  309. */
  310. protected void showDialog() {
  311. reconnectDialog.setReconnecting(true);
  312. reconnectDialog.show(connection);
  313. // We never want to show loading indicator and reconnect dialog at the
  314. // same time
  315. connection.getLoadingIndicator().hide();
  316. }
  317. /**
  318. * Called when the reconnect dialog should be hidden.
  319. */
  320. protected void hideDialog() {
  321. reconnectDialog.hide();
  322. }
  323. /**
  324. * Gets the text to show in the reconnect dialog after giving up (reconnect
  325. * limit reached)
  326. *
  327. * @param reconnectAttempt
  328. * The number of the current reconnection attempt
  329. * @return The text to show in the reconnect dialog after giving up
  330. */
  331. protected String getDialogTextGaveUp(int reconnectAttempt) {
  332. return getConfiguration().dialogTextGaveUp.replace("{0}",
  333. reconnectAttempt + "");
  334. }
  335. /**
  336. * Gets the text to show in the reconnect dialog
  337. *
  338. * @param reconnectAttempt
  339. * The number of the current reconnection attempt
  340. * @return The text to show in the reconnect dialog
  341. */
  342. protected String getDialogText(int reconnectAttempt) {
  343. return getConfiguration().dialogText.replace("{0}", reconnectAttempt
  344. + "");
  345. }
  346. @Override
  347. public void configurationUpdated() {
  348. // All other properties are fetched directly from the state when needed
  349. reconnectDialog.setModal(getConfiguration().dialogModal);
  350. }
  351. private ReconnectDialogConfigurationState getConfiguration() {
  352. return connection.getUIConnector().getState().reconnectDialogConfiguration;
  353. }
  354. @Override
  355. public void xhrInvalidContent(CommunicationProblemEvent event) {
  356. debug("xhrInvalidContent");
  357. endRequest();
  358. String responseText = event.getResponse().getText();
  359. /*
  360. * A servlet filter or equivalent may have intercepted the request and
  361. * served non-UIDL content (for instance, a login page if the session
  362. * has expired.) If the response contains a magic substring, do a
  363. * synchronous refresh. See #8241.
  364. */
  365. MatchResult refreshToken = RegExp.compile(
  366. ApplicationConnection.UIDL_REFRESH_TOKEN
  367. + "(:\\s*(.*?))?(\\s|$)").exec(responseText);
  368. if (refreshToken != null) {
  369. WidgetUtil.redirect(refreshToken.getGroup(2));
  370. } else {
  371. handleUnrecoverableCommunicationError(
  372. "Invalid JSON response from server: " + responseText, event);
  373. }
  374. }
  375. @Override
  376. public void pushInvalidContent(PushConnection pushConnection, String message) {
  377. debug("pushInvalidContent");
  378. if (pushConnection.isBidirectional()) {
  379. // We can't be sure that what was pushed was actually a response but
  380. // at this point it should not really matter, as something is
  381. // seriously broken.
  382. endRequest();
  383. }
  384. // Do nothing special for now. Should likely do the same as
  385. // xhrInvalidContent
  386. handleUnrecoverableCommunicationError("Invalid JSON from server: "
  387. + message, null);
  388. }
  389. @Override
  390. public void xhrInvalidStatusCode(CommunicationProblemEvent event) {
  391. debug("xhrInvalidStatusCode");
  392. Response response = event.getResponse();
  393. int statusCode = response.getStatusCode();
  394. getLogger().warning("Server returned " + statusCode + " for xhr");
  395. if (statusCode == 401) {
  396. // Authentication/authorization failed, no need to re-try
  397. endRequest();
  398. handleUnauthorized(event);
  399. return;
  400. } else {
  401. // 404, 408 and other 4xx codes CAN be temporary when you have a
  402. // proxy between the client and the server and e.g. restart the
  403. // server
  404. // 5xx codes may or may not be temporary
  405. handleRecoverableError(Type.XHR, event.getPayload());
  406. }
  407. }
  408. /**
  409. * @since
  410. */
  411. private void endRequest() {
  412. getConnection().getServerCommunicationHandler().endRequest();
  413. }
  414. protected void handleUnauthorized(CommunicationProblemEvent event) {
  415. /*
  416. * Authorization has failed (401). Could be that the session has timed
  417. * out.
  418. */
  419. connection.showAuthenticationError("");
  420. stopApplication();
  421. }
  422. private void stopApplication() {
  423. // Consider application not running any more and prevent all
  424. // future requests
  425. connection.setApplicationRunning(false);
  426. }
  427. private void handleUnrecoverableCommunicationError(String details,
  428. CommunicationProblemEvent event) {
  429. int statusCode = -1;
  430. if (event != null) {
  431. Response response = event.getResponse();
  432. if (response != null) {
  433. statusCode = response.getStatusCode();
  434. }
  435. }
  436. connection.handleCommunicationError(details, statusCode);
  437. stopApplication();
  438. }
  439. @Override
  440. public void xhrOk() {
  441. debug("xhrOk");
  442. if (isReconnecting()) {
  443. resolveTemporaryError(Type.XHR);
  444. }
  445. }
  446. private void resolveTemporaryError(Type type) {
  447. debug("resolveTemporaryError(" + type + ")");
  448. if (reconnectionCause != type) {
  449. // Waiting for some other problem to be resolved
  450. return;
  451. }
  452. reconnectionCause = null;
  453. reconnectAttempt = 0;
  454. hideDialog();
  455. getLogger().info("Re-established connection to server");
  456. }
  457. @Override
  458. public void pushOk(PushConnection pushConnection) {
  459. debug("pushOk()");
  460. if (isReconnecting()) {
  461. resolveTemporaryError(Type.PUSH);
  462. }
  463. }
  464. @Override
  465. public void pushScriptLoadError(String resourceUrl) {
  466. connection.handleCommunicationError(resourceUrl
  467. + " could not be loaded. Push will not work.", 0);
  468. }
  469. @Override
  470. public void pushNotConnected(JsonObject payload) {
  471. debug("pushNotConnected()");
  472. handleRecoverableError(Type.PUSH, payload);
  473. }
  474. @Override
  475. public void pushReconnectPending(PushConnection pushConnection) {
  476. debug("pushReconnectPending(" + pushConnection.getTransportType() + ")");
  477. getLogger().info("Reopening push connection");
  478. if (pushConnection.isBidirectional()) {
  479. // Lost connection for a connection which will tell us when the
  480. // connection is available again
  481. handleRecoverableError(Type.PUSH, null);
  482. } else {
  483. // Lost connection for a connection we do not necessarily know when
  484. // it is available again (long polling behind proxy). Do nothing and
  485. // show reconnect dialog if the user does something and the XHR
  486. // fails
  487. }
  488. }
  489. @Override
  490. public void pushError(PushConnection pushConnection) {
  491. debug("pushError()");
  492. connection.handleCommunicationError("Push connection using "
  493. + pushConnection.getTransportType() + " failed!", -1);
  494. }
  495. @Override
  496. public void pushClientTimeout(PushConnection pushConnection) {
  497. debug("pushClientTimeout()");
  498. // TODO Reconnect, allowing client timeout to be set
  499. // https://dev.vaadin.com/ticket/18429
  500. connection
  501. .handleCommunicationError(
  502. "Client unexpectedly disconnected. Ensure client timeout is disabled.",
  503. -1);
  504. }
  505. @Override
  506. public void pushClosed(PushConnection pushConnection) {
  507. debug("pushClosed()");
  508. getLogger().info("Push connection closed");
  509. }
  510. }