Skip to content

Commit

Permalink
Add utlity config file resource lookup
Browse files Browse the repository at this point in the history
Located on Context to allow looking up resources from the webapp
(prefixed with "webapp:") and make the resource lookup API more visible.
  • Loading branch information
rmaucher committed Jun 28, 2023
1 parent 91d3d59 commit 3d41f33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions java/org/apache/catalina/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.catalina;

import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;
Expand All @@ -38,6 +39,7 @@
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.ConfigurationSource.Resource;
import org.apache.tomcat.util.http.CookieProcessor;

/**
Expand Down Expand Up @@ -84,6 +86,11 @@ public interface Context extends Container, ContextBind {
String CHANGE_SESSION_ID_EVENT = "changeSessionId";


/**
* Prefix for resource lookup.
*/
String WEBAPP_PROTOCOL = "webapp:";

// ------------------------------------------------------------- Properties

/**
Expand Down Expand Up @@ -1963,4 +1970,17 @@ void setAllowMultipleLeadingForwardSlashInPath(
* @param dispatcherWrapsSameObject the new flag value
*/
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
*/
Resource findConfigFileResource(String name) throws IOException;
}
22 changes: 22 additions & 0 deletions java/org/apache/catalina/core/StandardContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -127,6 +128,8 @@
import org.apache.tomcat.util.descriptor.web.MessageDestination;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
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;
import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.scan.StandardJarScanner;
Expand Down Expand Up @@ -3497,6 +3500,25 @@ public String[] findWrapperListeners() {
}


@Override
public 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()) {
InputStream stream = resource.getInputStream();
try {
return new Resource(stream, resource.getURL().toURI());
} catch (URISyntaxException e) {
stream.close();
}
}
return null;

This comment has been minimized.

Copy link
@michael-o

michael-o Jun 29, 2023

Member

I believe that this implementation is not correct. Look how the ConfigResourceLoader is implemented. It throws and IOException from the URISyntaxException and a FileNotFoundException if the resource is not available:

            } catch (URISyntaxException e) {
                stream.close();
                throw new IOException(sm.getString("catalinaConfigurationSource.cannotObtainURL", name), e);
            }

and

        if (classpathUrl == null) {
            throw new FileNotFoundException(sm.getString("classpathUrlStreamHandler.notFound", u));
        }

Please stick to that pattern otherwise I need to handle different approaches in the listener.

So it can never return null, only throw an exception.

} else {
return ConfigFileLoader.getSource().getResource(name);
}
}

/**
* Reload this web application, if reloading is supported.
* <p>
Expand Down
6 changes: 6 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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">
Expand Down

0 comments on commit 3d41f33

Please sign in to comment.