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 java.util.Map;
022import java.util.Set;
023
024/**
025 * Contract for parsing HTML form fields encoded according to the <a href="https://datatracker.ietf.org/doc/html/rfc7578">{@code multipart/form-data}</a> specification.
026 * <p>
027 * A standard threadsafe implementation can be acquired via the {@link #defaultInstance()} factory method.
028 * <p>
029 * See <a href="https://www.soklet.com/docs/request-handling#multipart-form-data">https://www.soklet.com/docs/request-handling#multipart-form-data</a> for detailed documentation.
030 *
031 * @author <a href="https://www.revetkn.com">Mark Allen</a>
032 */
033@FunctionalInterface
034public interface MultipartParser {
035        /**
036         * Given a request, detect all HTML form fields with <a href="https://datatracker.ietf.org/doc/html/rfc7578">{@code multipart/form-data}</a> encoding and parse their values.
037         *
038         * @param request the request to parse
039         * @return a mapping of form field names to corresponding sets of form field values
040         */
041        @NonNull
042        Map<@NonNull String, @NonNull Set<@NonNull MultipartField>> extractMultipartFields(@NonNull Request request);
043
044        /**
045         * Acquires a threadsafe {@link MultipartParser}.
046         * <p>
047         * The returned instance is guaranteed to be a JVM-wide singleton.
048         *
049         * @return a {@code MultipartParser} instance
050         */
051        @NonNull
052        static MultipartParser defaultInstance() {
053                return DefaultMultipartParser.defaultInstance();
054        }
055}