polishing

implement getMimeType so AbstractMessageConverterMethodProcessor.resolveMediaType doesn't fail with UnsupportedOperationException
This commit is contained in:
Dennis Kieselhorst
2024-11-12 22:35:54 +01:00
committed by Oleg Zhurakousky
parent 317553b8fa
commit 230eceb603
2 changed files with 17 additions and 9 deletions

View File

@@ -78,11 +78,9 @@ public final class ServerlessMVC {
private volatile ServletWebServerApplicationContext applicationContext;
private ServletContext servletContext;
private final CountDownLatch contextStartupLatch = new CountDownLatch(1);
private final long initializatioinTimeout;
private final long initializationTimeout;
public static ServerlessMVC INSTANCE(Class<?>... componentClasses) {
ServerlessMVC mvc = new ServerlessMVC();
@@ -103,7 +101,7 @@ public final class ServerlessMVC {
if (!StringUtils.hasText(timeoutValue)) {
timeoutValue = System.getProperty(INIT_TIMEOUT);
}
this.initializatioinTimeout = StringUtils.hasText(timeoutValue) ? Long.valueOf(timeoutValue) : 20000;
this.initializationTimeout = StringUtils.hasText(timeoutValue) ? Long.valueOf(timeoutValue) : 20000;
}
private void initializeContextAsync(Class<?>... componentClasses) {
@@ -137,7 +135,7 @@ public final class ServerlessMVC {
public ServletContext getServletContext() {
this.waitForContext();
return this.servletContext;
return this.dispatcher.getServletContext();
}
public void stop() {
@@ -157,7 +155,7 @@ public final class ServerlessMVC {
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws Exception {
Assert.state(this.waitForContext(), "Failed to initialize Application within the specified time of " + this.initializatioinTimeout + " milliseconds. "
Assert.state(this.waitForContext(), "Failed to initialize Application within the specified time of " + this.initializationTimeout + " milliseconds. "
+ "If you need to increase it, please set " + INIT_TIMEOUT + " environment variable");
this.service(request, response, (CountDownLatch) null);
}
@@ -189,7 +187,7 @@ public final class ServerlessMVC {
public boolean waitForContext() {
try {
return contextStartupLatch.await(initializatioinTimeout, TimeUnit.MILLISECONDS);
return contextStartupLatch.await(initializationTimeout, TimeUnit.MILLISECONDS);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -215,7 +213,6 @@ public final class ServerlessMVC {
* Create a {@code FilterChain} with Filter's and a Servlet.
*
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 4.0.x
*/
ProxyFilterChain(DispatcherServlet servlet) {

View File

@@ -16,9 +16,13 @@
package org.springframework.cloud.function.serverless.web;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
@@ -104,7 +108,14 @@ public class ServerlessServletContext implements ServletContext {
@Override
public String getMimeType(String file) {
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
String mimeType = null;
try {
mimeType = Files.probeContentType(Paths.get(file));
}
catch (IOException | InvalidPathException e) {
log("unable to probe for content type " + file, e);
}
return mimeType;
}
@Override