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

org.apache.juneau.rest.util.UrlPathMatch Maven / Gradle / Ivy

// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.rest.util;

import static org.apache.juneau.collections.JsonMap.*;
import static org.apache.juneau.common.internal.StringUtils.*;

import java.util.*;

import org.apache.juneau.internal.*;

/**
 * Represents a URL path pattern match.
 *
 * For example, given the pattern "/foo/{bar}/*" and the path "/foo/123/baz/qux", this match gives
 * you a map containing "{bar:123}" and a remainder string containing "baz/qux".
 *
 * 
See Also:
    *
*/ public class UrlPathMatch { private final int matchedParts; private final String path; private final Map vars; /** * Constructor. * * @param path The path being matched against. Can be null. * @param matchedParts The number of parts that were matched against the path. * @param keys The variable keys. Can be null. * @param values The variable values. Can be null. */ protected UrlPathMatch(String path, int matchedParts, String[] keys, String[] values) { this.path = path; this.matchedParts = matchedParts; this.vars = keys == null ? Collections.emptyMap() : new SimpleMap<>(keys, values); } /** * Returns a map of the path variables and values. * * @return * An unmodifiable map of variable keys/values. *
Returns an empty map if no variables were found in the path. */ public Map getVars() { return vars; } /** * Returns true if this match contains one or more variables. * * @return true if this match contains one or more variables. */ public boolean hasVars() { return ! vars.isEmpty(); } /** * Returns true if any of the variable values are blank. * * @return true if any of the variable values are blank. */ public boolean hasEmptyVars() { for (String v : vars.values()) if (isEmpty(v)) return true; return false; } /** * Returns the remainder of the path after the pattern match has been made. * *

* Same as {#link {@link #getSuffix()} but trims the leading slash if there is one. * * @return The remainder of the path after the pattern match has been made. */ public String getRemainder() { String suffix = getSuffix(); if (isNotEmpty(suffix) && suffix.charAt(0) == '/') suffix = suffix.substring(1); return suffix; } /** * Returns the remainder of the URL after the pattern was matched. * * @return * The remainder of the URL after the pattern was matched. *
Can be null if nothing remains to be matched. *
Otherwise, always starts with '/'. */ public String getSuffix() { String s = path; for (int j = 0; j < matchedParts; j++) { int k = s.indexOf('/', 1); if (k == -1) return null; s = s.substring(k); } return s; } /** * Returns the part of the URL that the pattern matched against. * * @return * The part of the URL that the pattern matched against. *
Can be null if nothing matched. *
Otherwise, always starts with '/'. */ public String getPrefix() { int c = 0; for (int j = 0; j < matchedParts; j++) { c = path.indexOf('/', c+1); if (c == -1) c = path.length(); } return nullIfEmpty(path.substring(0, c)); } @Override /* Object */ public String toString() { return filteredMap() .append("v", getVars()) .append("r", getRemainder()) .asString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy