Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-12-08 22:51:25 +0000
committerJoakim Erdfelt2015-12-08 22:51:25 +0000
commit5e3fbbccd0df05049f248e5ac3a9207feaeb3b29 (patch)
tree4af1c1748fae358c461b6684f48f5792ad1142f8 /jetty-util
parent7cdc58e6b9921d85023d4255ebbce0c091cb6359 (diff)
downloadorg.eclipse.jetty.project-5e3fbbccd0df05049f248e5ac3a9207feaeb3b29.tar.gz
org.eclipse.jetty.project-5e3fbbccd0df05049f248e5ac3a9207feaeb3b29.tar.xz
org.eclipse.jetty.project-5e3fbbccd0df05049f248e5ac3a9207feaeb3b29.zip
Backporting GzipHandler's IncludeExclude configs
Diffstat (limited to 'jetty-util')
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExclude.java136
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/RegexSet.java103
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/IncludeExcludeTest.java153
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/RegexSetTest.java86
4 files changed, 478 insertions, 0 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExclude.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExclude.java
new file mode 100644
index 0000000000..b5af37356e
--- /dev/null
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExclude.java
@@ -0,0 +1,136 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+
+/** Utility class to maintain a set of inclusions and exclusions.
+ * <p>Maintains a set of included and excluded elements. The method {@link #matches(Object)}
+ * will return true IFF the passed object is not in the excluded set AND ( either the
+ * included set is empty OR the object is in the included set)
+ * <p>The type of the underlying {@link Set} used may be passed into the
+ * constructor, so special sets like Servlet PathMap may be used.
+ * <p>
+ * @param <ITEM> The type of element
+ */
+public class IncludeExclude<ITEM>
+{
+ public interface MatchSet<ITEM> extends Set<ITEM>
+ {
+ public boolean matches(ITEM item);
+ }
+
+ @SuppressWarnings("serial")
+ protected static class ContainsMatchSet<ITEM> extends HashSet<ITEM> implements MatchSet<ITEM>
+ {
+ @Override
+ public boolean matches(ITEM item)
+ {
+ return contains(item);
+ }
+ }
+
+ private final MatchSet<ITEM> _includes;
+ private final MatchSet<ITEM> _excludes;
+
+ /**
+ * Default constructor over {@link HashSet}
+ */
+ public IncludeExclude()
+ {
+ _includes = new ContainsMatchSet<ITEM>();
+ _excludes = new ContainsMatchSet<ITEM>();
+ }
+
+ /**
+ * Construct an IncludeExclude
+ * @param setClass The type of {@link Set} to using internally
+ * @param matcher A function to test if a passed ITEM is matched by the passed SET, or null to use {@link Set#contains(Object)}
+ */
+ public IncludeExclude(Class<? extends MatchSet<ITEM>> setClass)
+ {
+ try
+ {
+ _includes = setClass.newInstance();
+ _excludes = setClass.newInstance();
+ }
+ catch (InstantiationException | IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void include(ITEM element)
+ {
+ _includes.add(element);
+ }
+
+ public void include(ITEM... element)
+ {
+ for (ITEM e: element)
+ _includes.add(e);
+ }
+
+ public void exclude(ITEM element)
+ {
+ _excludes.add(element);
+ }
+
+ public void exclude(ITEM... element)
+ {
+ for (ITEM e: element)
+ _excludes.add(e);
+ }
+
+ public boolean matches(ITEM e)
+ {
+ if (_includes.size()>0 && !_includes.matches(e))
+ return false;
+ return !_excludes.matches(e);
+ }
+
+ public int size()
+ {
+ return _includes.size()+_excludes.size();
+ }
+
+ public Set<ITEM> getIncluded()
+ {
+ return _includes;
+ }
+
+ public Set<ITEM> getExcluded()
+ {
+ return _excludes;
+ }
+
+ public void clear()
+ {
+ _includes.clear();
+ _excludes.clear();
+ }
+
+ @Override
+ public String toString()
+ {
+ return String.format("%s@%x{i=%s,e=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_excludes);
+ }
+}
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RegexSet.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RegexSet.java
new file mode 100644
index 0000000000..288e5154f2
--- /dev/null
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RegexSet.java
@@ -0,0 +1,103 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util;
+
+import java.util.AbstractSet;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * A Set of Regular expressions strings.
+ * <p>
+ * Provides the efficient {@link #matches(String)} method to check for a match against all the combined Regex's
+ */
+public class RegexSet extends AbstractSet<String> implements IncludeExclude.MatchSet<String>
+{
+ private final Set<String> _patterns=new HashSet<String>();
+ private final Set<String> _unmodifiable=Collections.unmodifiableSet(_patterns);
+ private Pattern _pattern;
+
+ @Override
+ public Iterator<String> iterator()
+ {
+ return _unmodifiable.iterator();
+ }
+
+ @Override
+ public int size()
+ {
+ return _patterns.size();
+ }
+
+ @Override
+ public boolean add(String pattern)
+ {
+ boolean added = _patterns.add(pattern);
+ if (added)
+ updatePattern();
+ return added;
+ }
+
+ @Override
+ public boolean remove(Object pattern)
+ {
+ boolean removed = _patterns.remove(pattern);
+
+ if (removed)
+ updatePattern();
+ return removed;
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return _patterns.isEmpty();
+ }
+
+ @Override
+ public void clear()
+ {
+ _patterns.clear();
+ _pattern=null;
+ }
+
+ private void updatePattern()
+ {
+ StringBuilder builder = new StringBuilder();
+ builder.append("^(");
+ for (String pattern: _patterns)
+ {
+ if (builder.length()>2)
+ builder.append('|');
+ builder.append('(');
+ builder.append(pattern);
+ builder.append(')');
+ }
+ builder.append(")$");
+ _pattern = Pattern.compile(builder.toString());
+ }
+
+ public boolean matches(String s)
+ {
+ return _pattern!=null && _pattern.matcher(s).matches();
+ }
+}
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/IncludeExcludeTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/IncludeExcludeTest.java
new file mode 100644
index 0000000000..c2b4d61559
--- /dev/null
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/IncludeExcludeTest.java
@@ -0,0 +1,153 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class IncludeExcludeTest
+{
+ @Test
+ public void testEmpty()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>();
+
+ assertEquals(0,ie.size());
+ assertEquals(true,ie.matches("foo"));
+ }
+
+ @Test
+ public void testIncludeOnly()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>();
+ ie.include("foo");
+ ie.include("bar");
+
+ assertEquals(2,ie.size());
+ assertEquals(false,ie.matches(""));
+ assertEquals(true,ie.matches("foo"));
+ assertEquals(true,ie.matches("bar"));
+ assertEquals(false,ie.matches("foobar"));
+ }
+
+ @Test
+ public void testExcludeOnly()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>();
+ ie.exclude("foo");
+ ie.exclude("bar");
+
+ assertEquals(2,ie.size());
+
+ assertEquals(false,ie.matches("foo"));
+ assertEquals(false,ie.matches("bar"));
+ assertEquals(true,ie.matches(""));
+ assertEquals(true,ie.matches("foobar"));
+ assertEquals(true,ie.matches("wibble"));
+ }
+
+ @Test
+ public void testIncludeExclude()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>();
+ ie.include("foo");
+ ie.include("bar");
+ ie.exclude("bar");
+ ie.exclude("xxx");
+
+ assertEquals(4,ie.size());
+
+ assertEquals(true,ie.matches("foo"));
+ assertEquals(false,ie.matches("bar"));
+ assertEquals(false,ie.matches(""));
+ assertEquals(false,ie.matches("foobar"));
+ assertEquals(false,ie.matches("xxx"));
+ }
+
+
+
+ @Test
+ public void testEmptyRegex()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
+
+ assertEquals(0,ie.size());
+ assertEquals(true,ie.matches("foo"));
+ }
+
+ @Test
+ public void testIncludeRegex()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
+ ie.include("f..");
+ ie.include("b((ar)|(oo))");
+
+ assertEquals(2,ie.size());
+ assertEquals(false,ie.matches(""));
+ assertEquals(true,ie.matches("foo"));
+ assertEquals(true,ie.matches("far"));
+ assertEquals(true,ie.matches("bar"));
+ assertEquals(true,ie.matches("boo"));
+ assertEquals(false,ie.matches("foobar"));
+ assertEquals(false,ie.matches("xxx"));
+ }
+
+ @Test
+ public void testExcludeRegex()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
+ ie.exclude("f..");
+ ie.exclude("b((ar)|(oo))");
+
+ assertEquals(2,ie.size());
+
+ assertEquals(false,ie.matches("foo"));
+ assertEquals(false,ie.matches("far"));
+ assertEquals(false,ie.matches("bar"));
+ assertEquals(false,ie.matches("boo"));
+ assertEquals(true,ie.matches(""));
+ assertEquals(true,ie.matches("foobar"));
+ assertEquals(true,ie.matches("xxx"));
+ }
+
+ @Test
+ public void testIncludeExcludeRegex()
+ {
+ IncludeExclude<String> ie = new IncludeExclude<>(RegexSet.class);
+ ie.include(".*[aeiou].*");
+ ie.include("[AEIOU].*");
+ ie.exclude("f..");
+ ie.exclude("b((ar)|(oo))");
+
+ assertEquals(4,ie.size());
+ assertEquals(false,ie.matches("foo"));
+ assertEquals(false,ie.matches("far"));
+ assertEquals(false,ie.matches("bar"));
+ assertEquals(false,ie.matches("boo"));
+ assertEquals(false,ie.matches(""));
+ assertEquals(false,ie.matches("xxx"));
+
+ assertEquals(true,ie.matches("foobar"));
+ assertEquals(true,ie.matches("Ant"));
+
+ }
+
+
+}
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RegexSetTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RegexSetTest.java
new file mode 100644
index 0000000000..f80c2eda5c
--- /dev/null
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RegexSetTest.java
@@ -0,0 +1,86 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.util;
+
+import org.junit.Test;
+
+import org.junit.Assert;
+
+public class RegexSetTest
+{
+
+ @Test
+ public void testEmpty()
+ {
+ RegexSet set = new RegexSet();
+
+ Assert.assertEquals(false,set.contains("foo"));
+ Assert.assertEquals(false,set.matches("foo"));
+ Assert.assertEquals(false,set.matches(""));
+
+ }
+
+ @Test
+ public void testSimple()
+ {
+ RegexSet set = new RegexSet();
+ set.add("foo.*");
+
+ Assert.assertEquals(true,set.contains("foo.*"));
+ Assert.assertEquals(true,set.matches("foo"));
+ Assert.assertEquals(true,set.matches("foobar"));
+ Assert.assertEquals(false,set.matches("bar"));
+ Assert.assertEquals(false,set.matches(""));
+
+ }
+
+ @Test
+ public void testSimpleTerminated()
+ {
+ RegexSet set = new RegexSet();
+ set.add("^foo.*$");
+
+ Assert.assertEquals(true,set.contains("^foo.*$"));
+ Assert.assertEquals(true,set.matches("foo"));
+ Assert.assertEquals(true,set.matches("foobar"));
+ Assert.assertEquals(false,set.matches("bar"));
+ Assert.assertEquals(false,set.matches(""));
+ }
+
+ @Test
+ public void testCombined()
+ {
+ RegexSet set = new RegexSet();
+ set.add("^foo.*$");
+ set.add("bar");
+ set.add("[a-z][0-9][a-z][0-9]");
+
+ Assert.assertEquals(true,set.contains("^foo.*$"));
+ Assert.assertEquals(true,set.matches("foo"));
+ Assert.assertEquals(true,set.matches("foobar"));
+ Assert.assertEquals(true,set.matches("bar"));
+ Assert.assertEquals(true,set.matches("c3p0"));
+ Assert.assertEquals(true,set.matches("r2d2"));
+
+ Assert.assertEquals(false,set.matches("wibble"));
+ Assert.assertEquals(false,set.matches("barfoo"));
+ Assert.assertEquals(false,set.matches("2b!b"));
+ Assert.assertEquals(false,set.matches(""));
+ }
+}

Back to the top