@Override public boolean matches(final NormalisedPath requestPath) { if (this.numberOfParts() != requestPath.numberOfParts()) { return false; } for (int i = 0; i < this.numberOfParts(); i++) { if (!this.partMatches(i, requestPath.part(i))) { return false; } } return true; }
@Override public boolean hasParams(final int index) { final String part = part(index); return PARAM_PATTERN.matcher(part).find(); }
@Override public Map<String, Optional<String>> paramValues(final int index, final String requestPathPart) { final List<String> paramNames = paramNames(index); if (paramNames.isEmpty()) { return emptyMap(); final String template = part(index);
/** * A utility for finding the best fitting API path. * * @param api the OpenAPI definition * @param basePathOverride (Optional) override for the base path defined in the OpenAPI specification. */ public ApiOperationResolver(final OpenAPI api, @Nullable final String basePathOverride) { apiPrefix = ofNullable(basePathOverride).orElse(getBasePathFrom(api.getServers())); final Paths apiPaths = ofNullable(api.getPaths()).orElse(new Paths()); // normalise all API paths and group them by their number of parts apiPathsGroupedByNumberOfParts = apiPaths.keySet().stream() .map(p -> new ApiPathImpl(p, apiPrefix)) .collect(groupingBy(NormalisedPath::numberOfParts)); // create a operation mapping for the API path and HTTP method operations = HashBasedTable.create(); apiPaths.forEach((pathKey, apiPath) -> apiPath.readOperationsMap().forEach((httpMethod, operation) -> operations.put(pathKey, httpMethod, operation)) ); }
@Override public boolean partMatches(final int index, @Nonnull final String requestPathPart) { requireNonNull(requestPathPart, "A request path part is required"); final String template = part(index); final Pattern templatePattern = compile(quote(template).replaceAll(PARAM_REGEX, "\\\\E(.*?)\\\\Q"), CASE_INSENSITIVE); return templatePattern.matcher(requestPathPart).matches(); }
@Override public List<String> paramNames(final int index) { final String part = part(index); final Matcher matcher = PARAM_PATTERN.matcher(part); final List<String> result = new ArrayList<>(); while (matcher.find()) { result.add(matcher.group(1)); } return result; }