From: markt Date: Thu, 20 Jan 2011 16:35:55 +0000 (+0000) Subject: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50535 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1054819c933afab14bc8a6a8d655c6c573703508;p=tomcat7.0 Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50535 Provid an option (disabled by default) to serve resources from /WEB-INF/classes/META-INF/resources git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1061376 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java index 71a2a5fdc..c2e5b97b6 100644 --- a/java/org/apache/catalina/core/StandardContext.java +++ b/java/org/apache/catalina/core/StandardContext.java @@ -817,9 +817,22 @@ public class StandardContext extends ContainerBase private String webappVersion = ""; + private boolean addWebinfClassesResources = false; + // ----------------------------------------------------- Context Properties + public void setAddWebinfClassesResources( + boolean addWebinfClassesResources) { + this.addWebinfClassesResources = addWebinfClassesResources; + } + + + public boolean getAddWebinfClassesResources() { + return addWebinfClassesResources; + } + + @Override public void setWebappVersion(String webappVersion) { if (null == webappVersion) { @@ -4640,6 +4653,20 @@ public class StandardContext extends ContainerBase ((BaseDirContext) webappResources).allocate(); // Alias support ((BaseDirContext) webappResources).setAliases(getAliases()); + + if (effectiveMajorVersion >=3 && addWebinfClassesResources) { + try { + DirContext webInfCtx = + (DirContext) webappResources.lookup( + "/WEB-INF/classes"); + // Do the lookup to make sure it exists + webInfCtx.lookup("META-INF/resources"); + ((BaseDirContext) webappResources).addAltDirContext( + webInfCtx); + } catch (NamingException e) { + // Doesn't exist - ignore and carry on + } + } } // Register the cache in JMX if (isCachingAllowed()) { diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java index 2c5e5d138..3170279ea 100644 --- a/java/org/apache/catalina/startup/ContextConfig.java +++ b/java/org/apache/catalina/startup/ContextConfig.java @@ -1294,6 +1294,8 @@ public class ContextConfig } } processResourceJARs(resourceJars); + // See also StandardContext.resourcesStart() for + // WEB-INF/classes/META-INF/resources configuration } // Only look for ServletContainerInitializer if metadata is not diff --git a/java/org/apache/naming/resources/BaseDirContext.java b/java/org/apache/naming/resources/BaseDirContext.java index 1158107c4..ea09c726d 100644 --- a/java/org/apache/naming/resources/BaseDirContext.java +++ b/java/org/apache/naming/resources/BaseDirContext.java @@ -167,7 +167,15 @@ public abstract class BaseDirContext implements DirContext { } } - + + /** + * Add an alternative DirContext (must contain META-INF/resources) directly. + */ + public void addAltDirContext(DirContext altDirContext) { + altDirContexts.add(altDirContext); + } + + /** * Add an alias. */ diff --git a/test/org/apache/catalina/core/TestStandardContextResources.java b/test/org/apache/catalina/core/TestStandardContextResources.java index d9adf34ee..79d2bd466 100644 --- a/test/org/apache/catalina/core/TestStandardContextResources.java +++ b/test/org/apache/catalina/core/TestStandardContextResources.java @@ -71,6 +71,35 @@ public class TestStandardContextResources extends TomcatBaseTest { "

resourceD.jsp in resources.jar

"); assertPageContains("/test/folder/resourceE.jsp", "

resourceE.jsp in the web application

"); + assertPageContains("/test/resourceG.jsp", + "

resourceG.jsp in WEB-INF/classes

", 404); + } + + public void testResourcesWebInfClasses() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // app dir is relative to server home + File appDir = new File("test/webapp-3.0-fragments"); + + // Need to cast to be able to set StandardContext specific attribute + StandardContext ctxt = (StandardContext) + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + ctxt.setAddWebinfClassesResources(true); + + tomcat.start(); + + assertPageContains("/test/resourceA.jsp", + "

resourceA.jsp in the web application

"); + assertPageContains("/test/resourceB.jsp", + "

resourceB.jsp in resources.jar

"); + assertPageContains("/test/folder/resourceC.jsp", + "

resourceC.jsp in the web application

"); + assertPageContains("/test/folder/resourceD.jsp", + "

resourceD.jsp in resources.jar

"); + assertPageContains("/test/folder/resourceE.jsp", + "

resourceE.jsp in the web application

"); + assertPageContains("/test/resourceG.jsp", + "

resourceG.jsp in WEB-INF/classes

"); } public void testResourcesAbsoluteOrdering() throws Exception { @@ -196,11 +225,22 @@ public class TestStandardContextResources extends TomcatBaseTest { } } - private void assertPageContains(String pageUrl, String expected) + private void assertPageContains(String pageUrl, String expectedBody) throws IOException { - ByteChunk res = getUrl("http://localhost:" + getPort() + pageUrl); - String result = res.toString(); - assertTrue(result, result.indexOf(expected) > 0); + assertPageContains(pageUrl, expectedBody, 200); + } + + private void assertPageContains(String pageUrl, String expectedBody, + int expectedStatus) throws IOException { + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + pageUrl, res, null); + + assertEquals(expectedStatus, sc); + + if (expectedStatus == 200) { + String result = res.toString(); + assertTrue(result, result.indexOf(expectedBody) > 0); + } } } diff --git a/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp b/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp new file mode 100644 index 000000000..91046ca92 --- /dev/null +++ b/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp @@ -0,0 +1,21 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--%> +<%-- + Resource file that is present both in the web application and in the + WEB-INF/lib/resources.jar file. The one in the web application should win. +--%> +

resourceG.jsp in WEB-INF/classes

diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml index 3a75b7254..56829cfad 100644 --- a/webapps/docs/config/context.xml +++ b/webapps/docs/config/context.xml @@ -397,6 +397,17 @@ + +

This attribute controls if, in addition to static resources being + served from META-INF/resources inside web application JAR + files, static resources are also served from + WEB-INF/classes/META-INF/resources. This only applies to + web applications with a major version of 3 or higher. Since this is a + proprietary extension to the Servlet 3 specification, it is disabled by + default. To enable this feature, set the attribute to true. +

+
+

This attribute provides a list of external locations from which to load resources for this context. The list of aliases should be of