You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2023/06/29 14:07:28 UTC

[tomcat] branch 9.0.x updated: Add utlity config file resource lookup

This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new f3d7b7a829 Add utlity config file resource lookup
f3d7b7a829 is described below

commit f3d7b7a8291f43cf5bd925efce6e5eb4889abf40
Author: remm <re...@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 4b35db7291..484fb582ae 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
 
     /**
@@ -1928,6 +1940,34 @@ public interface Context extends Container, ContextBind {
     boolean getCreateUploadTargets();
 
 
+    /**
+     * 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 43ef25d42d..ef9cc26b8c 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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org