You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pa...@apache.org on 2020/12/15 22:47:35 UTC

[felix-dev] branch issues/FELIX-6294 created (now af8cf08)

This is an automated email from the ASF dual-hosted git repository.

pauls pushed a change to branch issues/FELIX-6294
in repository https://gitbox.apache.org/repos/asf/felix-dev.git.


      at af8cf08  FELIX-6294: add a slash to urls for directories

This branch includes the following new commits:

     new af8cf08  FELIX-6294: add a slash to urls for directories

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[felix-dev] 01/01: FELIX-6294: add a slash to urls for directories

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch issues/FELIX-6294
in repository https://gitbox.apache.org/repos/asf/felix-dev.git

commit af8cf08fddc8fe43343cd2122deb9aab40330323
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Tue Dec 15 23:47:17 2020 +0100

    FELIX-6294: add a slash to urls for directories
---
 .../apache/felix/framework/BundleRevisionImpl.java | 12 ++++++
 .../apache/felix/framework/ExtensionManager.java   |  9 ++++-
 .../framework/cache/ConnectContentContent.java     |  6 +++
 .../org/apache/felix/framework/cache/Content.java  | 12 ++++++
 .../framework/cache/ContentDirectoryContent.java   |  8 ++++
 .../felix/framework/cache/DirectoryContent.java    | 12 ++++++
 .../apache/felix/framework/cache/JarContent.java   | 16 +++++++-
 .../felix/framework/util/MultiReleaseContent.java  |  6 +++
 .../felix/framework/ResourceLoadingTest.java       | 44 +++++++++++++++++++++-
 9 files changed, 122 insertions(+), 3 deletions(-)

diff --git a/framework/src/main/java/org/apache/felix/framework/BundleRevisionImpl.java b/framework/src/main/java/org/apache/felix/framework/BundleRevisionImpl.java
index 5aa9daa..388e426 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleRevisionImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleRevisionImpl.java
@@ -514,6 +514,10 @@ public class BundleRevisionImpl implements BundleRevision, Resource
         {
             if (contentPath.get(i).hasEntry(name))
             {
+                if (!name.endsWith("/") && contentPath.get(i).isDirectory(name))
+                {
+                    name += "/";
+                }
                 url = createURL(i + 1, name);
             }
         }
@@ -552,6 +556,10 @@ public class BundleRevisionImpl implements BundleRevision, Resource
             {
                 if (contentPath.get(i).hasEntry(name))
                 {
+                    if (!name.endsWith("/") && contentPath.get(i).isDirectory(name))
+                    {
+                        name += "/";
+                    }
                     // Use the class path index + 1 for creating the path so
                     // that we can differentiate between module content URLs
                     // (where the path will start with 0) and module class
@@ -588,6 +596,10 @@ public class BundleRevisionImpl implements BundleRevision, Resource
             // Check the module content.
             if (getContent().hasEntry(name))
             {
+                if (!name.endsWith("/") && getContent().isDirectory(name))
+                {
+                    name += "/";
+                }
                 // Module content URLs start with 0, whereas module
                 // class path URLs start with the index into the class
                 // path + 1.
diff --git a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
index 5ed9955..5f0ee34 100644
--- a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
+++ b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
@@ -869,7 +869,14 @@ class ExtensionManager implements Content
         };
     }
 
-    public boolean hasEntry(String name) {
+    public boolean hasEntry(String name)
+    {
+        return false;
+    }
+
+    @Override
+    public boolean isDirectory(String name)
+    {
         return false;
     }
 
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/ConnectContentContent.java b/framework/src/main/java/org/apache/felix/framework/cache/ConnectContentContent.java
index 88e6dec..a37c598 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/ConnectContentContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/ConnectContentContent.java
@@ -68,6 +68,12 @@ public class ConnectContentContent implements Content
     }
 
     @Override
+    public boolean isDirectory(String name)
+    {
+        return m_content.getEntry(name).map(entry -> entry.getName().endsWith("/")).orElse(false);
+    }
+
+    @Override
     public Enumeration<String> getEntries()
     {
         try
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/Content.java b/framework/src/main/java/org/apache/felix/framework/cache/Content.java
index 79580c9..4ec2ecd 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/Content.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/Content.java
@@ -49,6 +49,18 @@ public interface Content
 
     /**
      * <p>
+     * This method determines if the specified named entry is contained in
+     * the associated content and is a directory. The entry name is a relative path with '/'
+     * separators.
+     * </p>
+     * @param name The name of the entry to find.
+     * @return <tt>true</tt> if a corresponding entry was found and is a directory, <tt>false</tt>
+     *         otherwise.
+     **/
+    boolean isDirectory(String name);
+
+    /**
+     * <p>
      * Returns an enumeration of entry names as <tt>String</tt> objects.
      * An entry name is a path constructed with '/' as path element
      * separators and is relative to the root of the content. Entry names
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java b/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
index e1f04ac..1673270 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/ContentDirectoryContent.java
@@ -52,6 +52,14 @@ public class ContentDirectoryContent implements Content
         return m_content.hasEntry(m_rootPath + name);
     }
 
+    @Override
+    public boolean isDirectory(String name)
+    {
+        name = getName(name);
+
+        return m_content.isDirectory(m_rootPath + name);
+    }
+
     public Enumeration<String> getEntries()
     {
         Enumeration<String> result = new EntriesEnumeration(m_content.getEntries(), m_rootPath);
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
index 0240933..c462b55 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
@@ -83,6 +83,18 @@ public class DirectoryContent implements Content
                 ? BundleCache.getSecureAction().isFileDirectory(file) : true);
     }
 
+    @Override
+    public boolean isDirectory(String name)
+    {
+        name = getName(name);
+
+        // Return true if the file associated with the entry exists,
+        // unless the entry name ends with "/", in which case only
+        // return true if the file is really a directory.
+        File file = new File(m_dir, name);
+        return BundleCache.getSecureAction().isFileDirectory(file);
+    }
+
     public Enumeration<String> getEntries()
     {
         // Wrap entries enumeration to filter non-matching entries.
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java b/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
index 79c2249..fd1dda0 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/JarContent.java
@@ -101,7 +101,7 @@ public class JarContent implements Content
         }
     }
 
-    public boolean hasEntry(String name) throws IllegalStateException
+    public boolean hasEntry(String name)
     {
         try
         {
@@ -114,6 +114,20 @@ public class JarContent implements Content
         }
     }
 
+    @Override
+    public boolean isDirectory(String name)
+    {
+        try
+        {
+            ZipEntry ze = m_zipFile.getEntry(name);
+            return ze != null && ze.isDirectory();
+        }
+        catch (Exception ex)
+        {
+            return false;
+        }
+    }
+
     public Enumeration<String> getEntries()
     {
         // Wrap entries enumeration to filter non-matching entries.
diff --git a/framework/src/main/java/org/apache/felix/framework/util/MultiReleaseContent.java b/framework/src/main/java/org/apache/felix/framework/util/MultiReleaseContent.java
index 29a5e30..fa1b48d 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/MultiReleaseContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/MultiReleaseContent.java
@@ -88,6 +88,12 @@ public class MultiReleaseContent implements Content
     }
 
     @Override
+    public boolean isDirectory(String name)
+    {
+        return m_content.isDirectory(findPath(name));
+    }
+
+    @Override
     public Enumeration<String> getEntries()
     {
         Enumeration<String> entries = m_content.getEntries();
diff --git a/framework/src/test/java/org/apache/felix/framework/ResourceLoadingTest.java b/framework/src/test/java/org/apache/felix/framework/ResourceLoadingTest.java
index a89e430..19d6d07 100644
--- a/framework/src/test/java/org/apache/felix/framework/ResourceLoadingTest.java
+++ b/framework/src/test/java/org/apache/felix/framework/ResourceLoadingTest.java
@@ -79,7 +79,8 @@ public class ResourceLoadingTest extends TestCase
         cacheDir = null;
     }
 
-    public void testResourceLoadingWithHash() throws Exception {
+    public void testResourceLoadingWithHash() throws Exception
+    {
         String bmf = "Bundle-SymbolicName: cap.bundle\n"
             + "Bundle-Version: 1.2.3.Blah\n"
             + "Bundle-ManifestVersion: 2\n"
@@ -132,6 +133,47 @@ public class ResourceLoadingTest extends TestCase
         }
     }
 
+    public void testResourceLoadingWithDirectory() throws Exception
+    {
+        String bmf = "Bundle-SymbolicName: cap.bundle\n"
+                + "Bundle-Version: 1.2.3.Blah\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Import-Package: org.osgi.framework\n";
+        File bundleFile = File.createTempFile("felix-bundle", ".jar", tempDir);
+
+        Manifest mf = new Manifest(new ByteArrayInputStream(bmf.getBytes("utf-8")));
+        mf.getMainAttributes().putValue("Manifest-Version", "1.0");
+        JarOutputStream os = new JarOutputStream(new FileOutputStream(bundleFile), mf);
+
+        String name = "bla/bli/blub";
+        os.putNextEntry(new ZipEntry("bla/"));
+        os.putNextEntry(new ZipEntry("bla/bli/"));
+        os.putNextEntry(new ZipEntry(name));
+        os.write("This is a Test".getBytes());
+        os.close();
+
+        Bundle testBundle = felix.getBundleContext().installBundle(bundleFile.toURI().toASCIIString());
+
+        testBundle.start();
+
+        assertEquals(Bundle.ACTIVE, testBundle.getState());
+        assertTrue(testBundle.getResource("bla").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getEntry("bla").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.adapt(BundleWiring.class).getClassLoader().getResource("bla").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getResource("bla/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getEntry("bla/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.adapt(BundleWiring.class).getClassLoader().getResource("bla/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getResource("bla/bli").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getEntry("bla/bli").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.adapt(BundleWiring.class).getClassLoader().getResource("bla/bli").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getResource("bla/bli/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getEntry("bla/bli/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.adapt(BundleWiring.class).getClassLoader().getResource("bla/bli/").toExternalForm().endsWith("/"));
+        assertTrue(testBundle.getResource("bla/bli/blub").toExternalForm().endsWith("/blub"));
+        assertTrue(testBundle.getEntry("bla/bli/blub").toExternalForm().endsWith("/blub"));
+        assertTrue(testBundle.adapt(BundleWiring.class).getClassLoader().getResource("bla/bli/blub").toExternalForm().endsWith("/blub"));
+    }
+
     private static void deleteDir(File root) throws IOException
     {
         if (root.isDirectory())