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;
020import org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.lang.reflect.Method;
024import java.util.Objects;
025
026import static java.lang.String.format;
027import static java.util.Objects.requireNonNull;
028
029/**
030 * Represents a <em>Resource Method</em>, which is a Java {@link Method} invoked by Soklet to handle an HTTP request.
031 * <p>
032 * Instances can be acquired via the {@link #fromComponents(HttpMethod, ResourcePathDeclaration, Method, Boolean)} factory method.
033 * <p>
034 * Detailed documentation available at <a href="https://www.soklet.com/docs/request-handling">https://www.soklet.com/docs/request-handling</a>.
035 *
036 * @author <a href="https://www.revetkn.com">Mark Allen</a>
037 */
038@ThreadSafe
039public final class ResourceMethod {
040        @NonNull
041        private final HttpMethod httpMethod;
042        @NonNull
043        private final ResourcePathDeclaration resourcePathDeclaration;
044        @NonNull
045        private final Method method;
046        @NonNull
047        private final Boolean sseEventSource;
048        private volatile boolean bindingsValidated;
049
050        /**
051         * Vends a <em>Resource Method</em> given its unique components.
052         *
053         * @param httpMethod              an HTTP method
054         * @param resourcePathDeclaration an HTTP path which might contain placeholders, e.g. {@code /example/{exampleId}}
055         * @param method                  a Java method to invoke for the combination of HTTP method and resource path
056         * @param sseEventSource   is this <em>Resource Method</em> configured as a Server-Sent Event source?
057         * @return a <em>Resource Method</em> for the supplied components
058         */
059        @NonNull
060public static ResourceMethod fromComponents(@NonNull HttpMethod httpMethod,
061                                                                                                                                                                                        @NonNull ResourcePathDeclaration resourcePathDeclaration,
062                                                                                                                                                                                        @NonNull Method method,
063                                                                                                                                                                                        @NonNull Boolean sseEventSource) {
064                requireNonNull(httpMethod);
065                requireNonNull(resourcePathDeclaration);
066                requireNonNull(method);
067                requireNonNull(sseEventSource);
068
069                return new ResourceMethod(httpMethod, resourcePathDeclaration, method, sseEventSource);
070        }
071
072        private ResourceMethod(@NonNull HttpMethod httpMethod,
073                                                                                                 @NonNull ResourcePathDeclaration resourcePathDeclaration,
074                                                                                                 @NonNull Method method,
075                                                                                                 @NonNull Boolean sseEventSource) {
076                requireNonNull(httpMethod);
077                requireNonNull(resourcePathDeclaration);
078                requireNonNull(method);
079                requireNonNull(sseEventSource);
080
081                this.httpMethod = httpMethod;
082                this.resourcePathDeclaration = resourcePathDeclaration;
083                this.method = method;
084                this.sseEventSource = sseEventSource;
085        }
086
087        @Override
088        @NonNull
089        public String toString() {
090                return format("%s{httpMethod=%s, resourcePathDeclaration=%s, method=%s, sseEventSource=%s}", getClass().getSimpleName(),
091                                getHttpMethod(), getResourcePathDeclaration(), getMethod(), isSseEventSource());
092        }
093
094        @Override
095        public boolean equals(@Nullable Object object) {
096                if (this == object)
097                        return true;
098
099                if (!(object instanceof ResourceMethod resourceMethod))
100                        return false;
101
102                return Objects.equals(getHttpMethod(), resourceMethod.getHttpMethod())
103                                && Objects.equals(getResourcePathDeclaration(), resourceMethod.getResourcePathDeclaration())
104                                && Objects.equals(getMethod(), resourceMethod.getMethod())
105                                && Objects.equals(isSseEventSource(), resourceMethod.isSseEventSource());
106        }
107
108        @Override
109        public int hashCode() {
110                return Objects.hash(getHttpMethod(), getResourcePathDeclaration(), getMethod(), isSseEventSource());
111        }
112
113        boolean hasValidatedBindings() {
114                return this.bindingsValidated;
115        }
116
117        void markBindingsValidated() {
118                this.bindingsValidated = true;
119        }
120
121        /**
122         * Returns the HTTP method for this <em>Resource Method</em>.
123         *
124         * @return the HTTP method
125         */
126        @NonNull
127        public HttpMethod getHttpMethod() {
128                return this.httpMethod;
129        }
130
131        /**
132         * Returns the HTTP path for this <em>Resource Method</em>, which might contain placeholders - for example, {@code /example/{exampleId}}.
133         *
134         * @return the HTTP path
135         */
136        @NonNull
137        public ResourcePathDeclaration getResourcePathDeclaration() {
138                return this.resourcePathDeclaration;
139        }
140
141        /**
142         * Returns the Java method to invoke for the combination of HTTP method and resource path.
143         *
144         * @return the Java method to invoke
145         */
146        @NonNull
147        public Method getMethod() {
148                return this.method;
149        }
150
151        /**
152         * Returns whether this <em>Resource Method</em> functions as a Server-Sent Event Source.
153         *
154         * @return {@code true} if this <em>Resource Method</em> functions as a Server-Sent Event Source, {@code false} otherwise
155         */
156        @NonNull
157        public Boolean isSseEventSource() {
158                return this.sseEventSource;
159        }
160}