Skip to content

Commit 230eceb

Browse files
dekiolegz
authored andcommitted
fix: null ServletContext returned (see aws/serverless-java-container#1087)
polishing implement getMimeType so AbstractMessageConverterMethodProcessor.resolveMediaType doesn't fail with UnsupportedOperationException
1 parent 317553b commit 230eceb

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessMVC.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ public final class ServerlessMVC {
7878

7979
private volatile ServletWebServerApplicationContext applicationContext;
8080

81-
private ServletContext servletContext;
82-
8381
private final CountDownLatch contextStartupLatch = new CountDownLatch(1);
8482

85-
private final long initializatioinTimeout;
83+
private final long initializationTimeout;
8684

8785
public static ServerlessMVC INSTANCE(Class<?>... componentClasses) {
8886
ServerlessMVC mvc = new ServerlessMVC();
@@ -103,7 +101,7 @@ private ServerlessMVC() {
103101
if (!StringUtils.hasText(timeoutValue)) {
104102
timeoutValue = System.getProperty(INIT_TIMEOUT);
105103
}
106-
this.initializatioinTimeout = StringUtils.hasText(timeoutValue) ? Long.valueOf(timeoutValue) : 20000;
104+
this.initializationTimeout = StringUtils.hasText(timeoutValue) ? Long.valueOf(timeoutValue) : 20000;
107105
}
108106

109107
private void initializeContextAsync(Class<?>... componentClasses) {
@@ -137,7 +135,7 @@ public ConfigurableWebApplicationContext getApplicationContext() {
137135

138136
public ServletContext getServletContext() {
139137
this.waitForContext();
140-
return this.servletContext;
138+
return this.dispatcher.getServletContext();
141139
}
142140

143141
public void stop() {
@@ -157,7 +155,7 @@ public void stop() {
157155
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
158156
*/
159157
public void service(HttpServletRequest request, HttpServletResponse response) throws Exception {
160-
Assert.state(this.waitForContext(), "Failed to initialize Application within the specified time of " + this.initializatioinTimeout + " milliseconds. "
158+
Assert.state(this.waitForContext(), "Failed to initialize Application within the specified time of " + this.initializationTimeout + " milliseconds. "
161159
+ "If you need to increase it, please set " + INIT_TIMEOUT + " environment variable");
162160
this.service(request, response, (CountDownLatch) null);
163161
}
@@ -189,7 +187,7 @@ public void service(HttpServletRequest request, HttpServletResponse response, Co
189187

190188
public boolean waitForContext() {
191189
try {
192-
return contextStartupLatch.await(initializatioinTimeout, TimeUnit.MILLISECONDS);
190+
return contextStartupLatch.await(initializationTimeout, TimeUnit.MILLISECONDS);
193191
}
194192
catch (InterruptedException e) {
195193
Thread.currentThread().interrupt();
@@ -215,7 +213,6 @@ private static class ProxyFilterChain implements FilterChain {
215213
* Create a {@code FilterChain} with Filter's and a Servlet.
216214
*
217215
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
218-
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
219216
* @since 4.0.x
220217
*/
221218
ProxyFilterChain(DispatcherServlet servlet) {

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessServletContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
package org.springframework.cloud.function.serverless.web;
1818

19+
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.net.MalformedURLException;
2122
import java.net.URL;
23+
import java.nio.file.Files;
24+
import java.nio.file.InvalidPathException;
25+
import java.nio.file.Paths;
2226
import java.util.ArrayList;
2327
import java.util.Collections;
2428
import java.util.Enumeration;
@@ -104,7 +108,14 @@ public int getEffectiveMinorVersion() {
104108

105109
@Override
106110
public String getMimeType(String file) {
107-
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
111+
String mimeType = null;
112+
try {
113+
mimeType = Files.probeContentType(Paths.get(file));
114+
}
115+
catch (IOException | InvalidPathException e) {
116+
log("unable to probe for content type " + file, e);
117+
}
118+
return mimeType;
108119
}
109120

110121
@Override

0 commit comments

Comments
 (0)