Skip to content

Commit f383aa1

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 b1bf4bb commit f383aa1

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
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

@@ -186,6 +190,14 @@ public Compression getCompression() {
186190
return this.compression;
187191
}
188192

193+
public MimeMappings getMimeMappings() {
194+
return this.mimeMappings;
195+
}
196+
197+
public void setMimeMappings(Map<String, String> customMappings) {
198+
customMappings.forEach(this.mimeMappings::add);
199+
}
200+
189201
public Http2 getHttp2() {
190202
return this.http2;
191203
}

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-1
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
import java.util.Map;
2525

2626
import io.undertow.UndertowOptions;
27+
import jakarta.servlet.http.HttpServlet;
28+
import jakarta.servlet.http.HttpServletRequest;
29+
import jakarta.servlet.http.HttpServletResponse;
2730
import org.apache.catalina.connector.Connector;
2831
import org.apache.catalina.core.StandardContext;
2932
import org.apache.catalina.core.StandardEngine;
3033
import org.apache.catalina.valves.AccessLogValve;
3134
import org.apache.catalina.valves.RemoteIpValve;
3235
import org.apache.coyote.AbstractProtocol;
3336
import org.apache.tomcat.util.net.AbstractEndpoint;
34-
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
3537
import org.eclipse.jetty.server.Server;
38+
import org.eclipse.jetty.servlet.ServletContextHandler;
3639
import org.eclipse.jetty.util.thread.QueuedThreadPool;
3740
import org.junit.jupiter.api.Test;
3841
import reactor.netty.http.HttpDecoderSpec;
@@ -46,6 +49,12 @@
4649
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
4750
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
4851
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
52+
import org.springframework.boot.web.server.MimeMappings;
53+
import org.springframework.boot.web.server.MimeMappings.Mapping;
54+
import org.springframework.http.HttpEntity;
55+
import org.springframework.http.HttpHeaders;
56+
import org.springframework.http.MediaType;
57+
import org.springframework.http.client.ClientHttpResponse;
4958
import org.springframework.test.util.ReflectionTestUtils;
5059
import org.springframework.util.unit.DataSize;
5160

@@ -66,6 +75,7 @@
6675
* @author Rafiullah Hamedy
6776
* @author Chris Bono
6877
* @author Parviz Rozikov
78+
* @author Lasse Wulff
6979
*/
7080
@DirtiesUrlFactories
7181
class ServerPropertiesTests {
@@ -182,6 +192,21 @@ void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
182192
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
183193
}
184194

195+
@Test
196+
void testDefaultMimeMapping() {
197+
assertThat(this.properties.getMimeMappings())
198+
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
199+
}
200+
201+
@Test
202+
void testCustomizedMimeMapping() {
203+
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
204+
expectedMappings.add("mjs", "text/javascript");
205+
bind("server.mime-mappings.mjs", "text/javascript");
206+
assertThat(this.properties.getMimeMappings())
207+
.containsExactly(expectedMappings.getAll().toArray(new Mapping[0]));
208+
}
209+
185210
@Test
186211
void testCustomizeUriEncoding() {
187212
bind("server.tomcat.uri-encoding", "US-ASCII");

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)