Skip to content

Commit ca921d1

Browse files
authored
Merge pull request #13373 from jetty/fix/12.1.x/mimetypes-mutable-clear
Issue #13366 - Introduce MimeTypes.Mutable.clear()
2 parents db2e237 + f493116 commit ca921d1

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,29 @@ public boolean isDefault()
587587
return isDefault;
588588
}
589589

590+
/**
591+
* <p>
592+
* Clear the MimeTypes references.
593+
* </p>
594+
*
595+
* <p>
596+
* Once you have cleared out the MimeTypes object, make sure to properly
597+
* set it up with extension to mime-type maps, along with inferred and
598+
* assumed charsets for the relevant mime-types (eg: html, text, json, etc).
599+
* </p>
600+
*
601+
* @see #addMimeMapping(String, String)
602+
* @see #addAssumed(String, String)
603+
* @see #addInferred(String, String)
604+
*/
605+
public void clear()
606+
{
607+
_mimeMap.clear();
608+
_assumedEncodings.clear();
609+
_assumedNoEncodings.clear();
610+
_inferredEncodings.clear();
611+
}
612+
590613
/**
591614
* Set a mime mapping
592615
*
@@ -735,13 +758,10 @@ public void addEncodings(Resource encodingProperties) throws UncheckedIOExceptio
735758
*/
736759
public void setFrom(MimeTypes other)
737760
{
738-
_mimeMap.clear();
761+
clear();
739762
_mimeMap.putAll(other.getMimeMap());
740-
_assumedEncodings.clear();
741763
_assumedEncodings.putAll(other._assumedEncodings);
742-
_inferredEncodings.clear();
743764
_inferredEncodings.putAll(other._inferredEncodings);
744-
_assumedNoEncodings.clear();
745765
_assumedNoEncodings.addAll(other._assumedNoEncodings);
746766
}
747767

jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.junit.jupiter.params.provider.MethodSource;
2222

2323
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.greaterThan;
2425
import static org.hamcrest.Matchers.is;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.assertNull;
2628

2729
public class MimeTypesTest
@@ -148,4 +150,38 @@ public void testMimeTypesGetBaseType(String contentTypeWithCharset, MimeTypes.Ty
148150
{
149151
assertThat(MimeTypes.getBaseType(contentTypeWithCharset), is(expectedType));
150152
}
153+
154+
/**
155+
* Test of a use case where the webapp wants a specific set of mime types,
156+
* no defaults. This is accomplished by replacing getting the MimeTypes.Mutable
157+
* associated with the webapp, calling clear, and then setting the values you want.
158+
*/
159+
@Test
160+
public void testMimeTypesMutableClear()
161+
{
162+
MimeTypes.Mutable webappMimeTypes = new MimeTypes.Mutable();
163+
164+
// Verify that "defaults" behavior is still there.
165+
assertEquals("UTF-8", webappMimeTypes.getAssumedCharsetName("application/json"));
166+
assertThat(webappMimeTypes._mimeMap.size(), greaterThan(100));
167+
168+
// Clear out the mime-types
169+
webappMimeTypes.clear();
170+
assertThat(webappMimeTypes._mimeMap.size(), is(0));
171+
assertThat(webappMimeTypes._assumedEncodings.size(), is(0));
172+
assertThat(webappMimeTypes._assumedNoEncodings.size(), is(0));
173+
assertThat(webappMimeTypes._inferredEncodings.size(), is(0));
174+
175+
// Set a few new values
176+
webappMimeTypes.addMimeMapping("html", "text/html");
177+
// HTML is inferred to be UTF-8, and will always have a 'charset' on the content-type header
178+
webappMimeTypes.addInferred("text/html", "utf-8");
179+
180+
webappMimeTypes.addMimeMapping("json", "application/json");
181+
// JSON is always UTF-8, and is never represented as a 'charset' on the content-type header
182+
webappMimeTypes.addAssumed("application/json", "utf-8");
183+
184+
// Images never have a 'charset' on the content-type field
185+
webappMimeTypes.addAssumed("image/*", null);
186+
}
151187
}

0 commit comments

Comments
 (0)