tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1547848 - in /tomcat/trunk: java/org/apache/catalina/webresources/ test/org/apache/catalina/webresources/
Date Wed, 04 Dec 2013 17:18:57 GMT
Author: markt
Date: Wed Dec  4 17:18:57 2013
New Revision: 1547848

URL: http://svn.apache.org/r1547848
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=55837
getRealPath() should return a result for resources that don't exist where possible
Add some unit tests to check this.

Modified:
    tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/EmptyResource.java
    tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
    tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java

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=1547848&r1=1547847&r2=1547848&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java Wed Dec  4 17:18:57
2013
@@ -92,10 +92,13 @@ public class DirResourceSet extends Abst
         String webAppMount = getWebAppMount();
         WebResourceRoot root = getRoot();
         if (path.startsWith(webAppMount)) {
-            File f = file(path.substring(webAppMount.length()), true);
+            File f = file(path.substring(webAppMount.length()), false);
             if (f == null) {
                 return new EmptyResource(root, path);
             }
+            if (!f.exists()) {
+                return new EmptyResource(root, path, f);
+            }
             if (f.isDirectory() && path.charAt(path.length() - 1) != '/') {
                 path = path += '/';
             }

Modified: tomcat/trunk/java/org/apache/catalina/webresources/EmptyResource.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/EmptyResource.java?rev=1547848&r1=1547847&r2=1547848&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/EmptyResource.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/EmptyResource.java Wed Dec  4 17:18:57
2013
@@ -16,6 +16,8 @@
  */
 package org.apache.catalina.webresources;
 
+import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.security.cert.Certificate;
@@ -28,10 +30,16 @@ public class EmptyResource implements We
 
     private final WebResourceRoot root;
     private final String webAppPath;
+    private final File file;
 
     public EmptyResource(WebResourceRoot root, String webAppPath) {
+        this(root, webAppPath, null);
+    }
+
+    public EmptyResource(WebResourceRoot root, String webAppPath, File file) {
         this.root = root;
         this.webAppPath = webAppPath;
+        this.file = file;
     }
 
     @Override
@@ -86,7 +94,15 @@ public class EmptyResource implements We
 
     @Override
     public String getCanonicalPath() {
-        return null;
+        if (file == null) {
+            return null;
+        } else {
+            try {
+                return file.getCanonicalPath();
+            } catch (IOException e) {
+                return null;
+            }
+        }
     }
 
     @Override

Modified: tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java?rev=1547848&r1=1547847&r2=1547848&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java Wed Dec  4 17:18:57
2013
@@ -239,6 +239,7 @@ public class StandardRoot extends Lifecy
             boolean useClassLoaderResources) {
         WebResource result = null;
         WebResource virtual = null;
+        WebResource mainEmpty = null;
         for (ArrayList<WebResourceSet> list : allResources) {
             for (WebResourceSet webResourceSet : list) {
                 if (useClassLoaderResources || !webResourceSet.getClassLoaderOnly()) {
@@ -246,8 +247,12 @@ public class StandardRoot extends Lifecy
                     if (result.exists()) {
                         return result;
                     }
-                    if (virtual == null && result.isVirtual()) {
-                        virtual = result;
+                    if (virtual == null) {
+                        if (result.isVirtual()) {
+                            virtual = result;
+                        } else if (main.equals(webResourceSet)) {
+                            mainEmpty = result;
+                        }
                     }
                 }
             }
@@ -259,7 +264,7 @@ public class StandardRoot extends Lifecy
         }
 
         // Default is empty resource in main resources
-        return new EmptyResource(this, path);
+        return mainEmpty;
     }
 
     @Override

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=1547848&r1=1547847&r2=1547848&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java (original)
+++ tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestResourceSet.java Wed Dec
 4 17:18:57 2013
@@ -19,6 +19,7 @@ package org.apache.catalina.webresources
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -374,6 +375,43 @@ public abstract class AbstractTestResour
         }
     }
 
+    // ------------------------------------------------------ getCanonicalPath()
+
+    @Test
+    public final void testGetCanonicalPathExists() {
+        WebResource exists =
+                resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
+        String existsCanonicalPath = exists.getCanonicalPath();
+
+        URL existsUrl = exists.getURL();
+        if ("file".equals(existsUrl.getProtocol())) {
+            // Should have a canonical path
+            Assert.assertNotNull(existsCanonicalPath);
+        } else {
+            Assert.assertNull(existsCanonicalPath);
+        }
+    }
+
+    @Test
+    public final void testGetCanonicalPathDoesNotExist() {
+        WebResource exists =
+                resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
+        WebResource doesNotExist =
+                resourceRoot.getResource(getMount() + "/d1/dummy.txt");
+        String doesNotExistCanonicalPath = doesNotExist.getCanonicalPath();
+
+        URL existsUrl = exists.getURL();
+        if ("file".equals(existsUrl.getProtocol())) {
+            // Should be possible to construct a canonical path for a resource
+            // that doesn't exist given that a resource that does exist in the
+            // same directory has a URL with the file protocol
+            Assert.assertNotNull(doesNotExistCanonicalPath);
+        } else {
+            Assert.assertNull(doesNotExistCanonicalPath);
+        }
+    }
+
+
     // ------------------------------------------------------------ constructors
 
     public abstract void testNoArgConstructor();



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


Mime
View raw message