All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.muserver.rest.PathMatch Maven / Gradle / Ivy

The newest version!
package io.muserver.rest;

import jakarta.ws.rs.core.PathSegment;

import java.util.Collections;
import java.util.HashMap;
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 {

    /**
     * An empty match
     */
    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 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() {
        Map copy = new HashMap<>(params.size());
        for (Map.Entry entry : params.entrySet()) {
            copy.put(entry.getKey(), entry.getValue().getPath());
        }
        return copy;
    }
    /**
     * Returns a mapping of URI names to path segments. For example the template URI is /fruit/{name}
     * then pathMatch.params().get("name").getPath() will return orange if the URI was /fruit/orange.
     * 

The segments also contain matrix parameter info.

* @return Returns a read-only map of path parameters names to values. */ public Map segments() { 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 + '}'; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy