-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds regex pattern matching on paths, so path variables could be more complex that are now. Here are some examples ``` /r/users/:id{\d+} ``` will match only ``` /r/users/123 ``` but not ``` /r/users/john ```
- Loading branch information
Showing
3 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ([email protected]) | ||
|
@@ -23,7 +28,15 @@ private static List<PathMatcher> toMatchChain(String path) { | |
|
||
List<PathMatcher> matchers = new ArrayList<PathMatcher>(); | ||
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,33 @@ 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<String, String> findMatches(String incoming) { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
|
||
//matches nothing, i.e. always returns false (used for blocking sitebricks) | ||
@Immutable | ||
static class IgnoringPathMatcher implements PathMatcher { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters