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.

DefaultCommunicationProblemHandler.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.http.client.Request;
  19. import com.google.gwt.http.client.Response;
  20. import com.google.gwt.regexp.shared.MatchResult;
  21. import com.google.gwt.regexp.shared.RegExp;
  22. import com.google.gwt.user.client.Timer;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.WidgetUtil;
  25. import elemental.json.JsonObject;
  26. /**
  27. * Default implementation of the communication problem handler.
  28. *
  29. * Implements error handling by assuming all problems are terminal and simply
  30. * showing a notification to the user
  31. *
  32. * @since 7.6
  33. * @author Vaadin Ltd
  34. */
  35. public class DefaultCommunicationProblemHandler implements
  36. CommunicationProblemHandler {
  37. private ApplicationConnection connection;
  38. @Override
  39. public void setConnection(ApplicationConnection connection) {
  40. this.connection = connection;
  41. }
  42. protected ApplicationConnection getConnection() {
  43. return connection;
  44. }
  45. public static Logger getLogger() {
  46. return Logger.getLogger(DefaultCommunicationProblemHandler.class
  47. .getName());
  48. }
  49. @Override
  50. public void xhrException(CommunicationProblemEvent event) {
  51. handleUnrecoverableCommunicationError(
  52. event.getException().getMessage(), event);
  53. }
  54. @Override
  55. public void xhrInvalidStatusCode(final CommunicationProblemEvent event) {
  56. Response response = event.getResponse();
  57. int statusCode = response.getStatusCode();
  58. if (statusCode == 0) {
  59. handleNoConnection(event);
  60. } else if (statusCode == 401) {
  61. handleAuthorizationFailed(event);
  62. } else if (statusCode == 503) {
  63. handleServiceUnavailable(event);
  64. } else if ((statusCode / 100) == 4) {
  65. // Handle all 4xx errors the same way as (they are
  66. // all permanent errors)
  67. String msg = "UIDL could not be read from server."
  68. + " Check servlets mappings. Error code: " + statusCode;
  69. handleUnrecoverableCommunicationError(msg, event);
  70. } else if ((statusCode / 100) == 5) {
  71. // Something's wrong on the server, there's nothing the
  72. // client can do except maybe try again.
  73. String msg = "Server error. Error code: " + statusCode;
  74. handleUnrecoverableCommunicationError(msg, event);
  75. return;
  76. }
  77. }
  78. private void handleServiceUnavailable(final CommunicationProblemEvent event) {
  79. /*
  80. * We'll assume msec instead of the usual seconds. If there's no
  81. * Retry-After header, handle the error like a 500, as per RFC 2616
  82. * section 10.5.4.
  83. */
  84. String delay = event.getResponse().getHeader("Retry-After");
  85. if (delay != null) {
  86. getLogger().warning("503, retrying in " + delay + "msec");
  87. (new Timer() {
  88. @Override
  89. public void run() {
  90. // send does not call startRequest so we do
  91. // not call endRequest before it
  92. getServerCommunicationHandler().send(event.getPayload());
  93. }
  94. }).schedule(Integer.parseInt(delay));
  95. return;
  96. } else {
  97. String msg = "Server error. Error code: "
  98. + event.getResponse().getStatusCode();
  99. handleUnrecoverableCommunicationError(msg, event);
  100. }
  101. }
  102. private void handleAuthorizationFailed(CommunicationProblemEvent event) {
  103. /*
  104. * Authorization has failed (401). Could be that the session has timed
  105. * out and the container is redirecting to a login page.
  106. */
  107. connection.showAuthenticationError("");
  108. endRequestAndStopApplication();
  109. }
  110. private void endRequestAndStopApplication() {
  111. getServerCommunicationHandler().endRequest();
  112. // Consider application not running any more and prevent all
  113. // future requests
  114. connection.setApplicationRunning(false);
  115. }
  116. private void handleNoConnection(final CommunicationProblemEvent event) {
  117. handleUnrecoverableCommunicationError(
  118. "Invalid status code 0 (server down?)", event);
  119. }
  120. private void handleUnrecoverableCommunicationError(String details,
  121. CommunicationProblemEvent event) {
  122. Response response = event.getResponse();
  123. int statusCode = -1;
  124. if (response != null) {
  125. statusCode = response.getStatusCode();
  126. }
  127. connection.handleCommunicationError(details, statusCode);
  128. endRequestAndStopApplication();
  129. }
  130. @Override
  131. public void xhrInvalidContent(CommunicationProblemEvent event) {
  132. String responseText = event.getResponse().getText();
  133. /*
  134. * A servlet filter or equivalent may have intercepted the request and
  135. * served non-UIDL content (for instance, a login page if the session
  136. * has expired.) If the response contains a magic substring, do a
  137. * synchronous refresh. See #8241.
  138. */
  139. MatchResult refreshToken = RegExp.compile(
  140. ApplicationConnection.UIDL_REFRESH_TOKEN
  141. + "(:\\s*(.*?))?(\\s|$)").exec(responseText);
  142. if (refreshToken != null) {
  143. WidgetUtil.redirect(refreshToken.getGroup(2));
  144. } else {
  145. handleUnrecoverableCommunicationError(
  146. "Invalid JSON response from server: " + responseText, event);
  147. }
  148. }
  149. @Override
  150. public void pushInvalidContent(PushConnection pushConnection, String message) {
  151. // Do nothing for now. Should likely do the same as xhrInvalidContent
  152. }
  153. private ServerCommunicationHandler getServerCommunicationHandler() {
  154. return connection.getServerCommunicationHandler();
  155. }
  156. @Override
  157. public boolean heartbeatInvalidStatusCode(Request request, Response response) {
  158. int status = response.getStatusCode();
  159. int interval = connection.getHeartbeat().getInterval();
  160. if (status == 0) {
  161. getLogger().warning(
  162. "Failed sending heartbeat, server is unreachable, retrying in "
  163. + interval + "secs.");
  164. } else if (status == Response.SC_GONE) {
  165. // FIXME Stop application?
  166. connection.showSessionExpiredError(null);
  167. // If session is expired break the loop
  168. return false;
  169. } else if (status >= 500) {
  170. getLogger().warning(
  171. "Failed sending heartbeat, see server logs, retrying in "
  172. + interval + "secs.");
  173. } else {
  174. getLogger()
  175. .warning(
  176. "Failed sending heartbeat to server. Error code: "
  177. + status);
  178. }
  179. return true;
  180. }
  181. @Override
  182. public boolean heartbeatException(Request request, Throwable exception) {
  183. getLogger().severe(
  184. "Exception sending heartbeat: " + exception.getMessage());
  185. return true;
  186. }
  187. @Override
  188. public void pushError(PushConnection pushConnection) {
  189. connection.handleCommunicationError("Push connection using "
  190. + pushConnection.getTransportType() + " failed!", -1);
  191. }
  192. @Override
  193. public void pushClientTimeout(PushConnection pushConnection) {
  194. connection
  195. .handleCommunicationError(
  196. "Client unexpectedly disconnected. Ensure client timeout is disabled.",
  197. -1);
  198. }
  199. @Override
  200. public void pushScriptLoadError(String resourceUrl) {
  201. connection.handleCommunicationError(resourceUrl
  202. + " could not be loaded. Push will not work.", 0);
  203. }
  204. @Override
  205. public void heartbeatOk() {
  206. getLogger().fine("Heartbeat response OK");
  207. }
  208. @Override
  209. public void xhrOk() {
  210. }
  211. @Override
  212. public void pushClosed(PushConnection pushConnection) {
  213. }
  214. @Override
  215. public void pushReconnectPending(PushConnection pushConnection) {
  216. }
  217. @Override
  218. public void pushOk(PushConnection pushConnection) {
  219. }
  220. @Override
  221. public void pushNotConnected(JsonObject payload) {
  222. }
  223. }