You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/01/08 20:43:13 UTC

svn commit: r1430463 - in /tomcat/trunk: java/org/apache/catalina/webresources/ test/org/apache/catalina/webresources/

Author: markt
Date: Tue Jan  8 19:43:13 2013
New Revision: 1430463

URL: http://svn.apache.org/viewvc?rev=1430463&view=rev
Log:
Adds support for Virtual resources to the DirResourceSet and JarResourceSet. This will allow an application to full traverse the resource hierarchy including any non-main resources mounted under a directory structure that does not exist in the main resources.

Added:
    tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/JarResourceRoot.java
    tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
    tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java
    tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java

Modified: tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java Tue Jan  8 19:43:13 2013
@@ -37,6 +37,9 @@ public abstract class AbstractFileResour
 
     protected File file(String name, boolean mustExist) {
 
+        if (name.equals("/")) {
+            name = "";
+        }
         File file = new File(fileBase, name);
         if (file.exists() && file.canRead() || !mustExist) {
 

Modified: tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java Tue Jan  8 19:43:13 2013
@@ -121,6 +121,18 @@ public class DirResourceSet extends Abst
                 return result;
             }
         } else {
+            if (!path.endsWith("/")) {
+                path = path + "/";
+            }
+            if (webAppMount.startsWith(path)) {
+                int i = webAppMount.indexOf('/', path.length());
+                if (i == -1) {
+                    return new String[] {webAppMount.substring(path.length())};
+                } else {
+                    return new String[] {
+                            webAppMount.substring(path.length(), i)};
+                }
+            }
             return EMPTY_STRING_ARRAY;
         }
     }
@@ -148,6 +160,18 @@ public class DirResourceSet extends Abst
                     }
                 }
             }
+        } else {
+            if (!path.endsWith("/")) {
+                path = path + "/";
+            }
+            if (webAppMount.startsWith(path)) {
+                int i = webAppMount.indexOf('/', path.length());
+                if (i == -1) {
+                    result.add(webAppMount + "/");
+                } else {
+                    result.add(webAppMount.substring(0, i + 1));
+                }
+            }
         }
         result.setLocked(true);
         return result;

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarResourceRoot.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarResourceRoot.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResourceRoot.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarResourceRoot.java Tue Jan  8 19:43:13 2013
@@ -31,12 +31,26 @@ public class JarResourceRoot extends Abs
 
     private final File base;
     private final String baseUrl;
+    private final String name;
 
     public JarResourceRoot(WebResourceRoot root, File base, String baseUrl,
             String webAppPath) {
         super(root, webAppPath);
         this.base = base;
         this.baseUrl = "jar:" + baseUrl;
+        // Extract the name from the webAppPath
+        // Strip any trailing '/' character
+        String resourceName;
+        if (webAppPath.endsWith("/")) {
+            resourceName = webAppPath.substring(0, webAppPath.length() - 1);
+        } else {
+            resourceName = webAppPath;
+        }
+        int i = resourceName.lastIndexOf('/');
+        if (i > -1) {
+            resourceName = resourceName.substring(i + 1);
+        }
+        name = resourceName;
     }
 
     @Override
@@ -71,7 +85,7 @@ public class JarResourceRoot extends Abs
 
     @Override
     public String getName() {
-        return "";
+        return name;
     }
 
     @Override

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java Tue Jan  8 19:43:13 2013
@@ -167,6 +167,19 @@ public class JarResourceSet extends Abst
                     }
                 }
             }
+        } else {
+            if (!path.endsWith("/")) {
+                path = path + "/";
+            }
+            if (webAppMount.startsWith(path)) {
+                int i = webAppMount.indexOf('/', path.length());
+                if (i == -1) {
+                    return new String[] {webAppMount.substring(path.length())};
+                } else {
+                    return new String[] {
+                            webAppMount.substring(path.length(), i)};
+                }
+            }
         }
         return result.toArray(new String[result.size()]);
     }
@@ -204,6 +217,18 @@ public class JarResourceSet extends Abst
                     }
                 }
             }
+        } else {
+            if (!path.endsWith("/")) {
+                path = path + "/";
+            }
+            if (webAppMount.startsWith(path)) {
+                int i = webAppMount.indexOf('/', path.length());
+                if (i == -1) {
+                    result.add(webAppMount + "/");
+                } else {
+                    result.add(webAppMount.substring(0, i + 1));
+                }
+            }
         }
         result.setLocked(true);
         return result;

Modified: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java (original)
+++ tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java Tue Jan  8 19:43:13 2013
@@ -67,7 +67,13 @@ public abstract class AbstractTestResour
     public final void testGetResourceRoot() {
         WebResource webResource = resourceRoot.getResource(getMount() + "/");
         Assert.assertTrue(webResource.isDirectory());
-        Assert.assertEquals("", webResource.getName());
+        String expected;
+        if (getMount().length() > 0) {
+            expected = getMount().substring(1);
+        } else {
+            expected = "";
+        }
+        Assert.assertEquals(expected, webResource.getName());
         Assert.assertEquals(getMount() + "/", webResource.getWebappPath());
     }
 

Modified: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java?rev=1430463&r1=1430462&r2=1430463&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java (original)
+++ tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSetMount.java Tue Jan  8 19:43:13 2013
@@ -44,14 +44,17 @@ public abstract class AbstractTestResour
         String[] results = resourceRoot.list("/");
 
         Assert.assertNotNull(results);
-        Assert.assertEquals(0, results.length);
+        Assert.assertEquals(1, results.length);
+        Assert.assertEquals(getMount().substring(1), results[0]);
     }
 
     @Test
     public final void testListWebAppPathsAbove() {
         Set<String> results = resourceRoot.listWebAppPaths("/");
 
-        Assert.assertNull(results);
+        Assert.assertNotNull(results);
+        Assert.assertEquals(1, results.size());
+        Assert.assertTrue(results.contains(getMount() + "/"));
     }
 
     @Test

Added: tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java?rev=1430463&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java (added)
+++ tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java Tue Jan  8 19:43:13 2013
@@ -0,0 +1,63 @@
+/*
+ * 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.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+
+import org.apache.catalina.WebResourceRoot;
+import org.apache.catalina.WebResourceSet;
+
+public class TestDirResourceSetVirtual extends TestDirResourceSet {
+
+    @Override
+    public WebResourceRoot getWebResourceRoot() {
+        File f = new File(getBaseDir());
+        TesterWebResourceRoot root = new TesterWebResourceRoot();
+        WebResourceSet webResourceSet =
+                new DirResourceSet(new TesterWebResourceRoot(),
+                        f.getAbsolutePath(), "/", "/");
+        root.setMainResources(webResourceSet);
+
+        WebResourceSet f1 = new FileResourceSet(root,
+                "test/webresources/dir1/f1.txt", "/f1.txt", "/");
+        root.addPreResources(f1);
+
+        WebResourceSet f2 = new FileResourceSet(root,
+                "test/webresources/dir1/f2.txt", "/f2.txt", "/");
+        root.addPreResources(f2);
+
+        WebResourceSet d1 = new DirResourceSet(root,
+                "test/webresources/dir1/d1", "/d1", "/");
+        root.addPreResources(d1);
+
+        WebResourceSet d2 = new DirResourceSet(root,
+                "test/webresources/dir1/d2", "/d2", "/");
+        root.addPreResources(d2);
+
+        return root;
+    }
+
+    @Override
+    protected boolean isWriteable() {
+        return true;
+    }
+
+    @Override
+    public String getBaseDir() {
+        return "test/webresources/dir3";
+    }
+}

Propchange: tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetVirtual.java
------------------------------------------------------------------------------
    svn:eol-style = native



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