io.muserver.rest.PathMatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mu-server Show documentation
Show all versions of mu-server Show documentation
A simple but powerful web server framework
package io.muserver.rest;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The result of matching a template URI against a real request URI. If there is a match, then any path parameters
* are available in the {@link #params()} map.
*/
public class PathMatch {
public static final PathMatch EMPTY_MATCH = new PathMatch(true, Collections.emptyMap(), Pattern.compile("").matcher(""));
private final boolean matches;
private final Map params;
private final Matcher matcher;
PathMatch(boolean matches, Map params, Matcher matcher) {
this.matches = matches;
this.params = params;
this.matcher = matcher;
}
/**
* @return Returns true if the beginning of the checked URI matches this pattern. For example
* if the pattern is /abc
then this will return true
for /abc
* and /abc/def
etc.
*/
public boolean prefixMatches() {
return matches;
}
/**
* @return Returns true if the checked URI matches this pattern. For example
* if the pattern is /abc
then this will return true
for /abc
* but false for /abc/def
etc.
*/
public boolean fullyMatches() {
return matches && lastGroup() == null;
}
/**
* Returns a mapping of URI names to path params. For example the the template URI is /fruit/{name}
* then pathMatch.params().get("name")
will return orange
if the URI was /fruit/orange
* @return Returns a read-only map of path parameters names to values.
*/
public Map params() {
return params;
}
/**
* @return Returns the regex Matcher that was used
*/
public Matcher regexMatcher() {
return matcher;
}
/**
* @return Returns the last captured group value, which may be null
*/
String lastGroup() {
String group = matcher.group(matcher.groupCount());
return "/".equals(group) ? null : group;
}
@Override
public String toString() {
return "PathMatch{" +
"matches=" + matches +
", params=" + params +
'}';
}
}