Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest: match paths using regexp #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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<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