cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r746647 - in /cocoon/branches/BRANCH_2_1_X: ./ src/java/org/apache/cocoon/selection/ src/test/org/apache/cocoon/selection/
Date Sun, 22 Feb 2009 08:46:59 GMT
Author: joerg
Date: Sun Feb 22 08:46:59 2009
New Revision: 746647

URL: http://svn.apache.org/viewvc?rev=746647&view=rev
Log:
COCOON-1887: Change HostSelector to be case-insensitive according to RFC3986 section 3.2.2.

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/HostSelector.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/NamedPatternsSelector.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.xtest
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/HostSelector.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/HostSelector.java?rev=746647&r1=746646&r2=746647&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/HostSelector.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/HostSelector.java Sun
Feb 22 08:46:59 2009
@@ -16,12 +16,13 @@
  */
 package org.apache.cocoon.selection;
 
+import java.util.Map;
+
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.cocoon.environment.ObjectModelHelper;
 
-import java.util.Map;
+import org.apache.cocoon.environment.ObjectModelHelper;
 
 /**
  * A <code>Selector</code> that matches a string from within the host parameter
@@ -67,6 +68,6 @@
             return false;
         }
 
-        return checkPatterns(expression, host);
+        return checkPatterns(expression, host, false);
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/NamedPatternsSelector.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/NamedPatternsSelector.java?rev=746647&r1=746646&r2=746647&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/NamedPatternsSelector.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/selection/NamedPatternsSelector.java
Sun Feb 22 08:46:59 2009
@@ -16,18 +16,18 @@
  */
 package org.apache.cocoon.selection;
 
-import org.apache.avalon.framework.configuration.Configurable;
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.thread.ThreadSafe;
-
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.thread.ThreadSafe;
+
 /**
  * Abstract class for selectors that select a value when it matches
  * some patterns associated to the select expression.
@@ -39,7 +39,7 @@
  */
 
 public abstract class NamedPatternsSelector extends AbstractLogEnabled
-  implements Configurable, ThreadSafe, Selector {
+                                            implements Configurable, ThreadSafe, Selector
{
 
     /**
      * Association of names to String[] of values.
@@ -95,7 +95,7 @@
     }
 
     /**
-     * Checks if <code>value</code> is a substring of one of the patterns associated
+     * Checks if <code>value</code> is a (case-sensitive) substring of one of
the patterns associated
      * to <code>expression</code>
      *
      * @param expression the expression that is selected
@@ -103,6 +103,19 @@
      * @return true if <code>value</code> matches one of the patterns
      */
     protected boolean checkPatterns(String expression, String value) {
+        return checkPatterns(expression, value, true);
+    }
+
+    /**
+     * Checks if <code>value</code> is a substring of one of the patterns associated
+     * to <code>expression</code>
+     *
+     * @param expression the expression that is selected
+     * @param value the value to check
+     * @param caseSensitive boolean switch whether comparison is done case-sensitive  
+     * @return true if <code>value</code> matches one of the patterns
+     */
+    protected boolean checkPatterns(String expression, String value, boolean caseSensitive)
{
         if (value == null) {
             getLogger().debug("No value given -- failing.");
             return false;
@@ -114,9 +127,14 @@
             return false;
         }
 
+        if (!caseSensitive) {
+            value = value.toLowerCase();
+        }
+
         // Does a pattern match 'value' ?
         for (int i = 0; i < patterns.length; i++) {
-            if (value.indexOf(patterns[i]) != -1) {
+            if ((caseSensitive && value.indexOf(patterns[i]) != -1)
+                || (!caseSensitive && value.indexOf(patterns[i].toLowerCase()) !=
-1)) {
                 getLogger().debug(expression + " selected value " + value);
                 return true;
             }

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.java?rev=746647&r1=746646&r2=746647&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.java
Sun Feb 22 08:46:59 2009
@@ -19,6 +19,7 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
+
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.SitemapComponentTestCase;
 
@@ -92,4 +93,30 @@
         System.out.println(result);
         assertTrue( "Test if host is not " + expectedHostName, !result );
     }
+
+    /**
+     * Test the host selector matches regardless of whether the hostname and
+     * value are upper or lower case.
+     */
+    public void testHostCaseInsensitive() throws Exception {
+        final String hostLower = "myhost-dns-name";
+        final String hostUpper = "MYHOST-DNS-NAME-IN-A-EUROPE-COUNTRY";
+        String expectedHostName;
+
+        Parameters parameters = new Parameters();
+        boolean result;
+
+        getRequest().setHeader("Host", hostUpper);
+        expectedHostName = "myhost-eu";
+        result = this.select(HOST_SELECTOR, expectedHostName, parameters);
+        System.out.println(result);
+        assertTrue("Test upper case host doesn't match lower case value as expected.", result);
+
+        getRequest().setHeader("Host", hostLower);
+        expectedHostName = "myhost-uppercase";
+        result = this.select(HOST_SELECTOR, expectedHostName, parameters);
+        System.out.println(result);
+        assertTrue("Test lower case host doesn't match upper case value as expected.", result);
+    }
+
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.xtest
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.xtest?rev=746647&r1=746646&r2=746647&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.xtest
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/selection/HostSelectorTestCase.xtest
Sun Feb 22 08:46:59 2009
@@ -43,6 +43,7 @@
                        name="host">
       <host name="myhost-eu" value="myhost-dns-name-in-a-europe-country"/>
       <host name="myhost-us" value="myhost-dns-name-in-a-us-state"/>
+      <host name="myhost-uppercase" value="MYHOST-DNS-NAME"/>
     </component-instance>
   </selectors>
  </components>

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?rev=746647&r1=746646&r2=746647&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Sun Feb 22 08:46:59 2009
@@ -186,7 +186,10 @@
     <action dev="all" type="update">
       Starting with 2.1.12 the minimum required Java version will be 1.4.2.
     </action>
-    <action dev="joerg" type="add" fixes-bug="COCOON-2243" due-to="Benjamin Boksa" due-to-email="benjamin@boksa.de">
+    <action dev="JH" type="update" fixes-bug="COCOON-1887" due-to="Andrew Stevens" due-to-email="stevensa@users.sourceforge.net">
+      Change HostSelector to be case-insensitive according to RFC3986 section 3.2.2.
+    </action>
+    <action dev="JH" type="add" fixes-bug="COCOON-2243" due-to="Benjamin Boksa" due-to-email="benjamin@boksa.de">
       Handle case in ApplicationUtil.isUserInRole(..) when User is null.
     </action>
     <action dev="AG" type="fix" fixes-bug="COCOON-1822" due-to="Karen Torres &amp;
Rafael Márquez" due-to-email="ktorres@agssa.net &amp; rmarquez@agssa.net">



Mime
View raw message