Repository: deltaspike Updated Branches: refs/heads/master 9487e30fa -> 4b564cc24
DELTASPIKE-729 Fixed double request/window generation because of initial redirect Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/4b564cc2 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/4b564cc2 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/4b564cc2 Branch: refs/heads/master Commit: 4b564cc24e709373d8c85d548a02a70a63e83860 Parents: 9487e30 Author: tandraschko <[email protected]> Authored: Sun Sep 21 12:31:34 2014 +0200 Committer: tandraschko <[email protected]> Committed: Sun Sep 21 12:31:34 2014 +0200 ---------------------------------------------------------------------- .../component/window/WindowIdHtmlRenderer.java | 11 ++++++ .../jsf/impl/util/ClientWindowHelper.java | 37 ++++++++++++++++++- .../resources/deltaspike/windowhandler.js | 39 ++++++++++++++------ 3 files changed, 74 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b564cc2/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/component/window/WindowIdHtmlRenderer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/component/window/WindowIdHtmlRenderer.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/component/window/WindowIdHtmlRenderer.java index c5297bb..cab4d26 100644 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/component/window/WindowIdHtmlRenderer.java +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/component/window/WindowIdHtmlRenderer.java @@ -26,9 +26,11 @@ import javax.faces.context.ResponseWriter; import javax.faces.render.FacesRenderer; import javax.faces.render.Renderer; import java.io.IOException; +import javax.servlet.http.Cookie; import org.apache.deltaspike.core.api.provider.BeanProvider; import org.apache.deltaspike.core.spi.scope.window.WindowContext; +import org.apache.deltaspike.jsf.impl.util.ClientWindowHelper; import org.apache.deltaspike.jsf.spi.scope.window.ClientWindowConfig; @FacesRenderer(componentFamily = WindowIdComponent.COMPONENT_FAMILY, rendererType = WindowIdComponent.COMPONENT_TYPE) @@ -61,6 +63,15 @@ public class WindowIdHtmlRenderer extends Renderer writer.writeAttribute("type", "text/javascript", null); writer.write("window.deltaspikeWindowId='" + windowId + "';"); writer.write("window.deltaspikeClientWindowRenderMode='" + mode + "';"); + + // see #729 + Object cookie = ClientWindowHelper.getRequestWindowIdCookie(context, windowId); + if (cookie != null && cookie instanceof Cookie) + { + Cookie servletCookie = (Cookie) cookie; + ClientWindowHelper.removeRequestWindowIdCookie(context, servletCookie); + writer.write("window.deltaspikeInitialRedirectWindowId='" + servletCookie.getValue() + "';"); + } writer.endElement("script"); } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b564cc2/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/util/ClientWindowHelper.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/util/ClientWindowHelper.java b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/util/ClientWindowHelper.java index f8faaab..1f13f9c 100644 --- a/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/util/ClientWindowHelper.java +++ b/deltaspike/modules/jsf/impl/src/main/java/org/apache/deltaspike/jsf/impl/util/ClientWindowHelper.java @@ -19,19 +19,23 @@ package org.apache.deltaspike.jsf.impl.util; import java.io.IOException; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.enterprise.inject.Typed; import javax.faces.FacesException; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; import org.apache.deltaspike.jsf.spi.scope.window.ClientWindow; @Typed() public abstract class ClientWindowHelper -{ +{ public static final String INITIAL_REDIRECT_WINDOW_ID = ClientWindowHelper.class.getName() + ".INITIAL_REDIRECT_WINDOW_ID"; + public static final String REQUEST_WINDOW_ID_COOKIE_PREFIX = "dsrwid-"; /** * Handles the initial redirect for the URL modus, if no windowId is available in the current request URL. @@ -57,6 +61,9 @@ public abstract class ClientWindowHelper url = JsfUtils.addRequestParameters(externalContext, url, true); + // see #729 + addRequestWindowIdCookie(facesContext, newWindowId); + try { externalContext.redirect(url); @@ -111,4 +118,32 @@ public abstract class ClientWindowHelper return url; } + + public static void addRequestWindowIdCookie(FacesContext context, String windowId) + { + Map<String, Object> properties = new HashMap(); + properties.put("path", "/"); + properties.put("maxAge", 30); + + context.getExternalContext().addResponseCookie( + REQUEST_WINDOW_ID_COOKIE_PREFIX + windowId, windowId, properties); + } + + public static Object getRequestWindowIdCookie(FacesContext context, String windowId) + { + Map<String, Object> cookieMap = context.getExternalContext().getRequestCookieMap(); + + if (cookieMap.containsKey(REQUEST_WINDOW_ID_COOKIE_PREFIX + windowId)) + { + return cookieMap.get(REQUEST_WINDOW_ID_COOKIE_PREFIX + windowId); + } + + return null; + } + + public static void removeRequestWindowIdCookie(FacesContext context, Cookie cookie) + { + cookie.setMaxAge(0); + ((HttpServletResponse) context.getExternalContext().getResponse()).addCookie(cookie); + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b564cc2/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 c40ba81..ccdc21a 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 @@ -168,12 +168,19 @@ function assertWindowId() { // url param available? if (dswid) { - // -- url param available, we must recreate a new windowId to be sure that it is new and valid -- + // initial redirect case + // the windowId is valid - we don't need to a second request + if (window.deltaspikeInitialRedirectWindowId && dswid == window.deltaspikeInitialRedirectWindowId) { + window.name = window.deltaspikeInitialRedirectWindowId; + } + else { + // -- url param available, we must recreate a new windowId to be sure that it is new and valid -- - // set tempWindowId to remember the current state - window.name = 'tempWindowId'; - // we remove the dswid if avilable and redirect to the same url again the create a new windowId - window.location = setUrlParam(window.location.href, 'dswid', null); + // set tempWindowId to remember the current state + window.name = 'tempWindowId'; + // we remove the dswid if avilable and redirect to the same url again the create a new windowId + window.location = setUrlParam(window.location.href, 'dswid', null); + } } else if (window.deltaspikeWindowId) { // -- no dswid in the url -> an initial request without initial redirect -- @@ -197,12 +204,20 @@ function assertWindowId() { } function eraseRequestCookie() { - var requestToken = getUrlParameter('dsrid'); // random request param - if (requestToken) { - var cookieName = 'dsWindowId-' + requestToken; - var date = new Date(); - date.setTime(date.getTime()-(10*24*60*60*1000)); // - 10 day - var expires = "; expires="+date.toGMTString(); + + var date = new Date(); + date.setTime(date.getTime()-(10*24*60*60*1000)); // - 10 day + var expires = ";max-age=0;expires="+date.toGMTString(); + + var dsrid = getUrlParameter('dsrid'); // random request param + if (dsrid) { + var cookieName = 'dsWindowId-' + dsrid; + document.cookie = cookieName+"="+expires+"; path=/"; + } + + var dswid = getUrlParameter('dswid'); + if (dswid) { + var cookieName = 'dsrwid-' + dswid; document.cookie = cookieName+"="+expires+"; path=/"; } } @@ -224,7 +239,7 @@ window.onload = function(evt) { try { (oldWindowOnLoad)? oldWindowOnLoad(evt): null; } finally { - eraseRequestCookie(); // manually erase the old dsrid cookie because Firefox doesn't do it properly + eraseRequestCookie(); // manually erase the old dsrid/dsrwid cookie because Firefox doesn't do it properly assertWindowId(); applyWindowId(); jsf.ajax.addOnEvent(jsfAjaxHandler);
