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