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

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

Author: struberg
Date: Thu Nov 25 13:55:27 2010
New Revision: 1039045

URL: http://svn.apache.org/viewvc?rev=1039045&view=rev
Log:
EXTCDI-79 filter refined

Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/conversation/ClientAwareWindowHandler.java
    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/ClientsideWindowHandlerFilter.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandler.html
    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/scope/conversation/ClientAwareWindowHandler.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/scope/conversation/ClientAwareWindowHandler.java?rev=1039045&r1=1039044&r2=1039045&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/conversation/ClientAwareWindowHandler.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/scope/conversation/ClientAwareWindowHandler.java Thu Nov 25 13:55:27 2010
@@ -19,7 +19,6 @@
 package org.apache.myfaces.extensions.cdi.jsf2.impl.scope.conversation;
 
 import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.DefaultWindowHandler;
-import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.BookmarkAwareWindowHandler;
 import org.apache.myfaces.extensions.cdi.jsf2.impl.windowhandler.Jsf2WindowHandlerServlet;
 import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContextConfig;
 
@@ -30,17 +29,13 @@ import javax.faces.context.FacesContext;
 import javax.faces.context.PartialViewContext;
 import javax.inject.Inject;
 import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.ArrayList;
 
 /**
  * WindowHandler with JSF2 features
  */
 @Alternative
 @ApplicationScoped
-public class ClientAwareWindowHandler extends DefaultWindowHandler implements BookmarkAwareWindowHandler
+public class ClientAwareWindowHandler extends DefaultWindowHandler
 {
     private static final long serialVersionUID = 5293942986187078113L;
 
@@ -76,15 +71,6 @@ public class ClientAwareWindowHandler ex
         super.sendRedirect(externalContext, url, addRequestParameter);
     }
 
-    public String encodeBookmarkableURL(ExternalContext externalContext, String url, Map<String, List<String>> params)
-    {
-        Map<String, List<String>> newparms = new HashMap<String, List<String>>();
-        List<String> urlParam= new ArrayList<String>();
-        urlParam.add(externalContext.encodeBookmarkableURL(url, params));
-        newparms.put(Jsf2WindowHandlerServlet.URL_PARAM, urlParam);
-        return externalContext.encodeBookmarkableURL(getWindowHandlerPath(externalContext), newparms);
-    }
-
     private String getWindowHandlerPath(ExternalContext externalContext)
     {
         String contextPath = externalContext.getRequestContextPath();
@@ -98,4 +84,5 @@ public class ClientAwareWindowHandler ex
             return contextPath + Jsf2WindowHandlerServlet.WINDOWHANDLER_URL;
         }
     }
+
 }

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=1039045&r1=1039044&r2=1039045&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 Thu Nov 25 13:55:27 2010
@@ -18,12 +18,23 @@
  */
 package org.apache.myfaces.extensions.cdi.jsf2.impl.windowhandler;
 
+import org.apache.myfaces.extensions.cdi.core.api.projectstage.ProjectStage;
+import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+
 import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.Serializable;
 
 /**
  * Contains information about whether the user has
  * JavaScript enabled on his client, etc.
+ * It also contains the windowhandler html which gets sent to
+ * the browser to detect the current windowId.
+ *
+ * This allows the 'customisation' of this html file to e.g.
+ * adopt the background colour to avoid screen flickering.
  */
 @SessionScoped
 public class ClientInformation implements Serializable
@@ -32,6 +43,10 @@ public class ClientInformation implement
 
     private boolean javaScriptEnabled = true;
 
+    protected String windowHandlerHtml;
+
+    private @Inject ProjectStage projectStage;
+
 
     public boolean isJavaScriptEnabled()
     {
@@ -42,4 +57,35 @@ public class ClientInformation implement
     {
         this.javaScriptEnabled = javaScriptEnabled;
     }
+
+    public String getWindowHandlerHtml()
+            throws IOException
+    {
+        if (projectStage != ProjectStage.Development && windowHandlerHtml != null)
+        {
+            // use cached windowHandlerHtml except in Development
+            return windowHandlerHtml;
+        }
+
+        InputStream is = ClassUtils.getClassLoader(null).getResourceAsStream("static/windowhandlerfilter.html");
+        StringBuffer sb = new StringBuffer();
+        try
+        {
+            byte[] buf = new byte[32 * 1024];
+            int bytesRead;
+            while ((bytesRead = is.read(buf)) != -1)
+            {
+                String sbuf = new String(buf);
+                sb.append(sbuf);
+            }
+        }
+        finally
+        {
+            is.close();
+        }
+
+        windowHandlerHtml = sb.toString();
+        return windowHandlerHtml;
+    }
+
 }

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientsideWindowHandlerFilter.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/ClientsideWindowHandlerFilter.java?rev=1039045&r1=1039044&r2=1039045&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientsideWindowHandlerFilter.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/ClientsideWindowHandlerFilter.java Thu Nov 25 13:55:27 2010
@@ -18,7 +18,9 @@
  */
 package org.apache.myfaces.extensions.cdi.jsf2.impl.windowhandler;
 
-import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContext;
+import org.apache.myfaces.extensions.cdi.core.impl.util.CodiUtils;
+import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableWindowContextManager;
 
 import javax.faces.application.ResourceHandler;
 import javax.servlet.Filter;
@@ -27,17 +29,17 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.UUID;
 
 /**
  * TODO
  *
  * @author Jakob Korherr
+ * @author Mark Struberg
  */
 public class ClientsideWindowHandlerFilter implements Filter
 {
@@ -57,26 +59,57 @@ public class ClientsideWindowHandlerFilt
         if ("GET".equals(httpRequest.getMethod()) && !isResourceRequest(httpRequest))
         {
             // only for GET requests
-            String windowIdParam = servletRequest.getParameter("windowId");
 
-            if (windowIdParam == null)
+            ClientInformation clientInfo =
+                    CodiUtils.getOrCreateScopedInstanceOfBeanByClass(ClientInformation.class, false);
+
+            EditableWindowContextManager windowContextManager =
+                    CodiUtils.getOrCreateScopedInstanceOfBeanByClass(EditableWindowContextManager.class, false);
+
+            String windowId = null;
+            if (!clientInfo.isJavaScriptEnabled())
             {
-                // GET request without windowId - send windowhandler.html
-                sendWindowHandler(httpRequest, httpResponse);
+                // no handling possible
+                filterChain.doFilter(servletRequest, servletResponse);
+                return;
             }
-            else
+
+            Cookie[] cookies = ((HttpServletRequest) servletRequest).getCookies();
+
+            if (cookies != null)
             {
-                // GET request with windowId (from AJAX)
+                for (Cookie cookie : cookies)
+                {
+                    if ("codiWindowId".equals(cookie.getName()))
+                    {
+                        windowId = cookie.getValue();
+                        cookie.setMaxAge(0);
+                        break;
+                    }
+                }
+            }
 
-                // generate new windowId and set it as response header
-                String windowId; //X TODO get new windowId from CODI algorithm
-                //X TODO temp - random windowId
-                windowId = UUID.randomUUID().toString().replace("-", "");
+            if (windowId == null)
+            {
+                // GET request without windowId - send windowhandlerfilter.html
+                sendWindowHandler(httpRequest, httpResponse, clientInfo, null);
+            }
+            else if ("automatedEntryPoint".equals(windowId))
+            {
+                WindowContext windowContext = windowContextManager.getCurrentWindowContext();
+                windowId = windowContext.getId();
 
-                // set response header for JavaScript
-                httpResponse.setHeader("myfaces-codi-windowId", windowId);
+                // GET request with NEW windowId - send windowhandlerfilter.html
+                sendWindowHandler(httpRequest, httpResponse, clientInfo, windowId);
+            }
+            else
+            {
+                // GET request with windowId from Cookie
 
                 // pass through with WindowIdServletRequestWrapper
+                //X TODO don't think this is the best way to do it!
+                //X TODO we should tell the requests WindowManager the id directly!
+                //X otherwise we might get the windowId = xxxx in a link somewhere...
                 WindowIdServletRequestWrapper requestWrapper = new WindowIdServletRequestWrapper(httpRequest, windowId);
                 filterChain.doFilter(requestWrapper, servletResponse);
             }
@@ -101,26 +134,28 @@ public class ClientsideWindowHandlerFilt
         return requestURL.contains(ResourceHandler.RESOURCE_IDENTIFIER);
     }
 
-    private void sendWindowHandler(HttpServletRequest req, HttpServletResponse resp)
+    private void sendWindowHandler(HttpServletRequest req, HttpServletResponse resp,
+                                   ClientInformation clientInfo, String windowId)
             throws ServletException, IOException
     {
         resp.setStatus(HttpServletResponse.SC_OK);
         resp.setContentType("text/html");
 
-        InputStream is = ClassUtils.getClassLoader(null).getResourceAsStream("static/windowhandlerfilter.html");
+        String windowHandlerHtml = clientInfo.getWindowHandlerHtml();
+
+        if (windowId != null)
+        {
+            // we send the _real_ windowId
+            windowHandlerHtml = windowHandlerHtml.replace("automatedEntryPoint", windowId);
+        }
+
         OutputStream os = resp.getOutputStream();
         try
         {
-            byte[] buf = new byte[16 * 4096];
-            int bytesRead;
-            while ((bytesRead = is.read(buf)) != -1)
-            {
-                os.write(buf, 0, bytesRead);
-            }
+                os.write(windowHandlerHtml.getBytes());
         }
         finally
         {
-            is.close();
             os.close();
         }
     }

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=1039045&r1=1039044&r2=1039045&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 Thu Nov 25 13:55:27 2010
@@ -4,6 +4,9 @@
 <html>
     <head>
         <meta name="author" content="Mark Struberg">
+        <meta http-equiv="cache-control" content="no-cache">
+        <meta http-equiv="pragma" content="no-cache">
+        
 <!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandlerfilter.html
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandlerfilter.html?rev=1039045&r1=1039044&r2=1039045&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandlerfilter.html (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/resources/static/windowhandlerfilter.html Thu Nov 25 13:55:27 2010
@@ -22,61 +22,26 @@
 <html>
     <head>
         <script type="text/javascript" >
-            var http_request;
-
-            function createHttpRequest(){
-                var http_request2;
-                if (window.XMLHttpRequest) { // IE7, Mozilla, Safari,...
-                    http_request2 = new XMLHttpRequest();
-                    if (http_request2.overrideMimeType) {
-                        http_request2.overrideMimeType('text/xml');
-                    }
-                } else if (window.ActiveXObject) { // IE
-                    try {
-                        http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
-                    } catch (e) {
-                        try {
-                            http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
-                        } catch (e) {}
-                    }
+            window.onload=function(){
+                var origUrl = window.location.href;
+                var windowId = window.name
+                if (!windowId || windowId.length < 3) {
+                    // this will be replaced in the servletfilter
+                    windowId ='automatedEntryPoint';
                 }
 
-                if (!http_request2) {
-                    alert('Not possible to get HttpRequest object!');
-                    return false;
-                }
-                return http_request2;
-            }
+                // 1 minute expiry time
+                var expdt = new Date();
+                expdt.setTime(expdt.getTime()+(1*60*1000));
+                var expires = "; expires="+expdt.toGMTString();
 
-            function doRequest(url){
-                http_request.open('GET', url, true);
-                http_request.onreadystatechange = replaceContent;
-                http_request.send(null);
-            }
+                document.cookie = 'codiWindowId=' + windowId + expires;
 
-            function replaceContent(){
-                if (http_request.readyState == 4) {
-                    if (http_request.status == 200){
-                        if(http_request.responseText!=""){
-                            document.innerHTML=http_request.responseText;
-                            
-                            window.name= http_request.getResponseHeader("myfaces-codi-windowId");
-                        }
-                    }
-                }
-            }
-            var origUrl = window.location.href;
-            if (!window.name || window.name.length < 1) {
-                window.name = 'automatedEntryPoint';
+                // resubmit the page but now with the cookie
+                window.location.replace(url + ' '); // add space to force reload 
             }
-
-            var amp = origUrl.contains('?') ? "&" : "?";
-            var newUrl = origUrl + amp + "windowId=" + window.name;
-            http_request = createHttpRequest();
-            doRequest(newUrl);
         </script>
     </head>
 <body>
-
 </body>
 </html>
\ No newline at end of file