You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/11/22 23:40:12 UTC

svn commit: r1205204 - in /myfaces/core/trunk: api/src/main/java/javax/faces/application/ impl/src/main/java/org/apache/myfaces/application/ impl/src/main/java/org/apache/myfaces/context/servlet/ impl/src/test/java/org/apache/myfaces/context/servlet/ s...

Author: lu4242
Date: Tue Nov 22 22:40:11 2011
New Revision: 1205204

URL: http://svn.apache.org/viewvc?rev=1205204&view=rev
Log:
MYFACES-3405 includeViewParameters re-evaluates param/model values as EL expressions

Added:
    myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java   (with props)
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java   (with props)
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/application/NavigationCase.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/application/NavigationCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/application/NavigationCase.java?rev=1205204&r1=1205203&r2=1205204&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/application/NavigationCase.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/application/NavigationCase.java Tue Nov 22 22:40:11 2011
@@ -138,8 +138,8 @@ public class NavigationCase
         return new URL(externalContext.getRequestScheme(),
                 externalContext.getRequestServerName(),
                 externalContext.getRequestServerPort(),
-                context.getApplication().getViewHandler().getBookmarkableURL(context, getToViewId(context),
-                                                                             getParameters(), isIncludeViewParams()));
+                context.getApplication().getViewHandler().getBookmarkableURL(context, getToViewId(context), 
+                        _NavigationUtils.getEvaluatedNavigationParameters(getParameters()), isIncludeViewParams()));
     }
 
     public URL getResourceURL(FacesContext context) throws MalformedURLException
@@ -156,8 +156,8 @@ public class NavigationCase
         return new URL(externalContext.getRequestScheme(),
                 externalContext.getRequestServerName(),
                 externalContext.getRequestServerPort(),
-                context.getApplication().getViewHandler().getRedirectURL(context, getToViewId(context),
-                                                                         getParameters(), isIncludeViewParams()));
+                context.getApplication().getViewHandler().getRedirectURL(context, getToViewId(context), 
+                        _NavigationUtils.getEvaluatedNavigationParameters(getParameters()), isIncludeViewParams()));
     }
     
     public Map<String,List<String>> getParameters()

Added: myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java?rev=1205204&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java (added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java Tue Nov 22 22:40:11 2011
@@ -0,0 +1,107 @@
+/*
+ * 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 javax.faces.application;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+class _NavigationUtils
+{
+    /**
+     * Evaluate all EL expressions found as parameters and return a map that can be used for 
+     * redirect or render bookmark links
+     * 
+     * @param parameters parameter map retrieved from NavigationCase.getParameters()
+     * @return
+     */
+    public static Map<String, List<String> > getEvaluatedNavigationParameters(Map<String, List<String> > parameters)
+    {
+        Map<String,List<String>> evaluatedParameters = null;
+        if (parameters != null && parameters.size() > 0)
+        {
+            evaluatedParameters = new HashMap<String, List<String>>();
+            for (Map.Entry<String, List<String>> pair : parameters.entrySet())
+            {
+                boolean containsEL = false;
+                for (String value : pair.getValue())
+                {
+                    if (_isExpression(value))
+                    {
+                        containsEL = true;
+                        break;
+                    }
+                }
+                if (containsEL)
+                {
+                    evaluatedParameters.put(pair.getKey(), _evaluateValueExpressions(pair.getValue()));
+                }
+                else
+                {
+                    evaluatedParameters.put(pair.getKey(), pair.getValue());
+                }
+            }
+        }
+        else
+        {
+            evaluatedParameters = parameters;
+        }
+        return evaluatedParameters;
+    }
+    
+    /**
+     * Checks the Strings in the List for EL expressions and evaluates them.
+     * Note that the returned List will be a copy of the given List, because
+     * otherwise it will have unwanted side-effects.
+     * @param values
+     * @return
+     */
+    private static List<String> _evaluateValueExpressions(List<String> values)
+    {
+        // note that we have to create a new List here, because if we
+        // change any value on the given List, it will be changed in the
+        // NavigationCase too and the EL expression won't be evaluated again
+        List<String> target = new ArrayList<String>(values.size());
+        FacesContext context = FacesContext.getCurrentInstance();
+        for (String value : values)
+        {
+            if (_isExpression(value))
+            {
+                // evaluate the ValueExpression
+                value = context.getApplication().evaluateExpressionGet(context, value, String.class);
+            }
+            target.add(value);
+        }
+        return target;
+    }
+    
+    private static boolean _isExpression(String text)
+    {
+        return text.indexOf("#{") != -1;
+    }
+
+}

Propchange: myfaces/core/trunk/api/src/main/java/javax/faces/application/_NavigationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java?rev=1205204&r1=1205203&r2=1205204&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java Tue Nov 22 22:40:11 2011
@@ -50,6 +50,7 @@ import javax.faces.view.ViewMetadata;
 
 import org.apache.myfaces.config.RuntimeConfig;
 import org.apache.myfaces.config.element.NavigationRule;
+import org.apache.myfaces.shared.application.NavigationUtils;
 import org.apache.myfaces.shared.util.HashMapUtils;
 import org.apache.myfaces.shared.util.StringUtils;
 import org.apache.myfaces.view.facelets.tag.jsf.PreDisposeViewEvent;
@@ -104,8 +105,12 @@ public class NavigationHandlerImpl
                 ExternalContext externalContext = facesContext.getExternalContext();
                 ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
                 String toViewId = navigationCase.getToViewId(facesContext);
-                String redirectPath = viewHandler.getRedirectURL(facesContext, toViewId, navigationCase.getParameters(),
-                                                                 navigationCase.isIncludeViewParams());
+                
+
+                String redirectPath = viewHandler.getRedirectURL(
+                        facesContext, toViewId, 
+                        NavigationUtils.getEvaluatedNavigationParameters(navigationCase.getParameters()) ,
+                        navigationCase.isIncludeViewParams());
                 
                 //Clear ViewMap if we are redirecting to other resource
                 UIViewRoot viewRoot = facesContext.getViewRoot(); 

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java?rev=1205204&r1=1205203&r2=1205204&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java Tue Nov 22 22:40:11 2011
@@ -768,7 +768,7 @@ public final class ServletExternalContex
             {
                 if (pair.getKey() != null && pair.getKey().trim().length() != 0)
                 {
-                    paramMap.put(pair.getKey(), _evaluateValueExpressions(pair.getValue()));
+                    paramMap.put(pair.getKey(), pair.getValue());
                 }
             }
         }
@@ -820,37 +820,6 @@ public final class ServletExternalContex
     }
     
     /**
-     * Checks the Strings in the List for EL expressions and evaluates them.
-     * Note that the returned List will be a copy of the given List, because
-     * otherwise it will have unwanted side-effects.
-     * @param values
-     * @return
-     */
-    private List<String> _evaluateValueExpressions(List<String> values)
-    {
-        // note that we have to create a new List here, because if we
-        // change any value on the given List, it will be changed in the
-        // NavigationCase too and the EL expression won't be evaluated again
-        List<String> target = new ArrayList<String>(values.size());
-        FacesContext context = FacesContext.getCurrentInstance();
-        for (String value : values)
-        {
-            if (_isExpression(value))
-            {
-                // evaluate the ValueExpression
-                value = context.getApplication().evaluateExpressionGet(context, value, String.class);
-            }
-            target.add(value);
-        }
-        return target;
-    }
-    
-    private boolean _isExpression(String text)
-    {
-        return text.indexOf("#{") != -1;
-    }
-    
-    /**
      * @since 2.0
      */
     public Flash getFlash()

Modified: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java?rev=1205204&r1=1205203&r2=1205204&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java (original)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java Tue Nov 22 22:40:11 2011
@@ -65,6 +65,7 @@ public class ServletExternalContextImplT
      * Tests if encodeRedirectURL() and encodeBookmarkableURL() correctly
      * evaluate ValueExpressions as parameters values.
      */
+    /* TODO: Invalid test, because EL evaluation should be done before call these methods.
     @Test
     @SuppressWarnings("unchecked")
     public void testEncodeURLHandlesValueExpressionParameters()
@@ -94,7 +95,7 @@ public class ServletExternalContextImplT
         Assert.assertTrue(bookmarkableUrl.contains("param1=literalvalue"));
         Assert.assertTrue(bookmarkableUrl.contains("param1=myvalue1"));
         Assert.assertTrue(bookmarkableUrl.contains("param2=myvalue2"));
-    }
+    }*/
 
     @Test
     public void testEncodeRedirectUrlWithEmptyParamInBaseUrl()

Added: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java?rev=1205204&view=auto
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java (added)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java Tue Nov 22 22:40:11 2011
@@ -0,0 +1,107 @@
+/*
+ * 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.myfaces.shared.application;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * 
+ * @author Leonardo Uribe
+ *
+ */
+public class NavigationUtils
+{
+    /**
+     * Evaluate all EL expressions found as parameters and return a map that can be used for 
+     * redirect or render bookmark links
+     * 
+     * @param parameters parameter map retrieved from NavigationCase.getParameters()
+     * @return
+     */
+    public static Map<String, List<String> > getEvaluatedNavigationParameters(Map<String, List<String> > parameters)
+    {
+        Map<String,List<String>> evaluatedParameters = null;
+        if (parameters != null && parameters.size() > 0)
+        {
+            evaluatedParameters = new HashMap<String, List<String>>();
+            for (Map.Entry<String, List<String>> pair : parameters.entrySet())
+            {
+                boolean containsEL = false;
+                for (String value : pair.getValue())
+                {
+                    if (_isExpression(value))
+                    {
+                        containsEL = true;
+                        break;
+                    }
+                }
+                if (containsEL)
+                {
+                    evaluatedParameters.put(pair.getKey(), _evaluateValueExpressions(pair.getValue()));
+                }
+                else
+                {
+                    evaluatedParameters.put(pair.getKey(), pair.getValue());
+                }
+            }
+        }
+        else
+        {
+            evaluatedParameters = parameters;
+        }
+        return evaluatedParameters;
+    }
+    
+    /**
+     * Checks the Strings in the List for EL expressions and evaluates them.
+     * Note that the returned List will be a copy of the given List, because
+     * otherwise it will have unwanted side-effects.
+     * @param values
+     * @return
+     */
+    private static List<String> _evaluateValueExpressions(List<String> values)
+    {
+        // note that we have to create a new List here, because if we
+        // change any value on the given List, it will be changed in the
+        // NavigationCase too and the EL expression won't be evaluated again
+        List<String> target = new ArrayList<String>(values.size());
+        FacesContext context = FacesContext.getCurrentInstance();
+        for (String value : values)
+        {
+            if (_isExpression(value))
+            {
+                // evaluate the ValueExpression
+                value = context.getApplication().evaluateExpressionGet(context, value, String.class);
+            }
+            target.add(value);
+        }
+        return target;
+    }
+    
+    private static boolean _isExpression(String text)
+    {
+        return text.indexOf("#{") != -1;
+    }
+
+}

Propchange: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/application/NavigationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java?rev=1205204&r1=1205203&r2=1205204&view=diff
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java (original)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlRendererUtils.java Tue Nov 22 22:40:11 2011
@@ -63,6 +63,7 @@ import javax.faces.convert.Converter;
 import javax.faces.model.SelectItem;
 import javax.faces.model.SelectItemGroup;
 
+import org.apache.myfaces.shared.application.NavigationUtils;
 import org.apache.myfaces.shared.component.DisplayValueOnlyCapable;
 import org.apache.myfaces.shared.component.EscapeCapable;
 import org.apache.myfaces.shared.config.MyfacesConfig;
@@ -1589,8 +1590,8 @@ public final class HtmlRendererUtils
             }
         }
         // handle NavigationCase parameters
-        Map<String, List<String>> navigationCaseParams = navigationCase
-                .getParameters();
+        Map<String, List<String>> navigationCaseParams = 
+            NavigationUtils.getEvaluatedNavigationParameters(navigationCase.getParameters());
         if (navigationCaseParams != null)
         {
             if (parameters == null)