Skip to content

Commit fdb7570

Browse files
Add possibility for custom MimeMappings
Add a new property called 'mime-mappings' under the 'server' property. This is a key-value-map, which is added to the default MimeMappings. See spring-projectsgh-38902
1 parent 5f680cc commit fdb7570

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.boot.web.server.Compression;
3838
import org.springframework.boot.web.server.Cookie;
3939
import org.springframework.boot.web.server.Http2;
40+
import org.springframework.boot.web.server.MimeMappings;
4041
import org.springframework.boot.web.server.Shutdown;
4142
import org.springframework.boot.web.server.Ssl;
4243
import org.springframework.boot.web.servlet.server.Encoding;
@@ -71,6 +72,7 @@
7172
* @author Parviz Rozikov
7273
* @author Florian Storz
7374
* @author Michael Weidmann
75+
* @author Lasse Wulff
7476
* @since 1.0.0
7577
*/
7678
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
@@ -115,6 +117,8 @@ public class ServerProperties {
115117
@NestedConfigurationProperty
116118
private final Compression compression = new Compression();
117119

120+
private final MimeMappings mimeMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
121+
118122
@NestedConfigurationProperty
119123
private final Http2 http2 = new Http2();
120124

@@ -197,6 +201,14 @@ public Compression getCompression() {
197201
return this.compression;
198202
}
199203

204+
public MimeMappings getMimeMappings() {
205+
return this.mimeMappings;
206+
}
207+
208+
public void setMimeMappings(Map<String, String> customMappings) {
209+
customMappings.forEach(this.mimeMappings::add);
210+
}
211+
200212
public Http2 getHttp2() {
201213
return this.http2;
202214
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Olivier Lamy
3939
* @author Yunkun Huang
4040
* @author Scott Frederick
41+
* @author Lasse Wulff
4142
* @since 2.0.0
4243
*/
4344
public class ServletWebServerFactoryCustomizer
@@ -94,6 +95,7 @@ public void customize(ConfigurableServletWebServerFactory factory) {
9495
map.from(() -> this.cookieSameSiteSuppliers)
9596
.whenNot(CollectionUtils::isEmpty)
9697
.to(factory::setCookieSameSiteSuppliers);
98+
map.from(this.serverProperties::getMimeMappings).to(factory::setMimeMappings);
9799
this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory));
98100
}
99101

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

+26-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.web;
1818

19-
import java.io.IOException;
2019
import java.net.InetAddress;
2120
import java.net.URI;
2221
import java.nio.charset.StandardCharsets;
@@ -27,7 +26,6 @@
2726
import java.util.concurrent.atomic.AtomicReference;
2827

2928
import io.undertow.UndertowOptions;
30-
import jakarta.servlet.ServletException;
3129
import jakarta.servlet.http.HttpServlet;
3230
import jakarta.servlet.http.HttpServletRequest;
3331
import jakarta.servlet.http.HttpServletResponse;
@@ -55,7 +53,8 @@
5553
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
5654
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
5755
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
58-
import org.springframework.boot.web.servlet.ServletContextInitializer;
56+
import org.springframework.boot.web.server.MimeMappings;
57+
import org.springframework.boot.web.server.MimeMappings.Mapping;
5958
import org.springframework.http.HttpEntity;
6059
import org.springframework.http.HttpHeaders;
6160
import org.springframework.http.MediaType;
@@ -84,6 +83,7 @@
8483
* @author Rafiullah Hamedy
8584
* @author Chris Bono
8685
* @author Parviz Rozikov
86+
* @author Lasse Wulff
8787
*/
8888
@DirtiesUrlFactories
8989
class ServerPropertiesTests {
@@ -199,6 +199,21 @@ void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
199199
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
200200
}
201201

202+
@Test
203+
void testDefaultMimeMapping() {
204+
assertThat(this.properties.getMimeMappings())
205+
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
206+
}
207+
208+
@Test
209+
void testCustomizedMimeMapping() {
210+
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
211+
expectedMappings.add("mjs", "text/javascript");
212+
bind("server.mime-mappings.mjs", "text/javascript");
213+
assertThat(this.properties.getMimeMappings())
214+
.containsExactly(expectedMappings.getAll().toArray(new Mapping[0]));
215+
}
216+
202217
@Test
203218
void testCustomizeUriEncoding() {
204219
bind("server.tomcat.uri-encoding", "US-ASCII");
@@ -479,17 +494,14 @@ void jettyThreadPoolPropertyDefaultsShouldMatchServerDefault() {
479494
void jettyMaxHttpFormPostSizeMatchesDefault() {
480495
JettyServletWebServerFactory jettyFactory = new JettyServletWebServerFactory(0);
481496
JettyWebServer jetty = (JettyWebServer) jettyFactory
482-
.getWebServer((ServletContextInitializer) (servletContext) -> servletContext
483-
.addServlet("formPost", new HttpServlet() {
497+
.getWebServer((servletContext) -> servletContext.addServlet("formPost", new HttpServlet() {
484498

485-
@Override
486-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
487-
throws ServletException, IOException {
488-
req.getParameterMap();
489-
}
499+
@Override
500+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
501+
req.getParameterMap();
502+
}
490503

491-
})
492-
.addMapping("/form"));
504+
}).addMapping("/form"));
493505
jetty.start();
494506
org.eclipse.jetty.server.Connector connector = jetty.getServer().getConnectors()[0];
495507
final AtomicReference<Throwable> failure = new AtomicReference<>();
@@ -506,12 +518,12 @@ public void onDispatchFailure(Request request, Throwable ex) {
506518
template.setErrorHandler(new ResponseErrorHandler() {
507519

508520
@Override
509-
public boolean hasError(ClientHttpResponse response) throws IOException {
521+
public boolean hasError(ClientHttpResponse response) {
510522
return false;
511523
}
512524

513525
@Override
514-
public void handleError(ClientHttpResponse response) throws IOException {
526+
public void handleError(ClientHttpResponse response) {
515527

516528
}
517529

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Brian Clozel
4747
* @author Yunkun Huang
48+
* @author Lasse Wulff
4849
*/
4950
class ServletWebServerFactoryCustomizerTests {
5051

@@ -72,6 +73,13 @@ void testCustomizeDisplayName() {
7273
then(factory).should().setDisplayName("TestName");
7374
}
7475

76+
@Test
77+
void testCustomMimeMappings() {
78+
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
79+
this.customizer.customize(factory);
80+
then(factory).should().setMimeMappings(this.properties.getMimeMappings());
81+
}
82+
7583
@Test
7684
void testCustomizeDefaultServlet() {
7785
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);

0 commit comments

Comments
 (0)