Skip to content

Commit

Permalink
fix: Update URI pattern matching to use AntPathMatcher - #6
Browse files Browse the repository at this point in the history
- Fixed an issue where URI paths like `/foo` were not matching with patterns like `/`.
- The previous implementation using UriTemplate did not handle URI prefixes correctly.
- Replaced UriTemplate with AntPathMatcher to properly match URI prefixes and paths.
  • Loading branch information
Dh3356 committed Dec 2, 2024
1 parent 75ace10 commit 3cace7e
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
import java.util.Objects;
import java.util.Set;
import org.springframework.http.HttpMethod;
import org.springframework.web.util.UriTemplate;
import org.springframework.util.AntPathMatcher;

/**
* Pattern for matching restful requests.
* <p> This class is a part of the restful-interceptor module.
* <p> This class is used to match the request URI and HTTP method.
* <p> This class is used by {@link RestInterceptor}.
*
* @author Dh3356
* @since 0.1
*/
public class RestfulPattern {

private final UriTemplate path;
private final String path;
private final Set<HttpMethod> methods;
private final AntPathMatcher pathMatcher = new AntPathMatcher();

private RestfulPattern(final UriTemplate path, final Set<HttpMethod> methods) {
private RestfulPattern(final String path, final Set<HttpMethod> methods) {
this.path = path;
this.methods = methods;
}
Expand All @@ -28,14 +32,14 @@ private RestfulPattern(final UriTemplate path, final Set<HttpMethod> methods) {
* Create a new instance of {@link RestfulPattern} with the given path and HTTP method Collections.
*/
public static RestfulPattern of(final String path, final Collection<HttpMethod> methods) {
return new RestfulPattern(new UriTemplate(path), new HashSet<>(methods));
return new RestfulPattern(path, new HashSet<>(methods));
}

/**
* Create a new instance of {@link RestfulPattern} with the given path and HTTP method.
*/
public static RestfulPattern of(final String path, final HttpMethod method) {
return new RestfulPattern(new UriTemplate(path), Set.of(method));
return new RestfulPattern(path, Set.of(method));
}

public static RestfulPatternBuilder builder() {
Expand All @@ -47,11 +51,12 @@ public static RestfulPatternBuilder builder() {
* <p> If the request URI and HTTP method match, return true.
*/
public boolean matches(final HttpServletRequest request) {
return methods.contains(HttpMethod.valueOf(request.getMethod())) && path.matches(request.getRequestURI());
return methods.contains(HttpMethod.valueOf(request.getMethod())) &&
pathMatcher.match(path, request.getRequestURI());
}

public String getPath() {
return path.toString();
return path;
}

@Override
Expand Down Expand Up @@ -80,7 +85,7 @@ public int hashCode() {
public String toString() {
return "RestfulPattern{" +
"methods=" + methods +
", path=" + path.toString() +
", path=" + path +
'}';
}

Expand All @@ -91,16 +96,14 @@ public String toString() {
*/
public static class RestfulPatternBuilder {

private final Set<HttpMethod> methods;
private UriTemplate path;
private final Set<HttpMethod> methods = new HashSet<>();
private String path = "/**";

public RestfulPatternBuilder() {
this.path = new UriTemplate("/**");
this.methods = new HashSet<>();
}

public RestfulPatternBuilder path(String path) {
this.path = new UriTemplate(path);
this.path = path;
return this;
}

Expand Down

0 comments on commit 3cace7e

Please sign in to comment.