001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.util; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.*; 020import org.apache.juneau.collections.*; 021import org.apache.juneau.internal.*; 022import org.apache.juneau.marshall.*; 023 024/** 025 * Represents a URL path pattern match. 026 * 027 * For example, given the pattern <js>"/foo/{bar}/*"</js> and the path <js>"/foo/123/baz/qux"</js>, this match gives 028 * you a map containing <js>"{bar:123}"</js> and a remainder string containing <js>"baz/qux"</js>. 029 */ 030public class UrlPathPatternMatch { 031 032 private final int matchedParts; 033 private final String path; 034 private final Map<String,String> vars; 035 036 /** 037 * Constructor. 038 * 039 * @param path The path being matched against. Can be <jk>null</jk>. 040 * @param matchedParts The number of parts that were matched against the path. 041 * @param keys The variable keys. Can be <jk>null</jk>. 042 * @param values The variable values. Can be <jk>null</jk>. 043 */ 044 protected UrlPathPatternMatch(String path, int matchedParts, String[] keys, String[] values) { 045 this.path = path; 046 this.matchedParts = matchedParts; 047 this.vars = keys == null ? Collections.emptyMap() : new SimpleMap<>(keys, values); 048 } 049 050 /** 051 * Returns a map of the path variables and values. 052 * 053 * @return 054 * An unmodifiable map of variable keys/values. 055 * <br>Returns an empty map if no variables were found in the path. 056 */ 057 public Map<String,String> getVars() { 058 return vars; 059 } 060 061 /** 062 * Returns <jk>true</jk> if this match contains one or more variables. 063 * 064 * @return <jk>true</jk> if this match contains one or more variables. 065 */ 066 public boolean hasVars() { 067 return ! vars.isEmpty(); 068 } 069 070 071 /** 072 * Returns <jk>true</jk> if any of the variable values are blank. 073 * 074 * @return <jk>true</jk> if any of the variable values are blank. 075 */ 076 public boolean hasEmptyVars() { 077 for (String v : vars.values()) 078 if (isEmpty(v)) 079 return true; 080 return false; 081 } 082 083 /** 084 * Returns the remainder of the path after the pattern match has been made. 085 * 086 * @return The remainder of the path after the pattern match has been made. 087 */ 088 public String getRemainder() { 089 String suffix = getSuffix(); 090 if (isNotEmpty(suffix) && suffix.charAt(0) == '/') 091 suffix = suffix.substring(1); 092 return suffix; 093 } 094 095 /** 096 * Returns the remainder of the URL after the pattern was matched. 097 * 098 * @return 099 * The remainder of the URL after the pattern was matched. 100 * <br>Can be <jk>null</jk> if nothing remains to be matched. 101 * <br>Otherwise, always starts with <js>'/'</js>. 102 */ 103 public String getSuffix() { 104 String s = path; 105 for (int j = 0; j < matchedParts; j++) { 106 int k = s.indexOf('/', 1); 107 if (k == -1) 108 return null; 109 s = s.substring(k); 110 } 111 return s; 112 } 113 114 /** 115 * Returns the part of the URL that the pattern matched against. 116 * 117 * @return 118 * The part of the URL that the pattern matched against. 119 * <br>Can be <jk>null</jk> if nothing matched. 120 * <br>Otherwise, always starts with <js>'/'</js>. 121 */ 122 public String getPrefix() { 123 int c = 0; 124 for (int j = 0; j < matchedParts; j++) { 125 c = path.indexOf('/', c+1); 126 if (c == -1) 127 c = path.length(); 128 } 129 return nullIfEmpty(path.substring(0, c)); 130 } 131 132 /** 133 * Converts this object to a map. 134 * 135 * @return This object converted to a map. 136 */ 137 public OMap toMap() { 138 return new DefaultFilteringOMap().append("v", getVars()).append("r", getRemainder()); 139 } 140 141 @Override /* Object */ 142 public String toString() { 143 return SimpleJson.DEFAULT.toString(toMap()); 144 } 145} 146