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.nio.ByteBuffer;
023
024/**
025 * A response-compression implementation and its HTTP content coding.
026 * <p>
027 * Soklet supplies {@link #gzipInstance()} using the JDK's gzip implementation without runtime
028 * dependencies. Applications can supply other codecs, including codecs backed by optional external
029 * libraries, without adding those libraries to Soklet itself. A codec is selected by a
030 * {@link ResponseCompressionPlan}; it does not decide request eligibility, negotiate with a client,
031 * or write HTTP headers.
032 * <p>
033 * Implementations must be thread-safe. Soklet can reuse one codec concurrently for many responses,
034 * and does not synchronize access to it.
035 *
036 * @author <a href="https://www.revetkn.com">Mark Allen</a>
037 */
038@ThreadSafe
039public interface ResponseCompressionCodec {
040        /**
041         * Acquires the shared built-in {@code gzip} codec.
042         * <p>
043         * The codec uses the JDK's default gzip compression settings and does not cache results.
044         *
045         * @return a thread-safe gzip codec
046         */
047        @NonNull
048        static ResponseCompressionCodec gzipInstance() {
049                return DefaultGzipResponseCompressionCodec.defaultInstance();
050        }
051
052        /**
053         * The single HTTP content coding produced by this codec, such as {@code gzip} or {@code br}.
054         * <p>
055         * Return a nonempty ASCII HTTP token without whitespace, parameters, or a coding chain. The reserved
056         * values {@code identity} and {@code *} are not compression codecs. A plan validates this value and
057         * snapshots its lowercase form at construction; the codec must continue to produce that coding for
058         * the lifetime of every plan using it.
059         *
060         * @return the non-null content coding
061         */
062        @NonNull
063        String getContentEncoding();
064
065        /**
066         * Compresses the bytes between the supplied buffer's position and limit.
067         * <p>
068         * Soklet supplies a read-only buffer view confined to this invocation. An implementation may advance
069         * that view's position but must not retain the view, access it after returning, or mutate the underlying
070         * response bytes. Compression must complete synchronously on the invoking request-handling thread.
071         * <p>
072         * Return a non-null complete encoding of exactly the supplied bytes, using this codec's content coding.
073         * The returned array is not defensively copied. Neither the codec nor any caller may mutate it while a
074         * response is writing it or a cache retains it for reuse. A null result or an exception is a
075         * response-processing failure; Soklet does not silently send partially encoded or uncompressed bytes.
076         *
077         * @param uncompressedBody the read-only view of the finalized uncompressed body
078         * @return the non-null compressed bytes
079         */
080        byte @NonNull [] compress(@NonNull ByteBuffer uncompressedBody);
081}