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.file.Path; 023import java.util.Optional; 024 025/** 026 * Resolves a file path to an HTTP {@code Content-Type} value. 027 * <p> 028 * Implementations used with {@link StaticFiles} must be thread-safe; {@link StaticFiles} invokes 029 * resolvers concurrently from request-handling threads. 030 * <p> 031 * {@link StaticFiles}' default resolver uses a curated deterministic extension map for common web 032 * assets. It does not call {@link java.nio.file.Files#probeContentType(Path)}. A custom resolver 033 * fully replaces that default, and {@link Optional#empty()} means no {@code Content-Type} header is 034 * emitted. 035 * 036 * @author <a href="https://www.revetkn.com">Mark Allen</a> 037 */ 038@FunctionalInterface 039@ThreadSafe 040public interface MimeTypeResolver { 041 /** 042 * Resolves the {@code Content-Type} for {@code path}. 043 * <p> 044 * When configured through {@link StaticFiles.Builder#mimeTypeResolver(MimeTypeResolver)}, this 045 * resolver fully replaces the default resolver. To extend Soklet's default extension map, delegate 046 * to {@link #defaultInstance()} for paths your implementation does not handle. 047 * 048 * @param path the resolved file path being served 049 * @return the content type to emit, or {@link Optional#empty()} to omit it 050 */ 051 @NonNull 052 Optional<@NonNull String> contentTypeFor(@NonNull Path path); 053 054 /** 055 * Acquires Soklet's default threadsafe {@link MimeTypeResolver}. 056 * <p> 057 * The default resolver uses a curated deterministic extension map for common web assets and returns 058 * {@link Optional#empty()} for unknown extensions. 059 * 060 * @return the default {@code MimeTypeResolver} 061 */ 062 @NonNull 063 static MimeTypeResolver defaultInstance() { 064 return DefaultMimeTypeResolver.defaultInstance(); 065 } 066}