001/* 002 * Copyright 2022-2026 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 org.jspecify.annotations.NonNull; 020 021import javax.annotation.concurrent.ThreadSafe; 022import java.lang.reflect.Method; 023import java.util.Optional; 024import java.util.Set; 025 026import static java.util.Objects.requireNonNull; 027 028/** 029 * Contract for matching incoming HTTP requests with appropriate <em>Resource Methods</em> (Java methods to invoke to handle requests). 030 * <p> 031 * A <em>Resource Method</em> is a Java {@link Method} with an HTTP method annotation applied, e.g. {@link com.soklet.annotation.GET}, {@link com.soklet.annotation.POST}, {@link com.soklet.annotation.SseEventSource}, ... 032 * <p> 033 * Standard threadsafe implementations can be acquired via these factory methods: 034 * <ul> 035 * <li>{@link #fromClasspathIntrospection()} (examines an annotation-processor-generated lookup table of Java {@link Method} declarations with corresponding HTTP method annotations)</li> 036 * <li>{@link #fromClasses(Set)} (examines methods within the hardcoded set of classes)</li> 037 * <li>{@link #fromMethods(Set)} (examines the hardcoded set of methods)</li> 038 * </ul> 039 * <p> 040 * The built-in resolvers prefer declarations without a varargs placeholder. 041 * Competing varargs declarations are compared from left to right: a literal 042 * component outranks a single-component placeholder, which outranks the 043 * varargs suffix. Thus {@code /widgets/{id}/{rest*}} outranks {@code /{path*}}. 044 * Fixed declarations prefer fewer placeholders, then more literals. Equally 045 * specific overlapping declarations are rejected instead of choosing a handler 046 * by registration order. HTTP methods and HTTP/SSE routes are ranked separately. 047 * The annotation processor applies the same ambiguity rule. 048 * <p> 049 * It is likely that one or more of the above implementations is sufficient for your application and test suite. 050 * <p> 051 * However, should a custom implementation be necessary, documentation is available at <a href="https://www.soklet.com/docs/request-handling#resource-method-resolution">https://www.soklet.com/docs/request-handling#resource-method-resolution</a>. 052 * 053 * @author <a href="https://www.revetkn.com">Mark Allen</a> 054 */ 055@ThreadSafe 056public interface ResourceMethodResolver { 057 /** 058 * Given an HTTP request, provide a matching <em>Resource Method</em> to invoke. 059 * <p> 060 * An unmatched <em>Resource Method</em> generally indicates an {@code HTTP 404}. 061 * When handling {@code HEAD}, Soklet first resolves that method. If unmatched, 062 * it resolves a {@code GET} copy before request-routing observation and interception. 063 * The selected GET method is reported as the resource method; the application 064 * still receives the original {@code HEAD} method and no response body is delivered. 065 * 066 * @param request the HTTP request 067 * @param serverType the type of server that's handling the request 068 * @return the matching <em>Resource Method</em>, or {@link Optional#empty()} if no match was found 069 */ 070 @NonNull 071 Optional<@NonNull ResourceMethod> resourceMethodForRequest(@NonNull Request request, 072 @NonNull ServerType serverType); 073 074 /** 075 * Vends the set of all <em>Resource Methods</em> registered in the system. 076 * 077 * @return the set of all <em>Resource Methods</em> in the system 078 */ 079 @NonNull 080 Set<@NonNull ResourceMethod> getResourceMethods(); 081 082 /** 083 * Acquires a threadsafe {@link ResourceMethodResolver} implementation which locates <em>Resource Methods</em> by examining a lookup table of Java {@link Method} declarations that are annotated with {@link com.soklet.annotation.GET}, {@link com.soklet.annotation.POST}, {@link com.soklet.annotation.SseEventSource}, etc. 084 * <p> 085 * This implementation requires that your application be compiled with the {@link SokletProcessor} annotation processor, as shown below: 086 * <p> 087 * <pre>javac -parameters -processor com.soklet.SokletProcessor ...[rest of javac command elided]</pre> 088 * <p> 089 * The returned instance is a singleton for the class loader that defines Soklet. 090 * Its first resource-method resolution uses that thread's context class loader 091 * and permanently caches the resulting route table or failure. Later calls do 092 * not discover routes again when the context class loader changes. 093 * <p> 094 * Applications that share Soklet from a parent class loader, use separate 095 * application class loaders, or support hot reload should configure an explicit 096 * {@link #fromClasses(Set)} or {@link #fromMethods(Set)} resolver for each application. 097 * 098 * @return a {@code ResourceMethodResolver} which performs classpath introspection against the annotation processor's lookup table 099 */ 100 @NonNull 101 static ResourceMethodResolver fromClasspathIntrospection() { 102 return DefaultResourceMethodResolver.fromClasspathIntrospection(); 103 } 104 105 /** 106 * Acquires a threadsafe {@link ResourceMethodResolver} implementation which locates <em>Resource Methods</em> by examining the provided {@code classes}. 107 * <p> 108 * This method is guaranteed to return a new instance. 109 * 110 * @param classes the classes to inspect for <em>Resource Methods</em> 111 * @return a {@code ResourceMethodResolver} backed by the given {@code classes} 112 */ 113 @NonNull 114static ResourceMethodResolver fromClasses(@NonNull Set<@NonNull Class<?>> classes) { 115 requireNonNull(classes); 116 return DefaultResourceMethodResolver.fromClasses(classes); 117} 118 119 /** 120 * Acquires a threadsafe {@link ResourceMethodResolver} implementation which locates <em>Resource Methods</em> by examining the provided {@code methods}. 121 * <p> 122 * This method is guaranteed to return a new instance. 123 * 124 * @param methods the methods to inspect for <em>Resource Method</em> annotations 125 * @return a {@code ResourceMethodResolver} backed by the given {@code methods} 126 */ 127 @NonNull 128static ResourceMethodResolver fromMethods(@NonNull Set<@NonNull Method> methods) { 129 requireNonNull(methods); 130 return DefaultResourceMethodResolver.fromMethods(methods); 131} 132}