Skip to content

Commit

Permalink
rest: match paths using regexp
Browse files Browse the repository at this point in the history
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
mgenov committed Jan 21, 2016
1 parent 8682029 commit 9b76977
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sitebricks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<classifier>jdk15</classifier>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}},},
};
}

Expand Down

0 comments on commit 9b76977

Please sign in to comment.