Changeset 18442 in svn
- Timestamp:
- 04/26/11 08:38:34 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
versions/6.6/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
r18161 r18442 397 397 startRequest(); 398 398 // Security: double cookie submission pattern 399 final String rd = uidlSecurityKey + VAR_BURST_SEPARATOR + requestData; 400 VConsole.log("Making UIDL Request with params: " + rd); 399 final String payload = uidlSecurityKey + VAR_BURST_SEPARATOR 400 + requestData; 401 VConsole.log("Making UIDL Request with params: " + payload); 401 402 String uri; 402 403 if (configuration.usePortletURLs()) { … … 413 414 } 414 415 415 if (!forceSync) { 416 final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, 417 uri); 418 // TODO enable timeout 419 // rb.setTimeoutMillis(timeoutMillis); 420 rb.setHeader("Content-Type", "text/plain;charset=utf-8"); 416 doUidlRequest(uri, payload, forceSync); 417 418 } 419 420 /** 421 * Sends an asynchronous or synchronous UIDL request to the server using the 422 * given URI. 423 * 424 * @param uri 425 * The URI to use for the request. May includes GET parameters 426 * @param payload 427 * The contents of the request to send 428 * @param synchronous 429 * true if the request should be synchronous, false otherwise 430 */ 431 protected void doUidlRequest(final String uri, final String payload, 432 final boolean synchronous) { 433 if (!synchronous) { 434 RequestCallback requestCallback = new RequestCallback() { 435 public void onError(Request request, Throwable exception) { 436 showCommunicationError(exception.getMessage()); 437 endRequest(); 438 if (!applicationRunning) { 439 // start failed, let's try to start the next app 440 ApplicationConfiguration.startNextApplication(); 441 } 442 } 443 444 public void onResponseReceived(Request request, 445 Response response) { 446 VConsole.log("Server visit took " 447 + String.valueOf((new Date()).getTime() 448 - requestStartTime.getTime()) + "ms"); 449 450 int statusCode = response.getStatusCode(); 451 452 switch (statusCode) { 453 case 0: 454 showCommunicationError("Invalid status code 0 (server down?)"); 455 endRequest(); 456 return; 457 458 case 401: 459 /* 460 * Authorization has failed. Could be that the session 461 * has timed out and the container is redirecting to a 462 * login page. 463 */ 464 showAuthenticationError(""); 465 endRequest(); 466 return; 467 468 case 503: 469 // We'll assume msec instead of the usual seconds 470 int delay = Integer.parseInt(response 471 .getHeader("Retry-After")); 472 VConsole.log("503, retrying in " + delay + "msec"); 473 (new Timer() { 474 @Override 475 public void run() { 476 activeRequests--; 477 doUidlRequest(uri, payload, synchronous); 478 } 479 }).schedule(delay); 480 return; 481 482 } 483 484 if ((statusCode / 100) == 4) { 485 // Handle all 4xx errors the same way as (they are 486 // all permanent errors) 487 showCommunicationError("UIDL could not be read from server. Check servlets mappings. Error code: " 488 + statusCode); 489 endRequest(); 490 return; 491 } 492 493 final Date start = new Date(); 494 // for(;;);[realjson] 495 final String jsonText = response.getText().substring(9, 496 response.getText().length() - 1); 497 final ValueMap json; 498 try { 499 json = parseJSONResponse(jsonText); 500 } catch (final Exception e) { 501 endRequest(); 502 showCommunicationError(e.getMessage() 503 + " - Original JSON-text:" + jsonText); 504 return; 505 } 506 507 VConsole.log("JSON parsing took " 508 + (new Date().getTime() - start.getTime()) + "ms"); 509 if (applicationRunning) { 510 handleReceivedJSONMessage(start, jsonText, json); 511 } else { 512 applicationRunning = true; 513 handleWhenCSSLoaded(jsonText, json); 514 ApplicationConfiguration.startNextApplication(); 515 } 516 } 517 518 }; 421 519 try { 422 rb.sendRequest(rd, new RequestCallback() { 423 public void onError(Request request, Throwable exception) { 424 showCommunicationError(exception.getMessage()); 425 endRequest(); 426 if (!applicationRunning) { 427 // start failed, let's try to start the next app 428 ApplicationConfiguration.startNextApplication(); 429 } 430 } 431 432 public void onResponseReceived(Request request, 433 Response response) { 434 VConsole.log("Server visit took " 435 + String.valueOf((new Date()).getTime() 436 - requestStartTime.getTime()) + "ms"); 437 438 int statusCode = response.getStatusCode(); 439 440 switch (statusCode) { 441 case 0: 442 showCommunicationError("Invalid status code 0 (server down?)"); 443 endRequest(); 444 return; 445 446 case 401: 447 /* 448 * Authorization has failed. Could be that the 449 * session has timed out and the container is 450 * redirecting to a login page. 451 */ 452 showAuthenticationError(""); 453 endRequest(); 454 return; 455 456 case 503: 457 // We'll assume msec instead of the usual seconds 458 int delay = Integer.parseInt(response 459 .getHeader("Retry-After")); 460 VConsole.log("503, retrying in " + delay + "msec"); 461 (new Timer() { 462 @Override 463 public void run() { 464 activeRequests--; 465 makeUidlRequest(requestData, extraParams, 466 forceSync); 467 } 468 }).schedule(delay); 469 return; 470 471 } 472 473 if ((statusCode / 100) == 4) { 474 // Handle all 4xx errors the same way as (they are 475 // all permanent errors) 476 showCommunicationError("UIDL could not be read from server. Check servlets mappings. Error code: " 477 + statusCode); 478 endRequest(); 479 return; 480 } 481 482 final Date start = new Date(); 483 // for(;;);[realjson] 484 final String jsonText = response.getText().substring(9, 485 response.getText().length() - 1); 486 final ValueMap json; 487 try { 488 json = parseJSONResponse(jsonText); 489 } catch (final Exception e) { 490 endRequest(); 491 showCommunicationError(e.getMessage() 492 + " - Original JSON-text:" + jsonText); 493 return; 494 } 495 496 VConsole.log("JSON parsing took " 497 + (new Date().getTime() - start.getTime()) 498 + "ms"); 499 if (applicationRunning) { 500 handleReceivedJSONMessage(start, jsonText, json); 501 } else { 502 applicationRunning = true; 503 handleWhenCSSLoaded(jsonText, json); 504 ApplicationConfiguration.startNextApplication(); 505 } 506 } 507 508 }); 509 520 doAsyncUIDLRequest(uri, payload, requestCallback); 510 521 } catch (RequestException e) { 511 522 VConsole.error(e); … … 515 526 // Synchronized call, discarded response (leaving the page) 516 527 SynchronousXHR syncXHR = (SynchronousXHR) SynchronousXHR.create(); 517 syncXHR.synchronousPost(uri + "&" + PARAM_UNLOADBURST + "=1", rd); 528 syncXHR.synchronousPost(uri + "&" + PARAM_UNLOADBURST + "=1", 529 payload); 518 530 /* 519 531 * Although we are in theory leaving the page, the page may still … … 522 534 endRequest(); 523 535 } 536 537 } 538 539 /** 540 * Sends an asynchronous UIDL request to the server using the given URI. 541 * 542 * @param uri 543 * The URI to use for the request. May includes GET parameters 544 * @param payload 545 * The contents of the request to send 546 * @param requestCallback 547 * The handler for the response 548 * @throws RequestException 549 * if the request could not be sent 550 */ 551 protected void doAsyncUIDLRequest(String uri, String payload, 552 RequestCallback requestCallback) throws RequestException { 553 RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri); 554 // TODO enable timeout 555 // rb.setTimeoutMillis(timeoutMillis); 556 rb.setHeader("Content-Type", "text/plain;charset=utf-8"); 557 rb.setRequestData(payload); 558 rb.setCallback(requestCallback); 559 560 rb.send(); 524 561 } 525 562
Note: See TracChangeset
for help on using the changeset viewer.
