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.

DefaultConnectionStateHandler.java 19KB

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