diff --git a/sitebricks/pom.xml b/sitebricks/pom.xml index 1f14e55c..8020079b 100644 --- a/sitebricks/pom.xml +++ b/sitebricks/pom.xml @@ -14,7 +14,7 @@ org.testng testng - jdk15 + 6.1.1 test diff --git a/sitebricks/src/main/java/com/google/sitebricks/routing/PathMatcherChain.java b/sitebricks/src/main/java/com/google/sitebricks/routing/PathMatcherChain.java index ddf854a9..84f903de 100644 --- a/sitebricks/src/main/java/com/google/sitebricks/routing/PathMatcherChain.java +++ b/sitebricks/src/main/java/com/google/sitebricks/routing/PathMatcherChain.java @@ -3,7 +3,12 @@ import net.jcip.annotations.Immutable; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; /** * @author Dhanji R. Prasanna (dhanji@gmail.com) @@ -23,7 +28,15 @@ private static List toMatchChain(String path) { List matchers = new ArrayList(); for (String piece : pieces) { - matchers.add((piece.startsWith(":")) ? new GreedyPathMatcher(piece) : new SimplePathMatcher(piece)); + if (piece.startsWith(":") && piece.contains("{") && piece.contains("}")) { + String name = piece.substring(0, piece.indexOf("{")); + String pattern = piece.substring(piece.indexOf("{") + 1, piece.lastIndexOf("}")); + matchers.add(new RegExPathMatcher(pattern, name)); + } else if (piece.startsWith(":") && !piece.contains("{")) { + matchers.add(new GreedyPathMatcher(piece)); + } else { + matchers.add(new SimplePathMatcher(piece)); + } } return Collections.unmodifiableList(matchers); @@ -117,6 +130,32 @@ public String name() { } } + @Immutable + static class RegExPathMatcher implements PathMatcher { + private final Pattern pattern; + private final String variable; + + public RegExPathMatcher(String pattern, String piece) { + this.pattern = Pattern.compile(pattern); + this.variable = piece.substring(1); + } + + @Override + public boolean matches(String incoming) { + return pattern.matcher(incoming).matches(); + } + + @Override + public String name() { + return variable; + } + + @Override + public Map findMatches(String incoming) { + return Collections.emptyMap(); + } + } + //matches nothing, i.e. always returns false (used for blocking sitebricks) @Immutable static class IgnoringPathMatcher implements PathMatcher { diff --git a/sitebricks/src/test/java/com/google/sitebricks/routing/PathMatcherTest.java b/sitebricks/src/test/java/com/google/sitebricks/routing/PathMatcherTest.java index 8392be5b..e5239094 100644 --- a/sitebricks/src/test/java/com/google/sitebricks/routing/PathMatcherTest.java +++ b/sitebricks/src/test/java/com/google/sitebricks/routing/PathMatcherTest.java @@ -76,6 +76,12 @@ public Object[][] getVarPathsAndMatches() { put("title", "sokdoasd"); put("id", "aoskpaokda"); }}, }, + {"/wiki/:id{\\d+}", "/wiki/123", new HashMap() {{ + put("id", "123"); + }},}, + {"/wiki/:id{[a-z]+}", "/wiki/hello", new HashMap() {{ + put("id", "hello"); + }},}, }; }