Skip to content

Commit 07ed2a1

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 gh-38902
1 parent 5f680cc commit 07ed2a1

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
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

+23-8
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;
@@ -42,6 +40,7 @@
4240
import org.eclipse.jetty.server.Request;
4341
import org.eclipse.jetty.server.Server;
4442
import org.eclipse.jetty.util.thread.QueuedThreadPool;
43+
import org.jetbrains.annotations.NotNull;
4544
import org.junit.jupiter.api.Test;
4645
import reactor.netty.http.HttpDecoderSpec;
4746

@@ -55,7 +54,8 @@
5554
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
5655
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
5756
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
58-
import org.springframework.boot.web.servlet.ServletContextInitializer;
57+
import org.springframework.boot.web.server.MimeMappings;
58+
import org.springframework.boot.web.server.MimeMappings.Mapping;
5959
import org.springframework.http.HttpEntity;
6060
import org.springframework.http.HttpHeaders;
6161
import org.springframework.http.MediaType;
@@ -84,6 +84,7 @@
8484
* @author Rafiullah Hamedy
8585
* @author Chris Bono
8686
* @author Parviz Rozikov
87+
* @author Lasse Wulff
8788
*/
8889
@DirtiesUrlFactories
8990
class ServerPropertiesTests {
@@ -199,6 +200,21 @@ void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
199200
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
200201
}
201202

203+
@Test
204+
void testDefaultMimeMapping() {
205+
assertThat(this.properties.getMimeMappings()).containsExactly(
206+
MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
207+
}
208+
209+
@Test
210+
void testCustomizedMimeMapping() {
211+
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
212+
expectedMappings.add("mjs", "text/javascript");
213+
bind("server.mime-mappings.mjs", "text/javascript");
214+
assertThat(this.properties.getMimeMappings()).containsExactly(
215+
expectedMappings.getAll().toArray(new Mapping[0]));
216+
}
217+
202218
@Test
203219
void testCustomizeUriEncoding() {
204220
bind("server.tomcat.uri-encoding", "US-ASCII");
@@ -479,12 +495,11 @@ void jettyThreadPoolPropertyDefaultsShouldMatchServerDefault() {
479495
void jettyMaxHttpFormPostSizeMatchesDefault() {
480496
JettyServletWebServerFactory jettyFactory = new JettyServletWebServerFactory(0);
481497
JettyWebServer jetty = (JettyWebServer) jettyFactory
482-
.getWebServer((ServletContextInitializer) (servletContext) -> servletContext
498+
.getWebServer((servletContext) -> servletContext
483499
.addServlet("formPost", new HttpServlet() {
484500

485501
@Override
486-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
487-
throws ServletException, IOException {
502+
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
488503
req.getParameterMap();
489504
}
490505

@@ -506,12 +521,12 @@ public void onDispatchFailure(Request request, Throwable ex) {
506521
template.setErrorHandler(new ResponseErrorHandler() {
507522

508523
@Override
509-
public boolean hasError(ClientHttpResponse response) throws IOException {
524+
public boolean hasError(@NotNull ClientHttpResponse response) {
510525
return false;
511526
}
512527

513528
@Override
514-
public void handleError(ClientHttpResponse response) throws IOException {
529+
public void handleError(@NotNull ClientHttpResponse response) {
515530

516531
}
517532

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)