You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ge...@apache.org on 2009/11/15 02:51:31 UTC

svn commit: r836312 - in /incubator/openwebbeans/trunk/webbeans-jsf/src: main/java/org/apache/webbeans/jsf/ main/java/org/apache/webbeans/spi/conversation/jsf/ main/java/org/apache/webbeans/util/ test/ test/java/ test/java/org/ test/java/org/apache/ te...

Author: gerdogdu
Date: Sun Nov 15 01:51:30 2009
New Revision: 836312

URL: http://svn.apache.org/viewvc?rev=836312&view=rev
Log:
Update while running TCK.

Added:
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/
    incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java   (with props)
Modified:
    incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansJSFFilter.java
    incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java   (contents, props changed)
    incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/spi/conversation/jsf/JSFConversationServiceImpl.java
    incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/util/JSFUtil.java

Modified: incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansJSFFilter.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansJSFFilter.java?rev=836312&r1=836311&r2=836312&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansJSFFilter.java (original)
+++ incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansJSFFilter.java Sun Nov 15 01:51:30 2009
@@ -63,18 +63,18 @@
 
                     if (!conversation.isTransient())
                     {
-                        // Find JSFish view od of the redirection
-                        path = JSFUtil.getRedirectViewId(location);
-                        path = JSFUtil.getViewHandler().getActionURL(JSFUtil.getCurrentFacesContext(), path);
-                        path = JSFUtil.getExternalContext().encodeActionURL(path + "?cid=" + conversation.getId());
+                        WebBeansPhaseListener.fromRedirect.set(Boolean.TRUE);
+                        
+                        path = JSFUtil.getRedirectViewIdWithCid(location, conversation.getId());
                     }
                 }
-
-                super.sendRedirect(path);
+                                
+                super.sendRedirect(path);                
 
             }
 
         };
+                
 
         chain.doFilter(request, responseWrapper);
     }

Modified: incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java?rev=836312&r1=836311&r2=836312&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java (original)
+++ incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java Sun Nov 15 01:51:30 2009
@@ -13,73 +13,94 @@
  */
 package org.apache.webbeans.jsf;
 
-import javax.enterprise.context.ConversationScoped;
 import javax.faces.component.UIViewRoot;
 import javax.faces.event.PhaseEvent;
 import javax.faces.event.PhaseId;
 import javax.faces.event.PhaseListener;
 
-import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.context.ContextFactory;
-import org.apache.webbeans.context.ConversationContext;
 import org.apache.webbeans.conversation.ConversationImpl;
 import org.apache.webbeans.conversation.ConversationManager;
 import org.apache.webbeans.logger.WebBeansLogger;
 import org.apache.webbeans.util.JSFUtil;
 
+/**
+ * Conversation related phase listener.
+ * 
+ * @version $Rev$ $Date$
+ *
+ */
 public class WebBeansPhaseListener implements PhaseListener
 {
-    private static final long serialVersionUID = -8131516076829979596L;
-
-    private static WebBeansLogger logger = WebBeansLogger.getLogger(WebBeansPhaseListener.class);
+    /**Logger instance*/
+    private static final WebBeansLogger logger = WebBeansLogger.getLogger(WebBeansPhaseListener.class);
 
-    private static ConversationManager conversationManager = ConversationManager.getInstance();
+    /**Conversation manager*/
+    private static final ConversationManager conversationManager = ConversationManager.getInstance();
     
+    /**Attribute id that conversation id is saved under*/
     public static final String CONVERSATION_ATTR_ID = "javax_webbeans_ConversationId";
 
+    /**current conversation if exist*/
     private ConversationImpl conversation = null;
+    
+    public static ThreadLocal<Boolean> fromRedirect = new ThreadLocal<Boolean>();
 
+    static
+    {
+        fromRedirect.set(Boolean.FALSE);
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
     public void afterPhase(PhaseEvent phaseEvent)
     {
         if (phaseEvent.getPhaseId().equals(PhaseId.RESTORE_VIEW))
         {
+            //Get request
             if (!JSFUtil.isPostBack())
             {
                 String cid = JSFUtil.getExternalContext().getRequestParameterMap().get("cid");
 
-                // non-faces get request
                 if (cid == null || cid.equals(""))
                 {
                     logger.info("Create new transitional conversation for non-faces request with view id : " + JSFUtil.getViewId());
                     
-                    conversation = (ConversationImpl) conversationManager.createNewConversation();
+                    conversation = (ConversationImpl) conversationManager.createNewConversationInstance();                    
 
                     ContextFactory.initConversationContext(null);
 
                 }
                 else
                 {
-                    logger.info("Propogation of the conversation with id : " + cid + " for view : " + JSFUtil.getViewId());
-                    conversation = (ConversationImpl) conversationManager.getConversation(cid);
+                    logger.info("Propogation of the conversation for non-faces request with cid=" + cid + " for view : " + JSFUtil.getViewId());
+                    
+                    conversation = (ConversationImpl) conversationManager.getConversation(cid, JSFUtil.getSession().getId());
 
                     // can not restore conversation, create new transitional
                     if (conversation == null)
                     {
-                        logger.info("Propogated conversation can not be restored for view id : " + JSFUtil.getViewId() + ". Creates new transitional conversation");
-                        conversation = (ConversationImpl) conversationManager.createNewConversation();
+                        logger.info("Propogated conversation for non-faces request can not be restored for view id : " + JSFUtil.getViewId() + ". Creates new transitional conversation");
+                        conversation = (ConversationImpl) conversationManager.createNewConversationInstance();
 
-                        ContextFactory.initConversationContext(null);
+                        ContextFactory.initConversationContext(null);                        
                     }
                     else
                     {
+                        logger.info("Conversation is restored for non-faces request with cid=" + cid + " for view id : " + JSFUtil.getViewId());
+                        
                         ContextFactory.initConversationContext(conversationManager.getConversationContext(conversation));
                     }
                 }
+                
+                if(fromRedirect.get() != null && fromRedirect.get() && conversation.getId() != null)
+                {
+                    this.conversation.setTransient(false);  
+                }                
             }
             else
             {
-                logger.info("Postback JSF Request for view id : " + JSFUtil.getViewId());
-
                 UIViewRoot viewRoot = JSFUtil.getViewRoot();
                 
                 Object attr = viewRoot.getAttributes().get(CONVERSATION_ATTR_ID);
@@ -91,21 +112,35 @@
                     conversationId = attr.toString();
                 }
                 
+                boolean createNew = false;
+                
                 if (conversationId != null)
                 {
                     // look long running conversation if exist
-                    conversation = (ConversationImpl) conversationManager.getConversation(conversationId);
+                    conversation = (ConversationImpl) conversationManager.getConversation(conversationId, JSFUtil.getSession().getId());
                     
-                    ContextFactory.initConversationContext(conversationManager.getConversationContext(conversation));
+                    if(conversation != null)
+                    {
+                        logger.info("Conversation is restored for JSF postback with cid=" + conversationId + " for view id : " + JSFUtil.getViewId());
+                        
+                        ContextFactory.initConversationContext(conversationManager.getConversationContext(conversation));   
+                    }
+                    else
+                    {
+                        createNew = true;
+                    }
 
                 }
-
-                // no long running conversation, create one transitional
                 else
                 {
+                    createNew = true;
+                }
+
+                if(createNew)
+                {
                     logger.info("Create new transient conversation for JSF postback view id : " + JSFUtil.getViewId());
                     
-                    conversation = (ConversationImpl) conversationManager.createNewConversation();
+                    conversation = (ConversationImpl) conversationManager.createNewConversationInstance();
 
                     ContextFactory.initConversationContext(null);
                 }
@@ -114,29 +149,24 @@
 
         else if (phaseEvent.getPhaseId().equals(PhaseId.RENDER_RESPONSE))
         {
-            ConversationContext context = (ConversationContext) BeanManagerImpl.getManager().getContext(ConversationScoped.class);
-
-            // if long running, saves it
-            if (!conversation.isTransient())
+            if (conversation.isTransient())
             {
-                logger.info("Conversation with id : " + conversation.getId() + " is marked as long running conversation");
+                logger.info("Destroying the conversation context with cid="+ conversation.getId() + " for view id : " + JSFUtil.getViewId());
                 
-                context.setActive(false);
+                this.conversation.end();
                 
+                ContextFactory.destroyConversationContext();                                                    
             }
-
-            // else destroy conversation context
             else
             {
-                logger.info("Destroying the conversation context for view id : " + JSFUtil.getViewId());
-                
-                ContextFactory.destroyConversationContext();                                    
+                conversation.updateTimeOut();
             }
-
         }
-
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void beforePhase(PhaseEvent phaseEvent)
     {
         if (phaseEvent.getPhaseId().equals(PhaseId.RESTORE_VIEW))
@@ -144,31 +174,12 @@
             ContextFactory.initConversationContext(null);            
         }
         
-        else if(phaseEvent.getPhaseId().equals(PhaseId.APPLY_REQUEST_VALUES))
-        {
-
-            if (JSFUtil.isPostBack())
-            {
-                logger.info("Activating the conversation context for view id : " + JSFUtil.getViewId());                
-
-                conversation.updateTimeOut();
-            }            
-        }
-
         else if (phaseEvent.getPhaseId().equals(PhaseId.RENDER_RESPONSE))
         {
-            ConversationContext context = (ConversationContext) BeanManagerImpl.getManager().getContext(ConversationScoped.class);
-
-            if (!JSFUtil.isPostBack())
-            {
-                logger.info("Activating the conversation context for view id : " + JSFUtil.getViewId());
-                context.setActive(true);
-
-                conversation.updateTimeOut();
-            }
-
             if (!conversation.isTransient())
             {
+                logger.info("Saving conversation with cid=" + this.conversation.getId() + " for view " + JSFUtil.getViewId());
+                
                 UIViewRoot viewRoot = JSFUtil.getViewRoot();
                 
                 viewRoot.getAttributes().put(CONVERSATION_ATTR_ID, conversation.getId());

Propchange: incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/jsf/WebBeansPhaseListener.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/spi/conversation/jsf/JSFConversationServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/spi/conversation/jsf/JSFConversationServiceImpl.java?rev=836312&r1=836311&r2=836312&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/spi/conversation/jsf/JSFConversationServiceImpl.java (original)
+++ incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/spi/conversation/jsf/JSFConversationServiceImpl.java Sun Nov 15 01:51:30 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.webbeans.spi.conversation.jsf;
 
+import javax.servlet.http.HttpSession;
+
 import org.apache.webbeans.spi.conversation.ConversationService;
 import org.apache.webbeans.util.JSFUtil;
 
@@ -29,7 +31,13 @@
 
     public String getConversationSessionId()
     {
-        return JSFUtil.getSession().getId();
+        HttpSession session = JSFUtil.getSession();
+        if(session != null)
+        {
+            return session.getId();
+        }
+        
+        return null;
     }
 
 }

Modified: incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/util/JSFUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/util/JSFUtil.java?rev=836312&r1=836311&r2=836312&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/util/JSFUtil.java (original)
+++ incubator/openwebbeans/trunk/webbeans-jsf/src/main/java/org/apache/webbeans/util/JSFUtil.java Sun Nov 15 01:51:30 2009
@@ -37,12 +37,26 @@
 
     public static ExternalContext getExternalContext()
     {
-        return getCurrentFacesContext().getExternalContext();
+        FacesContext context = getCurrentFacesContext();
+        
+        if(context != null)
+        {
+            return context.getExternalContext();
+        }
+        
+        return null;
     }
 
     public static HttpSession getSession()
     {
-        return (HttpSession) getExternalContext().getSession(true);
+        ExternalContext externalContext = getExternalContext();
+        
+        if(externalContext != null)
+        {
+            return (HttpSession) externalContext.getSession(true);
+        }
+        
+        return null; 
     }
 
     public static boolean isPostBack()
@@ -71,19 +85,67 @@
         getCurrentFacesContext().addMessage(null, fm);
     }
 
-    public static String getRedirectViewId(String redirectId)
+    public static String getRedirectViewIdWithCid(String redirectId, String cid)
     {
-        Asserts.assertNotNull(redirectId, "redirectId parameter can not be null");
-        String path = getExternalContext().getRequestContextPath();
-
-        int index = redirectId.indexOf(path);
+        Asserts.assertNotNull(redirectId, "redirectId parameter can not be null");        
 
-        return redirectId.substring(index + path.length(), redirectId.length());
+        StringBuffer buffer = new StringBuffer(redirectId);
+        int index = buffer.lastIndexOf("/");        
+        
+        String remainder = buffer.substring(index+1, buffer.length());
+        int indexOfQuery = remainder.indexOf('?');
+        
+        StringBuffer result = new StringBuffer();
+        if(indexOfQuery != -1)
+        {
+            result.append(buffer.substring(0,index+1));
+            result.append(remainder.substring(0,indexOfQuery+1));
+            result.append("cid");
+            result.append("=");
+            result.append(cid);
+            result.append("&");
+            result.append(remainder.substring(indexOfQuery+1, remainder.length()));            
+        }
+        else
+        {
+            int pathIndex = remainder.indexOf("#");
+            
+            if(pathIndex == -1)
+            {
+                result.append(buffer.substring(0,index+1));
+                result.append(remainder.substring(0));
+                result.append("?");
+                result.append("cid");
+                result.append("=");
+                result.append(cid);
+            }
+            else
+            {
+                result.append(buffer.substring(0,index+1));
+                result.append(remainder.substring(0,pathIndex));
+                result.append("?");
+                result.append("cid");
+                result.append("=");
+                result.append(cid);
+                result.append(remainder.substring(pathIndex,remainder.length()));
+            }            
+        }
+        
+        
+        return result.toString();
+            
     }
 
     public static UIViewRoot getViewRoot()
     {
-        return getCurrentFacesContext().getViewRoot();
+        FacesContext context = getCurrentFacesContext();
+        
+        if(context != null)
+        {
+            return context.getViewRoot();
+        }
+        
+        return null;
     }
 
     public static String getConversationId()

Added: incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java?rev=836312&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java (added)
+++ incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java Sun Nov 15 01:51:30 2009
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
+ * or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.apache.webbeans.jsf.test;
+
+import org.apache.webbeans.util.JSFUtil;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public class JSFUtilTest extends TestCase
+{
+    public void testCidUrlUpdate()
+    {
+        String withQuery = "/context/file.get?name=hipo";
+        String withQueryWithPath = "/context/file.get?name=hipo#same";
+        String withoutQuery = "/context/file.get";
+        String withoutQueryWithPath="/context/file.get#same";
+        String withContext = "/context";
+        String withContextWithPath = "/context#same";
+        String withQueryWithContext = "/context?hiho=hi";
+        String withQueryWithContextWithPath="/context?hiho=hi#same";
+        
+        String cid = "1";
+        
+        String path = JSFUtil.getRedirectViewIdWithCid(withQuery, cid);        
+        Assert.assertEquals("/context/file.get?cid=1&name=hipo", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withQueryWithPath, cid);        
+        Assert.assertEquals("/context/file.get?cid=1&name=hipo#same", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withoutQuery, cid);        
+        Assert.assertEquals("/context/file.get?cid=1", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withoutQueryWithPath, cid);        
+        Assert.assertEquals("/context/file.get?cid=1#same", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withContext, cid);        
+        Assert.assertEquals("/context?cid=1", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withContextWithPath, cid);
+        Assert.assertEquals("/context?cid=1#same", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withQueryWithContext, cid);
+        Assert.assertEquals("/context?cid=1&hiho=hi", path);
+        
+        path = JSFUtil.getRedirectViewIdWithCid(withQueryWithContextWithPath, cid);
+        Assert.assertEquals("/context?cid=1&hiho=hi#same", path);
+        
+        
+    }
+
+}

Propchange: incubator/openwebbeans/trunk/webbeans-jsf/src/test/java/org/apache/webbeans/jsf/test/JSFUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native