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 javax.annotation.Nonnull;
020import java.lang.reflect.Method;
021import java.util.Optional;
022import java.util.Set;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * Contract for matching incoming HTTP requests with appropriate <em>Resource Methods</em> (Java methods to invoke to handle requests).
028 * <p>
029 * Standard implementations can be acquired via these factory methods:
030 * <ul>
031 *   <li>{@link #withDefaults()}</li>
032 *   <li>{@link #withResourceClasses(Set)}</li>
033 *   <li>{@link #withMethods(Set)}</li>
034 * </ul>
035 * <p>
036 * However, should a custom implementation be necessary for your application, 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>.
037 *
038 * @author <a href="https://www.revetkn.com">Mark Allen</a>
039 */
040public interface ResourceMethodResolver {
041        /**
042         * Given an HTTP request, provide a matching <em>Resource Method</em> to invoke.
043         * <p>
044         * An unmatched <em>Resource Method</em> generally indicates an {@code HTTP 404}.
045         *
046         * @param request the HTTP request
047         * @return the matching <em>Resource Method</em>, or {@link Optional#empty()} if no match was found
048         */
049        @Nonnull
050        Optional<ResourceMethod> resourceMethodForRequest(@Nonnull Request request);
051
052        /**
053         * Vends the set of all <em>Resource Methods</em> registered in the system.
054         *
055         * @return the set of all <em>Resource Methods</em> in the system
056         */
057        @Nonnull
058        Set<ResourceMethod> getResourceMethods();
059
060
061        @Nonnull
062        static ResourceMethodResolver withDefaults() {
063                return DefaultResourceMethodResolver.defaultInstance();
064        }
065
066        @Nonnull
067        static ResourceMethodResolver withResourceClasses(@Nonnull Set<Class<?>> resourceClasses) {
068                requireNonNull(resourceClasses);
069                return DefaultResourceMethodResolver.withResourceClasses(resourceClasses);
070        }
071
072        @Nonnull
073        static ResourceMethodResolver withMethods(@Nonnull Set<Method> methods) {
074                requireNonNull(methods);
075                return DefaultResourceMethodResolver.withMethods(methods);
076        }
077}