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.

CommunicationProblemHandler.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Response;
  19. import com.google.gwt.regexp.shared.MatchResult;
  20. import com.google.gwt.regexp.shared.RegExp;
  21. import com.google.gwt.user.client.Timer;
  22. import com.vaadin.client.ApplicationConnection;
  23. import com.vaadin.client.WidgetUtil;
  24. import elemental.json.JsonObject;
  25. /**
  26. * TODO
  27. *
  28. * @since
  29. * @author Vaadin Ltd
  30. */
  31. public class CommunicationProblemHandler {
  32. private ApplicationConnection connection;
  33. /**
  34. * Sets the application connection this queue is connected to
  35. *
  36. * @param connection
  37. * the application connection this queue is connected to
  38. */
  39. public void setConnection(ApplicationConnection connection) {
  40. this.connection = connection;
  41. }
  42. public static Logger getLogger() {
  43. return Logger.getLogger(CommunicationProblemHandler.class.getName());
  44. }
  45. /**
  46. * @param payload
  47. * @param communicationProblemEvent
  48. */
  49. public void xhrException(JsonObject payload, CommunicationProblemEvent event) {
  50. handleUnrecoverableCommunicationError(
  51. event.getException().getMessage(), event);
  52. }
  53. /**
  54. * @param event
  55. * @param retry
  56. */
  57. public void xhrInvalidStatusCode(final CommunicationProblemEvent event,
  58. boolean retry) {
  59. Response response = event.getResponse();
  60. int statusCode = response.getStatusCode();
  61. if (statusCode == 0) {
  62. handleNoConnection(event, retry);
  63. } else if (statusCode == 401) {
  64. handleAuthorizationFailed(event);
  65. } else if (statusCode == 503) {
  66. handleServiceUnavailable(event);
  67. } else if ((statusCode / 100) == 4) {
  68. // Handle all 4xx errors the same way as (they are
  69. // all permanent errors)
  70. String msg = "UIDL could not be read from server."
  71. + " Check servlets mappings. Error code: " + statusCode;
  72. handleUnrecoverableCommunicationError(msg, event);
  73. } else if ((statusCode / 100) == 5) {
  74. // Something's wrong on the server, there's nothing the
  75. // client can do except maybe try again.
  76. String msg = "Server error. Error code: " + statusCode;
  77. handleUnrecoverableCommunicationError(msg, event);
  78. return;
  79. }
  80. }
  81. /**
  82. * @since
  83. * @param event
  84. */
  85. private void handleServiceUnavailable(final CommunicationProblemEvent event) {
  86. /*
  87. * We'll assume msec instead of the usual seconds. If there's no
  88. * Retry-After header, handle the error like a 500, as per RFC 2616
  89. * section 10.5.4.
  90. */
  91. String delay = event.getResponse().getHeader("Retry-After");
  92. if (delay != null) {
  93. getLogger().warning("503, retrying in " + delay + "msec");
  94. (new Timer() {
  95. @Override
  96. public void run() {
  97. // doUidlRequest does not call startRequest so we do
  98. // not call endRequest before it
  99. getServerCommunicationHandler().doUidlRequest(
  100. event.getUri(), event.getPayload(), true);
  101. }
  102. }).schedule(Integer.parseInt(delay));
  103. return;
  104. } else {
  105. String msg = "Server error. Error code: "
  106. + event.getResponse().getStatusCode();
  107. handleUnrecoverableCommunicationError(msg, event);
  108. }
  109. }
  110. /**
  111. * @since
  112. * @param event
  113. */
  114. private void handleAuthorizationFailed(CommunicationProblemEvent event) {
  115. /*
  116. * Authorization has failed (401). Could be that the session has timed
  117. * out and the container is redirecting to a login page.
  118. */
  119. connection.showAuthenticationError("");
  120. endRequestAndStopApplication();
  121. }
  122. /**
  123. * @since
  124. */
  125. private void endRequestAndStopApplication() {
  126. getServerCommunicationHandler().endRequest();
  127. // Consider application not running any more and prevent all
  128. // future requests
  129. connection.setApplicationRunning(false);
  130. }
  131. /**
  132. * @since
  133. * @param event
  134. * @param retry
  135. */
  136. private void handleNoConnection(final CommunicationProblemEvent event,
  137. boolean retry) {
  138. if (retry) {
  139. /*
  140. * There are 2 situations where the error can pop up:
  141. *
  142. * 1) Request was most likely canceled because the browser is maybe
  143. * navigating away from the page. Just send the request again
  144. * without displaying any error in case the navigation isn't carried
  145. * through.
  146. *
  147. * 2) The browser failed to establish a network connection. This was
  148. * observed with keep-alive requests, and under wi-fi roaming
  149. * conditions.
  150. *
  151. * Status code 0 does indicate that there was no server side
  152. * processing, so we can retry the request.
  153. */
  154. getLogger().warning("Status code 0, retrying");
  155. (new Timer() {
  156. @Override
  157. public void run() {
  158. // doUidlRequest does not call startRequest so we do
  159. // not call endRequest before it
  160. getServerCommunicationHandler().doUidlRequest(
  161. event.getUri(), event.getPayload(), false);
  162. }
  163. }).schedule(100);
  164. } else {
  165. handleUnrecoverableCommunicationError(
  166. "Invalid status code 0 (server down?)", event);
  167. }
  168. }
  169. private void handleUnrecoverableCommunicationError(String details,
  170. CommunicationProblemEvent event) {
  171. Response response = event.getResponse();
  172. int statusCode = -1;
  173. if (response != null) {
  174. statusCode = response.getStatusCode();
  175. }
  176. connection.handleCommunicationError(details, statusCode);
  177. endRequestAndStopApplication();
  178. }
  179. /**
  180. * @since
  181. * @param event
  182. */
  183. public void xhrInvalidContent(CommunicationProblemEvent event) {
  184. String responseText = event.getResponse().getText();
  185. /*
  186. * A servlet filter or equivalent may have intercepted the request and
  187. * served non-UIDL content (for instance, a login page if the session
  188. * has expired.) If the response contains a magic substring, do a
  189. * synchronous refresh. See #8241.
  190. */
  191. MatchResult refreshToken = RegExp.compile(
  192. ApplicationConnection.UIDL_REFRESH_TOKEN
  193. + "(:\\s*(.*?))?(\\s|$)").exec(responseText);
  194. if (refreshToken != null) {
  195. WidgetUtil.redirect(refreshToken.getGroup(2));
  196. } else {
  197. handleUnrecoverableCommunicationError(
  198. "Invalid JSON response from server: " + responseText, event);
  199. }
  200. }
  201. private ServerCommunicationHandler getServerCommunicationHandler() {
  202. return connection.getServerCommunicationHandler();
  203. }
  204. }