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

View File

@@ -16,9 +16,13 @@
package org.springframework.cloud.function.serverless.web; package org.springframework.cloud.function.serverless.web;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
@@ -104,7 +108,14 @@ public class ServerlessServletContext implements ServletContext {
@Override @Override
public String getMimeType(String file) { 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 @Override