This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push: new c2b225297e Add utlity config file resource lookup c2b225297e is described below commit c2b225297e16d019c26f7fb99ac32baa9bae757d Author: remm <r...@apache.org> AuthorDate: Thu Jun 29 15:59:36 2023 +0200 Add utlity config file resource lookup Located on Context to allow looking up resources from the webapp (prefixed with "webapp:") and make the resource lookup API more visible. --- java/org/apache/catalina/Context.java | 40 +++++++++++++++++++++++++++++++++++ webapps/docs/changelog.xml | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/java/org/apache/catalina/Context.java b/java/org/apache/catalina/Context.java index d8b286b358..f9ebfe3340 100644 --- a/java/org/apache/catalina/Context.java +++ b/java/org/apache/catalina/Context.java @@ -16,6 +16,10 @@ */ package org.apache.catalina; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; import java.net.URL; import java.util.Locale; import java.util.Map; @@ -38,6 +42,8 @@ import org.apache.tomcat.util.descriptor.web.FilterDef; import org.apache.tomcat.util.descriptor.web.FilterMap; import org.apache.tomcat.util.descriptor.web.LoginConfig; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; +import org.apache.tomcat.util.file.ConfigFileLoader; +import org.apache.tomcat.util.file.ConfigurationSource.Resource; import org.apache.tomcat.util.http.CookieProcessor; /** @@ -84,6 +90,12 @@ public interface Context extends Container, ContextBind { String CHANGE_SESSION_ID_EVENT = "changeSessionId"; + /** + * Prefix for resource lookup. + */ + String WEBAPP_PROTOCOL = "webapp:"; + + // ------------------------------------------------------------- Properties /** @@ -1955,6 +1967,34 @@ public interface Context extends Container, ContextBind { void setDispatcherWrapsSameObject(boolean dispatcherWrapsSameObject); + /** + * Find configuration file with the specified path, first looking into the + * webapp resources, then delegating to + * <code>ConfigFileLoader.getSource().getResource</code>. The + * <code>WEBAPP_PROTOCOL</code> constant prefix is used to denote webapp + * resources. + * @param name The resource name + * @return the resource + * @throws IOException if an error occurs or if the resource does not exist + */ + default Resource findConfigFileResource(String name) throws IOException { + if (name.startsWith(WEBAPP_PROTOCOL)) { + String path = name.substring(WEBAPP_PROTOCOL.length()); + WebResource resource = getResources().getResource(path); + if (resource.canRead() && resource.isFile()) { + InputStream stream = resource.getInputStream(); + try { + return new Resource(stream, resource.getURL().toURI()); + } catch (URISyntaxException e) { + stream.close(); + } + } + throw new FileNotFoundException(name); + } + return ConfigFileLoader.getSource().getResource(name); + } + + /** * @return <code>true</code> if the resources archive lookup will * use a bloom filter. diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index b444044e79..4690a18e38 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -124,6 +124,12 @@ if the web applications were deliberately crafted to allow it even when <code>allowLinking</code> was set to <code>false</code>. (markt) </fix> + <update> + Add utlity config file resource lookup on <code>Context</code> to allow + looking up resources from the webapp (prefixed with + <code>webapp:</code>) and make the resource lookup API more visible. + (remm) + </update> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org