Skip to content

Commit 500584f

Browse files
committed
Merge pull request #39430 from BenchmarkingBuffalo
* pr/39430: Add possibility for custom MimeMappings Closes gh-39430
2 parents 8c91b09 + 038ea2c commit 500584f

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
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

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
4747
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
4848
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
49+
import org.springframework.boot.web.server.MimeMappings;
50+
import org.springframework.boot.web.server.MimeMappings.Mapping;
4951
import org.springframework.test.util.ReflectionTestUtils;
5052
import org.springframework.util.unit.DataSize;
5153

@@ -66,6 +68,7 @@
6668
* @author Rafiullah Hamedy
6769
* @author Chris Bono
6870
* @author Parviz Rozikov
71+
* @author Lasse Wulff
6972
*/
7073
@DirtiesUrlFactories
7174
class ServerPropertiesTests {
@@ -182,6 +185,21 @@ void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
182185
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
183186
}
184187

188+
@Test
189+
void testDefaultMimeMapping() {
190+
assertThat(this.properties.getMimeMappings())
191+
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
192+
}
193+
194+
@Test
195+
void testCustomizedMimeMapping() {
196+
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
197+
expectedMappings.add("mjs", "text/javascript");
198+
bind("server.mime-mappings.mjs", "text/javascript");
199+
assertThat(this.properties.getMimeMappings())
200+
.containsExactly(expectedMappings.getAll().toArray(new Mapping[0]));
201+
}
202+
185203
@Test
186204
void testCustomizeUriEncoding() {
187205
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)