You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/11/26 14:32:46 UTC

svn commit: r1039346 - in /myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main: java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ resources/static/

Author: jakobk
Date: Fri Nov 26 13:32:45 2010
New Revision: 1039346

URL: http://svn.apache.org/viewvc?rev=1039346&view=rev
Log:
EXTCDI-79 include current path in cookie to make concurreny problems less likely, change windowId replace mechanism and remove windowhandlerfilter.html

Removed:
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandlerfilter.html
Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientInformation.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientInformation.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientInformation.java?rev=1039346&r1=1039345&r2=1039346&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientInformation.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientInformation.java Fri Nov 26 13:32:45 2010
@@ -41,6 +41,8 @@ public class ClientInformation implement
 {
     private static final long serialVersionUID = -3264016646002116064L;
 
+    private static final String DEFAULT_WINDOW_HANDLER_HTML_FILE = "static/windowhandler.html";
+
     private boolean javaScriptEnabled = true;
 
     protected String windowHandlerHtml;
@@ -67,7 +69,7 @@ public class ClientInformation implement
             return windowHandlerHtml;
         }
 
-        InputStream is = ClassUtils.getClassLoader(null).getResourceAsStream("static/windowhandler.html");
+        InputStream is = ClassUtils.getClassLoader(null).getResourceAsStream(DEFAULT_WINDOW_HANDLER_HTML_FILE);
         StringBuffer sb = new StringBuffer();
         try
         {

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java?rev=1039346&r1=1039345&r2=1039346&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java Fri Nov 26 13:32:45 2010
@@ -32,6 +32,8 @@ import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 
 import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
         .CREATE_NEW_WINDOW_CONTEXT_ID_VALUE;
@@ -48,7 +50,9 @@ import static org.apache.myfaces.extensi
 public class WindowHandlerPhaseListener implements javax.faces.event.PhaseListener
 {
 
-    private static final String WINDOW_ID_COOKIE_NAME = "codiWindowId";
+    private static final String WINDOW_ID_COOKIE_SUFFIX = "-codiWindowId";
+    private static final String UNINITIALIZED_WINDOW_ID_VALUE = "uninitializedWindowId";
+    private static final String WINDOW_ID_REPLACE_PATTERN = "$$windowIdValue$$";
 
     private Boolean isClientHandlerEnabled = null;
 
@@ -122,12 +126,14 @@ public class WindowHandlerPhaseListener 
 
             String windowHandlerHtml = clientInfo.getWindowHandlerHtml();
 
-            if (windowId != null)
+            if (windowId == null)
             {
-                // we send the _real_ windowId
-                windowHandlerHtml = windowHandlerHtml.replace("uninitializedWindowId", windowId);
+                windowId = UNINITIALIZED_WINDOW_ID_VALUE;
             }
 
+            // set the windowId value in the javascript code
+            windowHandlerHtml = windowHandlerHtml.replace(WINDOW_ID_REPLACE_PATTERN, windowId);
+
             OutputStream os = httpResponse.getOutputStream();
             try
             {
@@ -158,7 +164,15 @@ public class WindowHandlerPhaseListener 
 
     private String getWindowIdFromCookie(ExternalContext externalContext)
     {
-        Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(WINDOW_ID_COOKIE_NAME);
+        String cookieName = getEncodedPathName(externalContext) + WINDOW_ID_COOKIE_SUFFIX;
+        Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(cookieName);
+
+        if (cookie == null)
+        {
+            // if the current request went to a welcome page, we should only consider the contextPath
+            cookieName = getEncodedContextPath(externalContext) + WINDOW_ID_COOKIE_SUFFIX;
+            cookie = (Cookie) externalContext.getRequestCookieMap().get(cookieName);
+        }
 
         if (cookie != null)
         {
@@ -168,6 +182,69 @@ public class WindowHandlerPhaseListener 
         return null;
     }
 
+    private String getEncodedPathName(ExternalContext externalContext)
+    {
+        StringBuilder sb = new StringBuilder();
+
+        String contextPath = externalContext.getRequestContextPath();
+        if (contextPath != null)
+        {
+            sb.append(contextPath);
+        }
+
+        String servletPath = externalContext.getRequestServletPath();
+        if (servletPath != null)
+        {
+            sb.append(servletPath);
+        }
+
+        String pathInfo = externalContext.getRequestPathInfo();
+        if (pathInfo != null)
+        {
+            sb.append(pathInfo);
+        }
+
+        // remove all "/", because they can be different in JavaScript
+        String pathName = sb.toString().replace("/", "");
+
+        return encodeURIComponent(pathName, externalContext);
+    }
+
+    private String getEncodedContextPath(ExternalContext externalContext)
+    {
+        String contextPath = externalContext.getRequestContextPath();
+        if (contextPath != null)
+        {
+            // remove all "/", because they can be different in JavaScript
+            contextPath = contextPath.replace("/", "");
+
+            return encodeURIComponent(contextPath, externalContext);
+        }
+
+        return "";
+    }
+
+    /**
+     * JavaScript equivalent method.
+     *
+     * This is how the ExternalContext impl encodes URL parameter values.
+     *
+     * @param component
+     * @return
+     */
+    private String encodeURIComponent(String component, ExternalContext externalContext)
+    {
+        try
+        {
+            return URLEncoder.encode(component, externalContext.getResponseCharacterEncoding());
+        }
+        catch (UnsupportedEncodingException e)
+        {
+            throw new UnsupportedOperationException("Encoding type="
+                    + externalContext.getResponseCharacterEncoding() + " not supported", e);
+        }
+    }
+
     private boolean isWindowIdAlive(String windowId, EditableWindowContextManager windowContextManager)
     {
         for (EditableWindowContext wc : windowContextManager.getWindowContexts())

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html?rev=1039346&r1=1039345&r2=1039346&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html Fri Nov 26 13:32:45 2010
@@ -24,23 +24,33 @@
         <meta http-equiv="cache-control" content="no-cache">
         <meta http-equiv="pragma" content="no-cache">
         <script type="text/javascript" >
-            window.onload=function(){
-                var windowId = 'uninitializedWindowId';
-                if (windowId == ('uninitialized' + 'WindowId')) {
+            window.onload = function()
+            {
+                // this will be replaced in the phase listener
+                var windowId = '$$windowIdValue$$';
+
+                if (windowId == 'uninitializedWindowId')
+                {
                     windowId = window.name
                 }
-                if (!windowId || windowId.length < 3) {
-                    // this will be replaced in the servletfilter
-                    windowId ='automatedEntryPoint';
+
+                if (!windowId || windowId.length < 3)
+                {
+                    // request a new windowId
+                    windowId = 'automatedEntryPoint';
                 }
-                window.name=windowId;
+                
+                window.name = windowId;
 
                 // 2 seconds expiry time
                 var expdt = new Date();
                 expdt.setTime(expdt.getTime()+(2*1000));
                 var expires = "; expires="+expdt.toGMTString();
 
-                document.cookie = 'codiWindowId=' + windowId + expires;
+                // include pathName (without "/"s) in cookie name
+                var pathName = encodeURIComponent(window.location.pathname.replace(/\//g, ""));
+
+                document.cookie = pathName + '-codiWindowId=' + windowId + expires;
 
                 // resubmit the page but now with the cookie
                 window.location.reload();
@@ -49,4 +59,4 @@
     </head>
 <body>
 </body>
-</html>
\ No newline at end of file
+</html>