Author: mgrigorov
Date: Tue Jul  5 20:31:42 2011
New Revision: 1143196

URL: http://svn.apache.org/viewvc?rev=1143196&view=rev
Log:
WICKET-3863 SecurePackageResourceGuard acceptAbsolutePath pattern check loop

Optimize the traversal by going from the end to the start and stop at the first 
match.


Added:
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/collections/ReverseListIterator.java
    
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/collections/ReverseListIteratorTest.java
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/SecurePackageResourceGuard.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/SecurePackageResourceGuard.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/SecurePackageResourceGuard.java?rev=1143196&r1=1143195&r2=1143196&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/SecurePackageResourceGuard.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/SecurePackageResourceGuard.java
 Tue Jul  5 20:31:42 2011
@@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentLi
 import java.util.regex.Pattern;
 
 import org.apache.wicket.settings.IResourceSettings;
+import org.apache.wicket.util.collections.ReverseListIterator;
 import org.apache.wicket.util.string.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -142,13 +143,14 @@ public class SecurePackageResourceGuard 
 
                // Check against the pattern
                boolean hit = false;
-               for (SearchPattern pattern : this.pattern)
+               for (SearchPattern pattern : new 
ReverseListIterator<SearchPattern>(this.pattern))
                {
                        if ((pattern != null) && pattern.isActive())
                        {
                                if (pattern.matches(path))
                                {
                                        hit = pattern.isInclude();
+                                       break;
                                }
                        }
                }

Added: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/collections/ReverseListIterator.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/collections/ReverseListIterator.java?rev=1143196&view=auto
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/collections/ReverseListIterator.java
 (added)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/collections/ReverseListIterator.java
 Tue Jul  5 20:31:42 2011
@@ -0,0 +1,64 @@
+/*
+ * 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.wicket.util.collections;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+/**
+ * An iterator over {@link List} which goes from the end to the start
+ * 
+ * @param <E>
+ */
+public class ReverseListIterator<E> implements Iterator<E>, Iterable<E>
+{
+       private final ListIterator<E> delegateIterator;
+
+       /**
+        * Construct.
+        * 
+        * @param list
+        *            the list which will be iterated in reverse order
+        */
+       public ReverseListIterator(final List<E> list)
+       {
+               int start = list.size();
+               this.delegateIterator = list.listIterator(start);
+       }
+
+       public boolean hasNext()
+       {
+               return delegateIterator.hasPrevious();
+       }
+
+       public E next()
+       {
+               return delegateIterator.previous();
+       }
+
+       public void remove()
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       public Iterator<E> iterator()
+       {
+               return this;
+       }
+
+}
\ No newline at end of file

Added: 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/collections/ReverseListIteratorTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/collections/ReverseListIteratorTest.java?rev=1143196&view=auto
==============================================================================
--- 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/collections/ReverseListIteratorTest.java
 (added)
+++ 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/collections/ReverseListIteratorTest.java
 Tue Jul  5 20:31:42 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.wicket.util.collections;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link ReverseListIterator}
+ */
+public class ReverseListIteratorTest extends Assert
+{
+
+       /**
+        * Test that it reverses the items of a list while iterating on them
+        */
+       @Test
+       public void reverse()
+       {
+               List<Integer> list = new ArrayList<Integer>();
+               for (int i = 0; i < 10; i++)
+               {
+                       list.add(i);
+               }
+
+               Integer expected = 9;
+               for (Integer actual : new ReverseListIterator<Integer>(list))
+               {
+                       assertEquals(expected, actual);
+                       expected--;
+               }
+       }
+
+}


Reply via email to