Skip to content

Makes it easier to customize a Servlet-based web server's mime mappings #39430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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 @@ -37,6 +37,7 @@
import org.springframework.boot.web.server.Compression;
import org.springframework.boot.web.server.Cookie;
import org.springframework.boot.web.server.Http2;
import org.springframework.boot.web.server.MimeMappings;
import org.springframework.boot.web.server.Shutdown;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.servlet.server.Encoding;
Expand Down Expand Up @@ -71,6 +72,7 @@
* @author Parviz Rozikov
* @author Florian Storz
* @author Michael Weidmann
* @author Lasse Wulff
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
Expand Down Expand Up @@ -115,6 +117,8 @@ public class ServerProperties {
@NestedConfigurationProperty
private final Compression compression = new Compression();

private final MimeMappings mimeMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);

@NestedConfigurationProperty
private final Http2 http2 = new Http2();

Expand Down Expand Up @@ -186,6 +190,14 @@ public Compression getCompression() {
return this.compression;
}

public MimeMappings getMimeMappings() {
return this.mimeMappings;
}

public void setMimeMappings(Map<String, String> customMappings) {
customMappings.forEach(this.mimeMappings::add);
}

public Http2 getHttp2() {
return this.http2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Olivier Lamy
* @author Yunkun Huang
* @author Scott Frederick
* @author Lasse Wulff
* @since 2.0.0
*/
public class ServletWebServerFactoryCustomizer
Expand Down Expand Up @@ -94,6 +95,7 @@ public void customize(ConfigurableServletWebServerFactory factory) {
map.from(() -> this.cookieSameSiteSuppliers)
.whenNot(CollectionUtils::isEmpty)
.to(factory::setCookieSameSiteSuppliers);
map.from(this.serverProperties::getMimeMappings).to(factory::setMimeMappings);
this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.MimeMappings;
import org.springframework.boot.web.server.MimeMappings.Mapping;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.unit.DataSize;

Expand All @@ -66,6 +68,7 @@
* @author Rafiullah Hamedy
* @author Chris Bono
* @author Parviz Rozikov
* @author Lasse Wulff
*/
@DirtiesUrlFactories
class ServerPropertiesTests {
Expand Down Expand Up @@ -182,6 +185,21 @@ void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
}

@Test
void testDefaultMimeMapping() {
assertThat(this.properties.getMimeMappings())
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
}

@Test
void testCustomizedMimeMapping() {
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
expectedMappings.add("mjs", "text/javascript");
bind("server.mime-mappings.mjs", "text/javascript");
assertThat(this.properties.getMimeMappings())
.containsExactly(expectedMappings.getAll().toArray(new Mapping[0]));
}

@Test
void testCustomizeUriEncoding() {
bind("server.tomcat.uri-encoding", "US-ASCII");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @author Brian Clozel
* @author Yunkun Huang
* @author Lasse Wulff
*/
class ServletWebServerFactoryCustomizerTests {

Expand Down Expand Up @@ -72,6 +73,13 @@ void testCustomizeDisplayName() {
then(factory).should().setDisplayName("TestName");
}

@Test
void testCustomMimeMappings() {
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
this.customizer.customize(factory);
then(factory).should().setMimeMappings(this.properties.getMimeMappings());
}

@Test
void testCustomizeDefaultServlet() {
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
Expand Down