Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,29 @@ public boolean isDefault()
return isDefault;
}

/**
* <p>
* Clear the MimeTypes references.
* </p>
*
* <p>
* 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).
* </p>
*
* @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
*
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Comment on lines +175 to +185
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly using theses as examples on how to set up the MimeTypes object properly.

}
}