Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=48384
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 13 Feb 2010 20:12:11 +0000 (20:12 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 13 Feb 2010 20:12:11 +0000 (20:12 +0000)
Add a per context xslt option for directory listings
Make the fallback options work as described in the docs

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@909887 13f79535-47bb-0310-9956-ffa450edef68

conf/web.xml
java/org/apache/catalina/servlets/DefaultServlet.java
webapps/docs/default-servlet.xml

index 3d271c4..3db15fd 100644 (file)
   <!--   localXsltFile       Make directory listings an XML doc and         -->
   <!--                       pass the result to this style sheet residing   -->
   <!--                       in that directory. This overrides              -->
-  <!--                        globalXsltFile[null]                          -->
+  <!--                       contextXsltFile and globalXsltFile[null]       -->
+  <!--                                                                      -->
+  <!--   contextXsltFile     Make directory listings an XML doc and         -->
+  <!--                       pass the result to this style sheet which is   -->
+  <!--                       relative to the context root. This overrides   -->
+  <!--                       globalXsltFile[null]                           -->
   <!--                                                                      -->
   <!--   globalXsltFile      Site wide configuration version of             -->
   <!--                       localXsltFile This argument is expected        -->
index 9ad9552..59ecc49 100644 (file)
@@ -124,13 +124,19 @@ public class DefaultServlet
     /**
      * Allow customized directory listing per directory.
      */
-    protected String  localXsltFile = null;
+    protected String localXsltFile = null;
 
 
     /**
+     * Allow customized directory listing per context.
+     */
+    protected String contextXsltFile = null;
+    
+    
+    /**
      * Allow customized directory listing per instance.
      */
-    protected String  globalXsltFile = null;
+    protected String globalXsltFile = null;
 
 
     /**
@@ -248,6 +254,7 @@ public class DefaultServlet
         fileEncoding = getServletConfig().getInitParameter("fileEncoding");
 
         globalXsltFile = getServletConfig().getInitParameter("globalXsltFile");
+        contextXsltFile = getServletConfig().getInitParameter("contextXsltFile");
         localXsltFile = getServletConfig().getInitParameter("localXsltFile");
         readmeFile = getServletConfig().getInitParameter("readmeFile");
 
@@ -1195,6 +1202,9 @@ public class DefaultServlet
                     trimmed.equalsIgnoreCase(localXsltFile))
                     continue;
 
+                if ((cacheEntry.name + trimmed).equals(contextXsltFile))
+                    continue;
+
                 CacheEntry childCacheEntry =
                     resources.lookupCache(cacheEntry.name + resourceName);
                 if (!childCacheEntry.exists) {
@@ -1492,11 +1502,19 @@ public class DefaultServlet
             } catch (NamingException e) {
                 if (debug > 10)
                     log("localXsltFile '" + localXsltFile + "' not found", e);
-
-                return null;
             }
         }
 
+        if (contextXsltFile != null) {
+            InputStream is =
+                getServletContext().getResourceAsStream(contextXsltFile);
+            if (is != null)
+                return is;
+
+            if (debug > 10)
+                log("contextXsltFile '" + contextXsltFile + "' not found");
+        }
+
         /*  Open and read in file in one fell swoop to reduce chance
          *  chance of leaving handle open.
          */
index 063a3ab..6ab6ff9 100644 (file)
@@ -125,9 +125,21 @@ The DefaultServlet allows the following initParamters:
         If you wish to customize your directory listing, you
         can use an XSL transformation. This value is an absolute
         file name which be used for all directory listings.
-        This can be disabled by per webapp by also declaring the
-        default servlet in your local webapp's web.xml. The format
-        of the xml is shown below.
+        This can be overridden per context and/or per directory. See
+        <strong>contextXsltFile</strong> and <strong>localXsltFile</strong>
+        below. The format of the xml is shown below.
+    </td>
+  </tr>
+  <tr>
+    <th valign='top'>contextXsltFile</th>
+    <td valign='top'>
+        You may also customize your directory listing by context by
+        configuring <code>contextXsltFile</code>. This should be a context
+        relative path (e.g.: <code>/path/to/context.xslt</code>). This
+        overrides <code>globalXsltFile</code>. If this value is present but a
+        file does not exist, then <code>globalXsltFile</code> will be used. If
+        <code>globalXsltFile</code> does not exist, then the default
+        directory listing will be shown.
     </td>
   </tr>
   <tr>
@@ -136,8 +148,10 @@ The DefaultServlet allows the following initParamters:
         You may also customize your directory listing by directory by
         configuring <code>localXsltFile</code>. This should be a relative
         file name in the directory where the listing will take place.
-        This overrides <code>globalXsltFile</code>. If this value
-        is present but a file does not exist, then
+        This overrides <code>globalXsltFile</code> and
+        <code>contextXsltFile</code>. If this value is present but a file
+        does not exist, then <code>contextXsltFile</code> will be used. If
+        <code>contextXsltFile</code> does not exist, then
         <code>globalXsltFile</code> will be used. If
         <code>globalXsltFile</code> does not exist, then the default
         directory listing will be shown.