You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2016/04/19 16:11:37 UTC

wicket git commit: WICKET-6075 Error page redirection for Autocomplete field response

Repository: wicket
Updated Branches:
  refs/heads/wicket-7.x 97c45c278 -> 0e94268a2


WICKET-6075 Error page redirection for Autocomplete field response

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/0e94268a
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/0e94268a
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/0e94268a

Branch: refs/heads/wicket-7.x
Commit: 0e94268a21c9d93e8adcc14df51a601fe0c9242b
Parents: 97c45c2
Author: Andrea Del Bene <ad...@apache.org>
Authored: Tue Apr 19 16:03:45 2016 +0200
Committer: Andrea Del Bene <ad...@apache.org>
Committed: Tue Apr 19 16:11:03 2016 +0200

----------------------------------------------------------------------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js    | 127 +++++++++++--------
 wicket-core/src/test/js/ajax.js                 |  30 +++++
 2 files changed, 102 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/0e94268a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index 55e6e61..18be42e 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -704,6 +704,7 @@
 					if (attrs.wr) {
 						self.processAjaxResponse(data, textStatus, jqXHR, context);
 					} else {
+						self.handleRedirection(jqXHR, context);
 						self._executeHandlers(attrs.sh, attrs, jqXHR, data, textStatus);
 						we.publish(topic.AJAX_CALL_SUCCESS, attrs, jqXHR, data, textStatus);
 					}
@@ -754,6 +755,68 @@
 		},
 
 		/**
+		 * Method checks for 'Ajax-Location' header, if available, handles redirection to that URL.
+		 * 
+		 * @param jqXHR {Object} - the jQuery wrapper around XMLHttpRequest
+		 * @param context {Object} - the request context with the Ajax request attributes and the FunctionExecuter's steps
+		 * 
+		 * @return boolean - returns 'true' if 'Ajax-Location' header is found redirection is handled, 'false' otherwise.
+		 */
+		handleRedirection : function (jqXHR, context) {
+
+			// first try to get the redirect header
+			var redirectUrl;
+			try {
+				redirectUrl = jqXHR.getResponseHeader('Ajax-Location');
+			} catch (ignore) { // might happen in older mozilla
+			}
+
+			// the redirect header was not set, just return
+			if (typeof(redirectUrl) === "undefined" || redirectUrl === null || redirectUrl === "") {
+				return false;
+			}
+
+			// In case the page isn't really redirected. For example say the redirect is to an octet-stream.
+			// A file download popup will appear but the page in the browser won't change.
+			this.success(context);
+
+			var rhttp  = /^http:\/\//,  // checks whether the string starts with http://
+			    rhttps = /^https:\/\//; // checks whether the string starts with https://
+
+			// support/check for non-relative redirectUrl like as provided and needed in a portlet context
+			if (redirectUrl.charAt(0) === '/' || rhttp.test(redirectUrl) || rhttps.test(redirectUrl)) {
+				context.isRedirecting = true;
+				Wicket.Ajax.redirect(redirectUrl);
+			}
+			else {
+				var urlDepth = 0;
+				while (redirectUrl.substring(0, 3) === "../") {
+					urlDepth++;
+					redirectUrl = redirectUrl.substring(3);
+				}
+				// Make this a string.
+				var calculatedRedirect = window.location.pathname;
+				while (urlDepth > -1) {
+					urlDepth--;
+					var i = calculatedRedirect.lastIndexOf("/");
+					if (i > -1) {
+						calculatedRedirect = calculatedRedirect.substring(0, i);
+					}
+				}
+				calculatedRedirect += "/" + redirectUrl;
+
+				if (Wicket.Browser.isGecko()) {
+					// firefox 3 has problem with window.location setting relative url
+					calculatedRedirect = window.location.protocol + "//" + window.location.host + calculatedRedirect;
+				}
+
+				context.isRedirecting = true;
+				Wicket.Ajax.redirect(calculatedRedirect);
+			}
+			return context.isRedirecting;
+		},
+
+		/**
 		 * Method that processes the <ajax-response> in the context of an XMLHttpRequest.
 		 *
 		 * @param data {XmlDocument} - the <ajax-response> XML document
@@ -765,65 +828,19 @@
 
 			if (jqXHR.readyState === 4) {
 
-				// first try to get the redirect header
-				var redirectUrl;
-				try {
-					redirectUrl = jqXHR.getResponseHeader('Ajax-Location');
-				} catch (ignore) { // might happen in older mozilla
+				if (this.handleRedirection(jqXHR, context)) {
+					return; // 'Ajax-Location' header is set - redirected to it
 				}
 
-				// the redirect header was set, go to new url
-				if (typeof(redirectUrl) !== "undefined" && redirectUrl !== null && redirectUrl !== "") {
-
-					// In case the page isn't really redirected. For example say the redirect is to an octet-stream.
-					// A file download popup will appear but the page in the browser won't change.
-					this.success(context);
-
-					var rhttp  = /^http:\/\//,  // checks whether the string starts with http://
-					    rhttps = /^https:\/\//; // checks whether the string starts with https://
-
-					// support/check for non-relative redirectUrl like as provided and needed in a portlet context
-					if (redirectUrl.charAt(0) === '/' || rhttp.test(redirectUrl) || rhttps.test(redirectUrl)) {
-						context.isRedirecting = true;
-						Wicket.Ajax.redirect(redirectUrl);
-					}
-					else {
-						var urlDepth = 0;
-						while (redirectUrl.substring(0, 3) === "../") {
-							urlDepth++;
-							redirectUrl = redirectUrl.substring(3);
-						}
-						// Make this a string.
-						var calculatedRedirect = window.location.pathname;
-						while (urlDepth > -1) {
-							urlDepth--;
-							var i = calculatedRedirect.lastIndexOf("/");
-							if (i > -1) {
-								calculatedRedirect = calculatedRedirect.substring(0, i);
-							}
-						}
-						calculatedRedirect += "/" + redirectUrl;
-
-						if (Wicket.Browser.isGecko()) {
-							// firefox 3 has problem with window.location setting relative url
-							calculatedRedirect = window.location.protocol + "//" + window.location.host + calculatedRedirect;
-						}
-
-						context.isRedirecting = true;
-						Wicket.Ajax.redirect(calculatedRedirect);
-					}
+				// no redirect, just regular response
+				if (Wicket.Log.enabled()) {
+					var responseAsText = jqXHR.responseText;
+					Wicket.Log.info("Received ajax response (" + responseAsText.length + " characters)");
+					Wicket.Log.info("\n" + responseAsText);
 				}
-				else {
-					// no redirect, just regular response
-					if (Wicket.Log.enabled()) {
-						var responseAsText = jqXHR.responseText;
-						Wicket.Log.info("Received ajax response (" + responseAsText.length + " characters)");
-						Wicket.Log.info("\n" + responseAsText);
-					}
 
-					// invoke the loaded callback with an xml document
-					return this.loadedCallback(data, context);
-				}
+				// invoke the loaded callback with an xml document
+				return this.loadedCallback(data, context);
 			}
 		},
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/0e94268a/wicket-core/src/test/js/ajax.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js
index bdd772f..21c37a8 100644
--- a/wicket-core/src/test/js/ajax.js
+++ b/wicket-core/src/test/js/ajax.js
@@ -1359,6 +1359,36 @@ jQuery(document).ready(function() {
 			target.off("event1");
 		});
 
+		asyncTest('Do not hide the indicator if redirecting.', function () {
+
+			expect(2);
+
+			var oldRedirect = Wicket.Ajax.redirect;
+			Wicket.Ajax.redirect = function() {};
+
+			var attrs = {
+				u: 'data/ajax/redirectAjaxResponse.xml',
+				e: 'event1',
+				i: 'ajaxIndicator',
+				wr: false,
+				sh: [function(attrs, jqXHR, data, textStatus) {
+					var indicatorEl = Wicket.$(attrs.i);
+					equal("1", indicatorEl.getAttribute("showIncrementallyCount"));
+				}],
+				coh: [function(attrs, jqXHR, textStatus) {
+					var indicatorEl = Wicket.$(attrs.i);
+					equal("1", indicatorEl.getAttribute("showIncrementallyCount"));
+					Wicket.Ajax.redirect = oldRedirect;
+					start();
+				}]
+			};
+
+			Wicket.Ajax.ajax(attrs);
+			var target = jQuery(window);
+			target.triggerHandler("event1");
+			target.off("event1");
+		});
+
 		asyncTest('Do hide the indicator if not redirecting.', function () {
 
 			expect(2);