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 12:28:46 UTC

svn commit: r1039316 - in /myfaces/extensions/cdi/trunk: core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/ jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation...

Author: jakobk
Date: Fri Nov 26 11:28:45 2010
New Revision: 1039316

URL: http://svn.apache.org/viewvc?rev=1039316&view=rev
Log:
EXTCDI-79 cleanup and refactoring

Removed:
    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/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowIdServletRequestWrapper.java
Modified:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/WindowContextManager.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/WindowContextManagerObserver.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/phase/CodiLifecycleWrapper.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/windowhandler/WindowHandlerPhaseListener.java

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/WindowContextManager.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/WindowContextManager.java?rev=1039316&r1=1039315&r2=1039316&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/WindowContextManager.java (original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/scope/conversation/spi/WindowContextManager.java Fri Nov 26 11:28:45 2010
@@ -29,6 +29,8 @@ public interface WindowContextManager ex
 {
     String WINDOW_CONTEXT_ID_PARAMETER_KEY = "windowId";
 
+    String CREATE_NEW_WINDOW_CONTEXT_ID_VALUE = "automatedEntryPoint";
+
     WindowContext getCurrentWindowContext();
 
     WindowContext getWindowContext(String windowContextId);

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/WindowContextManagerObserver.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/WindowContextManagerObserver.java?rev=1039316&r1=1039315&r2=1039316&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/WindowContextManagerObserver.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/WindowContextManagerObserver.java Fri Nov 26 11:28:45 2010
@@ -18,26 +18,32 @@
  */
 package org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation;
 
+import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContextConfig;
 import org.apache.myfaces.extensions.cdi.jsf.api.listener.phase.AfterPhase;
 import org.apache.myfaces.extensions.cdi.jsf.api.listener.phase.JsfPhaseId;
 import org.apache.myfaces.extensions.cdi.jsf.api.request.RequestTypeResolver;
-import org.apache.myfaces.extensions.cdi.jsf.impl.util.RequestCache;
-import org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils;
-import static org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils.*;
-import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.WindowHandler;
-import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.JsfModuleConfig;
+import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableConversation;
 import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableWindowContext;
 import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableWindowContextManager;
-import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
-        .WINDOW_CONTEXT_ID_PARAMETER_KEY;
-import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContextConfig;
-import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableConversation;
+import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.JsfModuleConfig;
+import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.WindowHandler;
+import org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils;
+import org.apache.myfaces.extensions.cdi.jsf.impl.util.RequestCache;
 
 import javax.enterprise.event.Observes;
-import javax.faces.event.PhaseEvent;
 import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseEvent;
 import java.io.IOException;
 
+import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
+        .CREATE_NEW_WINDOW_CONTEXT_ID_VALUE;
+import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
+        .WINDOW_CONTEXT_ID_PARAMETER_KEY;
+import static org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils.cleanupInactiveWindowContexts;
+import static org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils.resolveWindowContextId;
+import static org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils.storeCurrentViewIdAsNewViewId;
+import static org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils.storeCurrentViewIdAsOldViewId;
+
 /**
  * @author Gerhard Petracek
  */
@@ -123,7 +129,7 @@ final class WindowContextManagerObserver
             String windowId = facesContext.getExternalContext()
                     .getRequestParameterMap().get(WINDOW_CONTEXT_ID_PARAMETER_KEY);
 
-            if("automatedEntryPoint".equalsIgnoreCase(windowId))
+            if(CREATE_NEW_WINDOW_CONTEXT_ID_VALUE.equalsIgnoreCase(windowId))
             {
                 return true;
             }

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/phase/CodiLifecycleWrapper.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/listener/phase/CodiLifecycleWrapper.java?rev=1039316&r1=1039315&r2=1039316&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/phase/CodiLifecycleWrapper.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/phase/CodiLifecycleWrapper.java Fri Nov 26 11:28:45 2010
@@ -18,10 +18,10 @@
  */
 package org.apache.myfaces.extensions.cdi.jsf2.impl.listener.phase;
 
-import javax.faces.lifecycle.Lifecycle;
-import javax.faces.event.PhaseListener;
-import javax.faces.context.FacesContext;
 import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseListener;
+import javax.faces.lifecycle.Lifecycle;
 import java.util.List;
 
 /**
@@ -52,6 +52,8 @@ class CodiLifecycleWrapper extends Lifec
     public void execute(FacesContext facesContext)
             throws FacesException
     {
+        // TODO move WindowHandlerPhaseListener invocation here (as early as possible, before any other PhaseListener) ?
+
         wrapped.execute(facesContext);
     }
 

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=1039316&r1=1039315&r2=1039316&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 11:28:45 2010
@@ -23,17 +23,21 @@ import org.apache.myfaces.extensions.cdi
 import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableWindowContext;
 import org.apache.myfaces.extensions.cdi.jsf.impl.scope.conversation.spi.EditableWindowContextManager;
 
-import javax.faces.application.ResourceHandler;
+import javax.faces.FacesException;
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 import javax.faces.event.PhaseEvent;
 import javax.faces.event.PhaseId;
 import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.OutputStream;
 
+import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
+        .CREATE_NEW_WINDOW_CONTEXT_ID_VALUE;
+import static org.apache.myfaces.extensions.cdi.core.impl.scope.conversation.spi.WindowContextManager
+        .WINDOW_CONTEXT_ID_PARAMETER_KEY;
+
 /**
  * JSF Phase Listener to track windowId creation.
  *
@@ -43,6 +47,9 @@ import java.io.OutputStream;
 @JsfPhaseListener
 public class WindowHandlerPhaseListener implements javax.faces.event.PhaseListener
 {
+
+    private static final String WINDOW_ID_COOKIE_NAME = "codiWindowId";
+
     private Boolean isClientHandlerEnabled = null;
 
     public PhaseId getPhaseId()
@@ -53,114 +60,65 @@ public class WindowHandlerPhaseListener 
     public void beforePhase(PhaseEvent phaseEvent)
     {
         FacesContext facesContext = phaseEvent.getFacesContext();
-        if (facesContext.isPostback())
+
+        if (facesContext.isPostback() || !isClientHandlerEnabled())
         {
+            // POST request or not enabled
             return;
         }
-        if (isClientHandlerEnabled == null)
-        {
-            //X TODO gerhard, where got this config moved to?
-            //X isClientHandlerEnabled = CodiUtils.getOrCreateScopedInstanceOfBeanByClass(
-            isClientHandlerEnabled = Boolean.TRUE; //X TODO get from codi config!
-        }
 
-        if (!isClientHandlerEnabled)
+        ClientInformation clientInfo =
+                CodiUtils.getOrCreateScopedInstanceOfBeanByClass(ClientInformation.class, false);
+        if (!clientInfo.isJavaScriptEnabled())
         {
+            // no handling possible
             return;
         }
         
-        // request/response have to support http
         ExternalContext externalContext = facesContext.getExternalContext();
-        HttpServletRequest httpRequest = (HttpServletRequest) externalContext.getRequest();
-        HttpServletResponse httpResponse = (HttpServletResponse) externalContext.getResponse();
 
-        if ("GET".equals(httpRequest.getMethod()) && !isResourceRequest(httpRequest))
+        String windowId = getWindowIdFromCookie(externalContext);
+        if (windowId == null)
+        {
+            // GET request without windowId - send windowhandlerfilter.html
+            sendWindowHandler(externalContext, clientInfo, null);
+            facesContext.responseComplete();
+        }
+        else
         {
-            // only for GET requests
-
-            ClientInformation clientInfo =
-                    CodiUtils.getOrCreateScopedInstanceOfBeanByClass(ClientInformation.class, false);
-
             EditableWindowContextManager windowContextManager =
                     CodiUtils.getOrCreateScopedInstanceOfBeanByClass(EditableWindowContextManager.class, false);
 
-            String windowId = null;
-            if (!clientInfo.isJavaScriptEnabled())
+            if (CREATE_NEW_WINDOW_CONTEXT_ID_VALUE.equals(windowId) || !isWindowIdAlive(windowId, windowContextManager))
             {
-                // no handling possible
-                return;
-            }
-
-            Cookie[] cookies = httpRequest.getCookies();
-
-            if (cookies != null)
-            {
-                for (Cookie cookie : cookies)
-                {
-                    if ("codiWindowId".equals(cookie.getName()))
-                    {
-                        windowId = cookie.getValue();
-                        cookie.setMaxAge(0);
-                        break;
-                    }
-                }
-            }
+                // no or invalid windowId --> create new one
+                windowId = windowContextManager.getCurrentWindowContext().getId();
 
-            if (windowId == null)
-            {
-                // GET request without windowId - send windowhandlerfilter.html
-                sendWindowHandler(httpRequest, httpResponse, clientInfo, null);
+                // GET request with NEW windowId - send windowhandlerfilter.html
+                sendWindowHandler(externalContext, clientInfo, windowId);
                 facesContext.responseComplete();
             }
             else
             {
-                boolean windowIdAlive = false;
-                for (EditableWindowContext wc : windowContextManager.getWindowContexts())
-                {
-                    if (windowId.equals(wc.getId()))
-                    {
-                        windowIdAlive = true;
-                        break;
-                    }
-                }
-
-
-                if ("automatedEntryPoint".equals(windowId) || !windowIdAlive)
-                {
-                    windowId = windowContextManager.getCurrentWindowContext().getId();
+                // we have a valid windowId - set it an continue with the request
 
-                    // GET request with NEW windowId - send windowhandlerfilter.html
-                    sendWindowHandler(httpRequest, httpResponse, clientInfo, windowId);
-                    facesContext.responseComplete();
-                }
-                else
-                {
-                    httpRequest.setAttribute("windowId", windowId);
-                }
+                //X TODO find better way to provide the windowId, because this approach assumes
+                // that the windowId will be cached on the RequestMap and the cache is the only
+                // point to get it #HACK
+                externalContext.getRequestMap().put(WINDOW_CONTEXT_ID_PARAMETER_KEY, windowId);
             }
         }
     }
 
-    public void destroy()
-    {
-    }
-
-    private boolean isResourceRequest(HttpServletRequest httpRequest)
-    {
-        // TODO more detail: copy algorithm from ResourceHandlerImpl
-
-        String requestURL = httpRequest.getRequestURL().toString();
-
-        return requestURL.contains(ResourceHandler.RESOURCE_IDENTIFIER);
-    }
-
-    private void sendWindowHandler(HttpServletRequest req, HttpServletResponse resp,
+    private void sendWindowHandler(ExternalContext externalContext,
                                    ClientInformation clientInfo, String windowId)
     {
+        HttpServletResponse httpResponse = (HttpServletResponse) externalContext.getResponse();
+
         try
         {
-            resp.setStatus(HttpServletResponse.SC_OK);
-            resp.setContentType("text/html");
+            httpResponse.setStatus(HttpServletResponse.SC_OK);
+            httpResponse.setContentType("text/html");
 
             String windowHandlerHtml = clientInfo.getWindowHandlerHtml();
 
@@ -170,7 +128,7 @@ public class WindowHandlerPhaseListener 
                 windowHandlerHtml = windowHandlerHtml.replace("uninitializedWindowId", windowId);
             }
 
-            OutputStream os = resp.getOutputStream();
+            OutputStream os = httpResponse.getOutputStream();
             try
             {
                     os.write(windowHandlerHtml.getBytes());
@@ -182,14 +140,50 @@ public class WindowHandlerPhaseListener 
         }
         catch (IOException ioe)
         {
-            throw new RuntimeException(ioe);
+            throw new FacesException(ioe);
         }
     }
 
+    private boolean isClientHandlerEnabled()
+    {
+        if (isClientHandlerEnabled == null)
+        {
+            //X TODO gerhard, where got this config moved to?
+            //X isClientHandlerEnabled = CodiUtils.getOrCreateScopedInstanceOfBeanByClass(
+            isClientHandlerEnabled = Boolean.TRUE; //X TODO get from codi config!
+        }
+
+        return isClientHandlerEnabled; 
+    }
+
+    private String getWindowIdFromCookie(ExternalContext externalContext)
+    {
+        Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(WINDOW_ID_COOKIE_NAME);
+
+        if (cookie != null)
+        {
+            return cookie.getValue();
+        }
+
+        return null;
+    }
+
+    private boolean isWindowIdAlive(String windowId, EditableWindowContextManager windowContextManager)
+    {
+        for (EditableWindowContext wc : windowContextManager.getWindowContexts())
+        {
+            if (windowId.equals(wc.getId()))
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     public void afterPhase(PhaseEvent phaseEvent)
     {
        // do nothing
     }
 
-
 }