Author: markt
Date: Thu Jan 20 16:35:55 2011
New Revision: 1061376

URL: http://svn.apache.org/viewvc?rev=1061376&view=rev
Log:
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

Added:
    tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/
    tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/
    
tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp
   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
    tomcat/trunk/java/org/apache/naming/resources/BaseDirContext.java
    tomcat/trunk/test/org/apache/catalina/core/TestStandardContextResources.java
    tomcat/trunk/webapps/docs/config/context.xml

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1061376&r1=1061375&r2=1061376&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Thu Jan 20 
16:35:55 2011
@@ -817,9 +817,22 @@ public class StandardContext extends Con
 
     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 Con
                 ((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()) {

Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=1061376&r1=1061375&r2=1061376&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Thu Jan 20 
16:35:55 2011
@@ -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

Modified: tomcat/trunk/java/org/apache/naming/resources/BaseDirContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/resources/BaseDirContext.java?rev=1061376&r1=1061375&r2=1061376&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/naming/resources/BaseDirContext.java (original)
+++ tomcat/trunk/java/org/apache/naming/resources/BaseDirContext.java Thu Jan 
20 16:35:55 2011
@@ -167,7 +167,15 @@ public abstract class BaseDirContext imp
         }
     }
     
-    
+
+    /**
+     * Add an alternative DirContext (must contain META-INF/resources) 
directly.
+     */
+    public void addAltDirContext(DirContext altDirContext) {
+        altDirContexts.add(altDirContext);
+    }
+
+
     /**
      * Add an alias.
      */

Modified: 
tomcat/trunk/test/org/apache/catalina/core/TestStandardContextResources.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestStandardContextResources.java?rev=1061376&r1=1061375&r2=1061376&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/core/TestStandardContextResources.java 
(original)
+++ 
tomcat/trunk/test/org/apache/catalina/core/TestStandardContextResources.java 
Thu Jan 20 16:35:55 2011
@@ -71,6 +71,35 @@ public class TestStandardContextResource
                 "<p>resourceD.jsp in resources.jar</p>");
         assertPageContains("/test/folder/resourceE.jsp",
                 "<p>resourceE.jsp in the web application</p>");
+        assertPageContains("/test/resourceG.jsp",
+                "<p>resourceG.jsp in WEB-INF/classes</p>", 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",
+                "<p>resourceA.jsp in the web application</p>");
+        assertPageContains("/test/resourceB.jsp",
+                "<p>resourceB.jsp in resources.jar</p>");
+        assertPageContains("/test/folder/resourceC.jsp",
+                "<p>resourceC.jsp in the web application</p>");
+        assertPageContains("/test/folder/resourceD.jsp",
+                "<p>resourceD.jsp in resources.jar</p>");
+        assertPageContains("/test/folder/resourceE.jsp",
+                "<p>resourceE.jsp in the web application</p>");
+        assertPageContains("/test/resourceG.jsp",
+                "<p>resourceG.jsp in WEB-INF/classes</p>");
     }
 
     public void testResourcesAbsoluteOrdering() throws Exception {
@@ -196,11 +225,22 @@ public class TestStandardContextResource
         }
     }
 
-    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);
+        }
     }
 }

Added: 
tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp?rev=1061376&view=auto
==============================================================================
--- 
tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp
 (added)
+++ 
tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp
 Thu Jan 20 16:35:55 2011
@@ -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.
+--%>
+<p>resourceG.jsp in WEB-INF/classes</p>

Propchange: 
tomcat/trunk/test/webapp-3.0-fragments/WEB-INF/classes/META-INF/resources/resourceG.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/webapps/docs/config/context.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/context.xml?rev=1061376&r1=1061375&r2=1061376&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/context.xml (original)
+++ tomcat/trunk/webapps/docs/config/context.xml Thu Jan 20 16:35:55 2011
@@ -397,6 +397,17 @@
 
     <attributes>
 
+      <attribute name="addWebinfClassesResources" required="false">
+        <p>This attribute controls if, in addition to static resources being
+        served from <code>META-INF/resources</code> inside web application JAR
+        files, static resources are also served from
+        <code>WEB-INF/classes/META-INF/resources</code>. 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 
<code>true</code>.
+        </p>
+      </attribute>
+
       <attribute name="aliases" required="false">
         <p>This attribute provides a list of external locations from which to
         load resources for this context. The list of aliases should be of



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to