From f493116b8fe2a78782e61376bb2411cb9b2c6b71 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 22 Jul 2025 07:34:58 -0500 Subject: [PATCH] Issue #13366 - Introduce MimeTypes.Mutable.clear() --- .../org/eclipse/jetty/http/MimeTypes.java | 28 ++++++++++++--- .../org/eclipse/jetty/http/MimeTypesTest.java | 36 +++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java index 885561f54bfc..0f86878d8103 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java @@ -587,6 +587,29 @@ public boolean isDefault() return isDefault; } + /** + *

+ * Clear the MimeTypes references. + *

+ * + *

+ * Once you have cleared out the MimeTypes object, make sure to properly + * set it up with extension to mime-type maps, along with inferred and + * assumed charsets for the relevant mime-types (eg: html, text, json, etc). + *

+ * + * @see #addMimeMapping(String, String) + * @see #addAssumed(String, String) + * @see #addInferred(String, String) + */ + public void clear() + { + _mimeMap.clear(); + _assumedEncodings.clear(); + _assumedNoEncodings.clear(); + _inferredEncodings.clear(); + } + /** * Set a mime mapping * @@ -735,13 +758,10 @@ public void addEncodings(Resource encodingProperties) throws UncheckedIOExceptio */ public void setFrom(MimeTypes other) { - _mimeMap.clear(); + clear(); _mimeMap.putAll(other.getMimeMap()); - _assumedEncodings.clear(); _assumedEncodings.putAll(other._assumedEncodings); - _inferredEncodings.clear(); _inferredEncodings.putAll(other._inferredEncodings); - _assumedNoEncodings.clear(); _assumedNoEncodings.addAll(other._assumedNoEncodings); } diff --git a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java index 1dc6ea640651..2cf7a368b14e 100644 --- a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java +++ b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java @@ -21,7 +21,9 @@ import org.junit.jupiter.params.provider.MethodSource; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; public class MimeTypesTest @@ -148,4 +150,38 @@ public void testMimeTypesGetBaseType(String contentTypeWithCharset, MimeTypes.Ty { assertThat(MimeTypes.getBaseType(contentTypeWithCharset), is(expectedType)); } + + /** + * Test of a use case where the webapp wants a specific set of mime types, + * no defaults. This is accomplished by replacing getting the MimeTypes.Mutable + * associated with the webapp, calling clear, and then setting the values you want. + */ + @Test + public void testMimeTypesMutableClear() + { + MimeTypes.Mutable webappMimeTypes = new MimeTypes.Mutable(); + + // Verify that "defaults" behavior is still there. + assertEquals("UTF-8", webappMimeTypes.getAssumedCharsetName("application/json")); + assertThat(webappMimeTypes._mimeMap.size(), greaterThan(100)); + + // Clear out the mime-types + webappMimeTypes.clear(); + assertThat(webappMimeTypes._mimeMap.size(), is(0)); + assertThat(webappMimeTypes._assumedEncodings.size(), is(0)); + assertThat(webappMimeTypes._assumedNoEncodings.size(), is(0)); + assertThat(webappMimeTypes._inferredEncodings.size(), is(0)); + + // Set a few new values + webappMimeTypes.addMimeMapping("html", "text/html"); + // HTML is inferred to be UTF-8, and will always have a 'charset' on the content-type header + webappMimeTypes.addInferred("text/html", "utf-8"); + + webappMimeTypes.addMimeMapping("json", "application/json"); + // JSON is always UTF-8, and is never represented as a 'charset' on the content-type header + webappMimeTypes.addAssumed("application/json", "utf-8"); + + // Images never have a 'charset' on the content-type field + webappMimeTypes.addAssumed("image/*", null); + } }