001/* 002 * Copyright 2022-2025 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import com.soklet.ResourcePathDeclaration.Component; 020import com.soklet.ResourcePathDeclaration.ComponentType; 021 022import javax.annotation.Nonnull; 023import javax.annotation.Nullable; 024import javax.annotation.concurrent.ThreadSafe; 025import java.util.Arrays; 026import java.util.Collections; 027import java.util.LinkedHashMap; 028import java.util.List; 029import java.util.Map; 030import java.util.Objects; 031 032import static java.lang.String.format; 033import static java.util.Collections.emptyList; 034import static java.util.Collections.unmodifiableList; 035import static java.util.Objects.requireNonNull; 036 037/** 038 * An HTTP URL path used to resolve a <em>Resource Method</em> at runtime, such as {@code /users/123}. 039 * <p> 040 * You may obtain instances via the {@link #withPath(String)} factory method. 041 * <p> 042 * <strong>Note: this type is not normally used by Soklet applications unless they support <a href="https://www.soklet.com/docs/server-sent-events">Server-Sent Events</a> or choose to implement a custom {@link ResourceMethodResolver}.</strong> 043 * <p> 044 * The corresponding compile-time type for {@link ResourcePath} is {@link ResourcePathDeclaration} and functionality is provided to check if the two "match" via {@link #matches(ResourcePathDeclaration)}. 045 * <p> 046 * For example, a {@link ResourcePath} {@code /users/123} would match {@link ResourcePathDeclaration} {@code /users/{userId}}. 047 * 048 * @author <a href="https://www.revetkn.com">Mark Allen</a> 049 */ 050@ThreadSafe 051public final class ResourcePath { 052 @Nonnull 053 final static ResourcePath OPTIONS_SPLAT_RESOURCE_PATH; 054 055 static { 056 OPTIONS_SPLAT_RESOURCE_PATH = new ResourcePath(); 057 } 058 059 @Nonnull 060 private final String path; 061 @Nonnull 062 private final List<String> components; 063 064 /** 065 * Vends an instance that represents a runtime representation of a resource path, for example {@code /users/123}. 066 * <p> 067 * This is in contrast to {@link ResourcePathDeclaration}, which represents compile-time path declarations 068 * that may include placeholders, e.g. {@code /users/{userId}}. 069 * 070 * @param path a runtime path (no placeholders) e.g. {@code /users/123} 071 */ 072 @Nonnull 073 public static ResourcePath withPath(@Nonnull String path) { 074 requireNonNull(path); 075 return new ResourcePath(path); 076 } 077 078 // Special "options splat" path 079 private ResourcePath() { 080 this.path = "*"; 081 this.components = List.of(); 082 } 083 084 private ResourcePath(@Nonnull String path) { 085 requireNonNull(path); 086 this.path = ResourcePathDeclaration.normalizePath(path); 087 this.components = unmodifiableList(extractComponents(this.path)); 088 } 089 090 /** 091 * Does this resource path match the given resource path (taking placeholders/varargs into account, if present)? 092 * <p> 093 * For example, resource path {@code /users/123} would match the resource path declaration {@code /users/{userId}}. 094 * 095 * @param resourcePathDeclaration the compile-time declaration to match against 096 * @return {@code true} if this resource path matches, {@code false} otherwise 097 */ 098 @Nonnull 099 public Boolean matches(@Nonnull ResourcePathDeclaration resourcePathDeclaration) { 100 requireNonNull(resourcePathDeclaration); 101 102 if (this == OPTIONS_SPLAT_RESOURCE_PATH) 103 return false; 104 105 List<Component> declarationComponents = resourcePathDeclaration.getComponents(); 106 107 if (!declarationComponents.isEmpty() && declarationComponents.get(declarationComponents.size() - 1).getType() == ComponentType.VARARGS) { 108 if (getComponents().size() < declarationComponents.size() - 1) 109 return false; 110 111 // Check prefix 112 for (int i = 0; i < declarationComponents.size() - 1; i++) { 113 Component comp = declarationComponents.get(i); 114 String pathComp = getComponents().get(i); 115 116 if (comp.getType() == ComponentType.LITERAL && !comp.getValue().equals(pathComp)) 117 return false; 118 } 119 120 return true; 121 } else { 122 if (getComponents().size() != declarationComponents.size()) 123 return false; 124 125 for (int i = 0; i < declarationComponents.size(); i++) { 126 Component comp = declarationComponents.get(i); 127 String pathComp = getComponents().get(i); 128 129 if (comp.getType() == ComponentType.LITERAL && !comp.getValue().equals(pathComp)) 130 return false; 131 } 132 133 return true; 134 } 135 } 136 137 /** 138 * What is the mapping between this resource path's placeholder values to the given resource path declaration's placeholder names? 139 * <p> 140 * For example, placeholder extraction for resource path {@code /users/123} and resource path declaration {@code /users/{userId}} 141 * would result in a value equivalent to {@code Map.of("userId", "123")}. 142 * <p> 143 * Resource path placeholder values are automatically URL-decoded. For example, placeholder extraction for resource path declaration {@code /users/{userId}} 144 * and resource path {@code /users/ab%20c} would result in a value equivalent to {@code Map.of("userId", "ab c")}. 145 * <p> 146 * For varargs placeholders, the extra path components are joined with '/'. 147 * 148 * @param resourcePathDeclaration compile-time resource path, used to provide placeholder names 149 * @return a mapping of placeholder names to values, or the empty map if there were no placeholders 150 * @throws IllegalArgumentException if the provided resource path declaration does not match this resource path, i.e. {@link #matches(ResourcePathDeclaration)} is {@code false} 151 */ 152 @Nonnull 153 public Map<String, String> extractPlaceholders(@Nonnull ResourcePathDeclaration resourcePathDeclaration) { 154 requireNonNull(resourcePathDeclaration); 155 156 if (!matches(resourcePathDeclaration)) 157 throw new IllegalArgumentException(format("%s is not a match for %s so we cannot extract placeholders", this, resourcePathDeclaration)); 158 159 Map<String, String> placeholders = new LinkedHashMap<>(); 160 List<Component> declarationComponents = resourcePathDeclaration.getComponents(); 161 162 if (!declarationComponents.isEmpty() && declarationComponents.get(declarationComponents.size() - 1).getType() == ComponentType.VARARGS) { 163 for (int i = 0; i < declarationComponents.size() - 1; i++) { 164 Component comp = declarationComponents.get(i); 165 166 if (comp.getType() == ComponentType.PLACEHOLDER) 167 placeholders.put(comp.getValue(), getComponents().get(i)); 168 } 169 170 // Join remaining components for varargs placeholder. 171 String varargsValue = String.join("/", getComponents().subList(declarationComponents.size() - 1, getComponents().size())); 172 placeholders.put(declarationComponents.get(declarationComponents.size() - 1).getValue(), varargsValue); 173 } else { 174 for (int i = 0; i < declarationComponents.size(); i++) { 175 Component comp = declarationComponents.get(i); 176 177 if (comp.getType() == ComponentType.PLACEHOLDER) 178 placeholders.put(comp.getValue(), getComponents().get(i)); 179 } 180 } 181 182 return Collections.unmodifiableMap(placeholders); 183 } 184 185 /** 186 * What is the string representation of this resource path? 187 * 188 * @return the string representation of this resource path, which must start with {@code /} 189 */ 190 @Nonnull 191 public String getPath() { 192 return this.path; 193 } 194 195 /** 196 * What are the {@code /}-delimited components of this resource path? 197 * 198 * @return the components, or the empty list if this path is equal to {@code /} 199 */ 200 @Nonnull 201 public List<String> getComponents() { 202 return this.components; 203 } 204 205 /** 206 * Assumes {@code path} is already normalized via {@link ResourcePathDeclaration#normalizePath(String)}. 207 * 208 * @param path (nonnull) Path from which components are extracted 209 * @return Logical components of the supplied {@code path} 210 */ 211 @Nonnull 212 protected List<String> extractComponents(@Nonnull String path) { 213 requireNonNull(path); 214 215 if ("/".equals(path)) 216 return emptyList(); 217 218 // Strip off leading / 219 path = path.substring(1); 220 return Arrays.asList(path.split("/")); 221 } 222 223 @Override 224 public String toString() { 225 return format("%s{path=%s, components=%s}", getClass().getSimpleName(), getPath(), getComponents()); 226 } 227 228 @Override 229 public boolean equals(@Nullable Object object) { 230 if (this == object) 231 return true; 232 233 if (!(object instanceof ResourcePath resourcePath)) 234 return false; 235 236 return Objects.equals(getPath(), resourcePath.getPath()); 237 } 238 239 @Override 240 public int hashCode() { 241 return Objects.hash(getPath()); 242 } 243}