001/*
002 * Copyright 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 */
016package com.revetsec.soklet;
017
018import com.google.errorprone.annotations.CheckReturnValue;
019import com.revetsec.jose.JwksPublisher;
020import com.soklet.MarshaledResponse;
021import com.soklet.Request;
022import org.jspecify.annotations.NonNull;
023
024import javax.annotation.concurrent.ThreadSafe;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * Publishes standalone public JSON Web Key Sets as prepared Soklet responses. The core publisher's exact bytes,
030 * strong entity tag and prepared content length are retained. Soklet's default marshaler supports this variant,
031 * including HEAD. Applications choose the route and its keys; no OAuth authorization server is required.
032 * For a typed body and application-owned JSON serialization, use {@link JwksResponse} instead.
033 *
034 * @since 1.0.0
035 */
036@ThreadSafe
037public final class JwksMarshaledResponse {
038
039        private JwksMarshaledResponse() {}
040
041        /**
042         * Prepares a standalone JWKS response using every {@code If-None-Match} occurrence.
043         * HEAD responses are bodyless and retain the core publisher's GET representation length.
044         * @param publisher configured public-key publisher
045         * @param request request on the application's JWKS route
046         * @return prepared status, headers and exact body bytes in a Soklet response
047         * @throws NullPointerException if a required argument is null
048         * @since 1.0.0
049         */
050        @CheckReturnValue
051        public static @NonNull MarshaledResponse from(@NonNull JwksPublisher publisher, @NonNull Request request) {
052                requireNonNull(publisher);
053                requireNonNull(request);
054                return from(publisher.responseFor(request.getHttpMethod().name(), JwksRequestHeaders.ifNoneMatchFor(request)));
055        }
056
057        /**
058         * Adapts a prepared core response without JSON marshalling or header regeneration.
059         * A prepared HEAD or not-modified response has no body; its headers are retained unchanged.
060         *
061         * @param response prepared standalone JWKS response
062         * @return exact-byte Soklet response
063         * @throws NullPointerException if response is null
064         * @since 1.0.0
065         */
066        @CheckReturnValue
067        public static @NonNull MarshaledResponse from(com.revetsec.jose.@NonNull JwksResponse response) {
068                requireNonNull(response);
069                MarshaledResponse.Builder builder = MarshaledResponse.withStatusCode(response.getStatusCode()).headers(response.getHeaders());
070                byte[] body = response.toHttpBody();
071                if (body.length != 0)
072                        builder.body(body);
073                return builder.build();
074        }
075}