Skip to content

Commit

Permalink
fix: fix static resource retrieval with Jetty 12 (#18471) (#18508)
Browse files Browse the repository at this point in the history
With Jetty 12, static resources retrieved via servlet context's `getResource(String)` may return non-null URL for non-existing resource. Jetty 11 returns null for non-existing resource.
This change adds a double check for the static resource retrieved with the getResource, and returns null if it doesn't exist, to keep other parts work the same way as with Jetty 11.

Fixes: #18410

Co-authored-by: Tomi Virtanen <tltv@vaadin.com>
  • Loading branch information
vaadin-bot and tltv authored Jan 22, 2024
1 parent b82d2c1 commit 701eb16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -161,7 +164,7 @@ private static Logger getLogger() {

private URL getResourceUrl(ServletContext context, String path)
throws MalformedURLException {
URL resourceUrl = context.getResource(path);
URL resourceUrl = VaadinServletService.getStaticResource(context, path);
if (resourceUrl == null) {
// this is a workaround specific for Spring default static resources
// location: see #8705
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -240,7 +243,7 @@ public String resolveResource(String url) {
@Override
public URL getStaticResource(String path) {
try {
return getServlet().getServletContext().getResource(path);
return getStaticResource(getServlet().getServletContext(), path);
} catch (MalformedURLException e) {
getLogger().warn("Error finding resource for '{}'", path, e);
}
Expand Down Expand Up @@ -316,4 +319,22 @@ protected VaadinContext constructVaadinContext() {
protected void setDefaultClassLoader() {
setClassLoader(getServlet().getServletContext().getClassLoader());
}

static URL getStaticResource(ServletContext servletContext, String path)
throws MalformedURLException {
URL url = servletContext.getResource(path);
if (url != null && Optional.ofNullable(servletContext.getServerInfo())
.orElse("").contains("jetty/12.")) {
// Making sure that resource exists before returning it. Jetty
// 12 may return URL for non-existing resource.
try {
if (!Files.exists(Path.of(url.toURI()))) {
url = null;
}
} catch (URISyntaxException e) {
url = null;
}
}
return url;
}
}

0 comments on commit 701eb16

Please sign in to comment.