Repository: deltaspike Updated Branches: refs/heads/master a86c606a3 -> 726b4ed87
DELTASPIKE-1144 added more logging, refactored, added check if the window name is managed by DS Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/726b4ed8 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/726b4ed8 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/726b4ed8 Branch: refs/heads/master Commit: 726b4ed879ccfbe3b48ad12bb1d9abd1c7bb766f Parents: a86c606 Author: tandraschko <[email protected]> Authored: Wed May 18 16:03:02 2016 +0200 Committer: tandraschko <[email protected]> Committed: Wed May 18 16:03:02 2016 +0200 ---------------------------------------------------------------------- .../resources/deltaspike/windowhandler.js | 199 +++++++++++-------- .../main/resources/static/windowhandler.html | 68 +++++-- 2 files changed, 167 insertions(+), 100 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/726b4ed8/deltaspike/modules/jsf/impl/src/main/resources/META-INF/resources/deltaspike/windowhandler.js ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/resources/deltaspike/windowhandler.js b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/resources/deltaspike/windowhandler.js index 0833ffe..e6184af 100644 --- a/deltaspike/modules/jsf/impl/src/main/resources/META-INF/resources/deltaspike/windowhandler.js +++ b/deltaspike/modules/jsf/impl/src/main/resources/META-INF/resources/deltaspike/windowhandler.js @@ -20,6 +20,7 @@ window.dswh = window.dswh || { DEBUG_MODE : false, TEMP_WINDOW_NAME : 'tempWindowId', + MANAGED_WINDOW_NAME_PREFIX : 'ds-', windowId : null, clientWindowRenderMode : null, @@ -28,11 +29,9 @@ window.dswh = window.dswh || { init : function(windowId, clientWindowRenderMode, maxWindowIdLength, cfg) { - if (dswh.DEBUG_MODE === true) { - console.log('---- DeltaSpike WindowHandler ----'); - console.log('#init(\'' + windowId + '\', \'' + clientWindowRenderMode + '\',' + maxWindowIdLength + ',' + dswh.utils.stringify(cfg) + ')'); - console.log('window.name: ' + window.name); - } + dswh.utils.log('------- DeltaSpike windowhandler.js -------'); + dswh.utils.log('--- #init(\'' + windowId + '\', \'' + clientWindowRenderMode + '\',' + maxWindowIdLength + ',' + dswh.utils.stringify(cfg) + ')'); + dswh.utils.log('window.name: ' + window.name); this.windowId = windowId; this.clientWindowRenderMode = clientWindowRenderMode; @@ -45,26 +44,20 @@ window.dswh = window.dswh || { var targetStrategy = this.strategy[clientWindowRenderMode]; if (targetStrategy) { - if (dswh.DEBUG_MODE === true) { - console.log(clientWindowRenderMode + ' #validate'); - } + dswh.utils.log('--- #validate'); targetStrategy.validate(); // early init // this is required if e.g. the onload attr is defined on the body tag and our onload handler won't be called // ATTENTION: the ds:windowId component must be placed as last body tag - if (dswh.DEBUG_MODE === true) { - console.log(clientWindowRenderMode + ' #init(false)'); - } + dswh.utils.log('--- #init(false)'); targetStrategy.init(false); // JSF ajax callback jsf.ajax.addOnEvent(function(event) { if (event.status === "success") { - if (dswh.DEBUG_MODE === true) { - console.log(clientWindowRenderMode + ' #init(true)'); - } + dswh.utils.log('--- #init(true)'); targetStrategy.init(true); } }); @@ -72,9 +65,7 @@ window.dswh = window.dswh || { // PF ajax callback if (window.$ && window.PrimeFaces) { $(document).on('pfAjaxComplete', function () { - if (dswh.DEBUG_MODE === true) { - console.log(clientWindowRenderMode + ' #init(true)'); - } + dswh.utils.log('--- #init(true)'); targetStrategy.init(true); }); } @@ -86,9 +77,7 @@ window.dswh = window.dswh || { try { (oldWindowOnLoad) ? oldWindowOnLoad(evt) : null; } finally { - if (dswh.DEBUG_MODE === true) { - console.log(clientWindowRenderMode + ' #init(false)'); - } + dswh.utils.log('--- #init(false)'); targetStrategy.init(false); } }; @@ -112,10 +101,12 @@ window.dswh = window.dswh || { }, assertWindowId : function() { + dswh.utils.log('--- #assertWindowId'); + // ensure that windowIds get checked even if no windowhandler.html is used - if (!window.name || window.name.length < 1) { - window.name = dswh.TEMP_WINDOW_NAME; - window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', null); + if (!dswh.utils.isWindowNameDefined() || !dswh.utils.isManagedWindowName()) { + dswh.utils.log('window name not defined or unmanaged - request new windowId'); + dswh.utils.requestNewWindowId(); } }, @@ -124,6 +115,10 @@ window.dswh = window.dswh || { var tokenizedRedirectEnabled = dswh.cfg.tokenizedRedirect; var storeWindowTreeEnabled = dswh.utils.isHtml5() && dswh.cfg.storeWindowTreeOnLinkClick; + dswh.utils.log('--- #overwriteLinkOnClickEvents'); + dswh.utils.log('tokenizedRedirect: ' + dswh.cfg.tokenizedRedirect); + dswh.utils.log('storeWindowTreeOnLinkClick: ' + dswh.cfg.storeWindowTreeOnLinkClick); + if (tokenizedRedirectEnabled || storeWindowTreeEnabled) { var links = document.getElementsByTagName("a"); for (var i = 0; i < links.length; i++) { @@ -177,6 +172,9 @@ window.dswh = window.dswh || { var storeWindowTreeEnabled = dswh.utils.isHtml5() && dswh.cfg.storeWindowTreeOnButtonClick; + dswh.utils.log('--- #overwriteButtonOnClickEvents'); + dswh.utils.log('storeWindowTreeOnButtonClick: ' + dswh.cfg.storeWindowTreeOnButtonClick); + if (storeWindowTreeEnabled) { var inputs = document.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { @@ -237,6 +235,9 @@ window.dswh = window.dswh || { }, tokenizedRedirect : function(link) { + + dswh.utils.log('--- #tokenizedRedirect'); + var requestToken = dswh.utils.generateRequestToken(); dswh.utils.storeCookie('dsrwid-' + requestToken, dswh.windowId, 3); window.location = dswh.utils.setUrlParam(link.href, 'dsrid', requestToken); @@ -246,6 +247,9 @@ window.dswh = window.dswh || { * store the current body in the html5 localstorage */ storeWindowTree : function() { + + dswh.utils.log('--- #storeWindowTree'); + // first we store all CSS we also need on the intermediate page var headNodes = document.getElementsByTagName("head")[0].childNodes; var oldSS = new Array(); @@ -285,6 +289,8 @@ window.dswh = window.dswh || { }, cleanupCookies : function() { + dswh.utils.log('--- #cleanupCookies'); + var dsrid = dswh.utils.getUrlParameter(window.location.href, 'dsrid'); if (dsrid) { dswh.utils.expireCookie('dsrwid-' + dsrid); @@ -306,90 +312,86 @@ window.dswh = window.dswh || { assertWindowId : function() { var dswid = dswh.utils.getUrlParameter(window.location.href, 'dswid'); - if (dswh.DEBUG_MODE === true) { - console.log('dswid: ' + dswid); - } + dswh.utils.log('--- #assertWindowId'); + dswh.utils.log('dswid: ' + dswid); - // window.name is null which means that "open in new tab/window" was used - if (!window.name || window.name.length < 1) { + // window name is defined -> existing tab + if (dswh.utils.isWindowNameDefined()) { - // url param available? - if (dswid) { - // initial redirect case - // the windowId is valid - we don't need to a second request - if (dswh.cfg.initialRedirectWindowId && dswid === dswh.cfg.initialRedirectWindowId) { - window.name = dswh.cfg.initialRedirectWindowId; + // is the current window name a already managed by DS? + if (dswh.utils.isManagedWindowName()) { - if (dswh.DEBUG_MODE === true) { - console.log('overtake initialRedirectWindowId'); - } - } - else { - // -- url param available, we must recreate a new windowId to be sure that it is new and valid -- + var windowId = dswh.utils.getWindowIdFromWindowName(); + + // we triggered the windowId recreation last request + if (windowId === dswh.TEMP_WINDOW_NAME) { + // enabled initial redirect + // -> use the new windowId from the url + if (dswid) { + dswh.utils.log('assign window name from request parameter'); - if (dswh.DEBUG_MODE === true) { - console.log('request new windowId'); + dswh.utils.setWindowIdAsWindowName(dswid); } + // disabled initial redirect + // -> use the new windowId from the rendered config as no url param is available + else { + dswh.utils.log('assign window name from server windowId'); - // set temp window name to remember the current state - window.name = dswh.TEMP_WINDOW_NAME; - // we remove the dswid if available and redirect to the same url again to create a new windowId - window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', null); + dswh.utils.setWindowIdAsWindowName(dswh.windowId); + } } - } - else if (dswh.windowId) { - // -- no dswid in the url -> an initial request without initial redirect -- + // security check like on the server side + else if (windowId.length > dswh.maxWindowIdLength) { + dswh.utils.log('window id from window name exeeds maxWindowIdLength - request new windowId'); - // this means that the initial redirect is disabled and we can just use the windowId as window.name - window.name = dswh.windowId; + dswh.utils.requestNewWindowId(); + } + // window name doesn't match requested windowId + // -> redirect to the same view with current windowId from the window name + else if (windowId !== dswid) { + dswh.utils.log('reload url with window name'); - if (dswh.DEBUG_MODE === true) { - console.log('assign window.name from windowId'); + window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', windowId); } } + else { + dswh.utils.log('window name is unmanaged - request new windowId'); + + dswh.utils.requestNewWindowId(); + } } + // window name is undefined -> "open in new tab/window" was used else { - if (window.name === dswh.TEMP_WINDOW_NAME) { - if (dswid) { - // we triggered the windowId recreation last request - use it now! - window.name = dswid; + // url param available? + if (dswid) { + // initial redirect + // -> the windowId is valid - we don't need to a second request + if (dswh.cfg.initialRedirectWindowId && dswid === dswh.cfg.initialRedirectWindowId) { + dswh.utils.log('assign window name from initialRedirectWindowId'); - if (dswh.DEBUG_MODE === true) { - console.log('assign window.name from request parameter'); - } + dswh.utils.setWindowIdAsWindowName(dswh.cfg.initialRedirectWindowId); } + // != initial redirect + // -> request a new windowId to avoid multiple tabs with the same windowId else { - // it could be from dswh.windowId in case of open in new tab and initial redirect is disabled - window.name = dswh.windowId; + dswh.utils.log('request new windowId'); - if (dswh.DEBUG_MODE === true) { - console.log('assign window.name from windowId'); - } + dswh.utils.requestNewWindowId(); } } - else if (window.name.length > dswh.maxWindowIdLength) { - if (dswh.DEBUG_MODE === true) { - console.log('current window.name exeeds maxWindowIdLength - request new windowId'); - } - - // set temp window name to remember the current state - window.name = dswh.TEMP_WINDOW_NAME; - // we remove the dswid if available and redirect to the same url again to create a new windowId - window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', null); - } - else if (window.name !== dswid) { - if (dswh.DEBUG_MODE === true) { - console.log('reload view with window.name'); - } + // as no url parameter is available, the request is a new tab with disabled initial redirect + // -> just use the windowId from the renderer + else if (dswh.windowId) { + dswh.utils.log('assign window name from server windowId'); - // window.name doesn't match requested windowId - // -> redirect to the same view with current window.name / windowId - window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', window.name); + dswh.utils.setWindowIdAsWindowName(dswh.windowId); } } }, cleanupCookies : function() { + dswh.utils.log('--- #cleanupCookies'); + var dswid = dswh.utils.getUrlParameter(window.location.href, 'dswid'); if (dswid) { dswh.utils.expireCookie('dsrwid-' + dswid); @@ -400,6 +402,33 @@ window.dswh = window.dswh || { utils : { + isWindowNameDefined : function() { + return window.name && window.name.length > 0; + }, + + isManagedWindowName : function() { + if (!window.name) { + return false; + } + + return window.name.indexOf(dswh.MANAGED_WINDOW_NAME_PREFIX) === 0; + }, + + getWindowIdFromWindowName : function() { + return window.name.substring(dswh.MANAGED_WINDOW_NAME_PREFIX.length); + }, + + setWindowIdAsWindowName : function(windowId) { + window.name = dswh.MANAGED_WINDOW_NAME_PREFIX + windowId; + }, + + requestNewWindowId : function() { + // set temp window name to remember the current state + dswh.utils.setWindowIdAsWindowName(dswh.TEMP_WINDOW_NAME); + // we remove the dswid if available and redirect to the same url again to create a new windowId + window.location = dswh.utils.setUrlParam(window.location.href, 'dswid', null); + }, + isHtml5 : function() { try { return !!localStorage.getItem; @@ -544,6 +573,12 @@ window.dswh = window.dswh || { var expires = "; expires=" + expiresDate.toGMTString(); document.cookie = name + '=' + value + expires + "; path=/"; + }, + + log : function(message) { + if (dswh.DEBUG_MODE === true) { + console.log(message); + } } } }; http://git-wip-us.apache.org/repos/asf/deltaspike/blob/726b4ed8/deltaspike/modules/jsf/impl/src/main/resources/static/windowhandler.html ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/resources/static/windowhandler.html b/deltaspike/modules/jsf/impl/src/main/resources/static/windowhandler.html index b5bdf21..f8dd6b8 100644 --- a/deltaspike/modules/jsf/impl/src/main/resources/static/windowhandler.html +++ b/deltaspike/modules/jsf/impl/src/main/resources/static/windowhandler.html @@ -115,30 +115,62 @@ replaceContent(); window.onload = function() { - // uncomment the following line to debug the intermediate page - //if (!confirm('reload?')) { return true; } + if (dswh.DEBUG_MODE === true){ + if (!confirm('reload?')) { + return true; + } + } loadCss(true); - var windowId = window.name; - var urlId = windowId; - if (!windowId || windowId.length < 1) { - var newWindowId = '$$windowIdValue$$'; - if (newWindowId !== 'uninitializedWindowId') { - window.name = newWindowId; // set the window.name with our windowId - windowId = newWindowId; - urlId = windowId; + var windowId = dswh.utils.getWindowIdFromWindowName(); + var urlWindowId = windowId; + + function requestNewWindowId() { + window.name = ''; + windowId = 'automatedEntryPoint'; + urlWindowId = null; + } + + dswh.utils.log('------- DeltaSpike windowhandler.html -------'); + dswh.utils.log('windowId: ' + windowId); + dswh.utils.log('window.name: ' + window.name); + + if (dswh.utils.isWindowNameDefined()) { + if (!dswh.utils.isManagedWindowName()) { + + dswh.utils.log('window name unmanaged - request new windowId'); + + requestNewWindowId(); } else { - windowId = 'automatedEntryPoint'; - urlId = null; + // occurs with the "duplicate tab" feature of chrome + if (windowId === dswh.TEMP_WINDOW_NAME) { + + dswh.utils.log('window name is temporary - request new windowId'); + + requestNewWindowId(); + } } } - // occurs with the "duplicate tab" feature of chrome - else if (windowId === dswh.TEMP_WINDOW_NAME) { - window.name = ''; - windowId = 'automatedEntryPoint'; - urlId = null; + // window name is undefined, which means that "open in new tab/window" was used + else { + var newWindowId = '$$windowIdValue$$'; + + dswh.utils.log('newWindowId ' + newWindowId); + + if (newWindowId === 'uninitializedWindowId') { + dswh.utils.log('windowId not initialized - request new windowId'); + + requestNewWindowId(); + } + else { + dswh.utils.log('assign window name from newWindowId'); + + dswh.utils.setWindowIdAsWindowName(newWindowId); // set the window.name with our windowId + windowId = newWindowId; + urlWindowId = newWindowId; + } } var requestToken = dswh.utils.generateRequestToken(); @@ -146,7 +178,7 @@ var redirectUrl = '$$requestUrl$$'; redirectUrl = dswh.utils.setUrlParam(redirectUrl, "dsrid", requestToken); // we still add the windowId page param to support lazy windowId dropping for some clients - redirectUrl = dswh.utils.setUrlParam(redirectUrl, "dswid", urlId); + redirectUrl = dswh.utils.setUrlParam(redirectUrl, "dswid", urlWindowId); dswh.utils.storeCookie('dsrwid-' + requestToken, windowId, 3);
