You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/02/02 10:05:39 UTC

[jackrabbit-filevault] branch master updated: JCRVLT-500 - support quoted pattern to decide absolute filter (#121)

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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git


The following commit(s) were added to refs/heads/master by this push:
     new f688495  JCRVLT-500 - support quoted pattern to decide absolute filter (#121)
f688495 is described below

commit f688495999147fb6438ec091e58b58aeba5e1365
Author: Timothee Maret <tm...@apache.org>
AuthorDate: Tue Feb 2 11:05:32 2021 +0100

    JCRVLT-500 - support quoted pattern to decide absolute filter (#121)
---
 .../vault/fs/filter/DefaultPathFilter.java         |   8 +-
 .../vault/fs/filter/DefaultPathFilterTest.java     |  18 ++-
 .../integration/TestExportWithQuotedPattern.java   | 126 +++++++++++++++++++++
 3 files changed, 147 insertions(+), 5 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilter.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilter.java
index 74fd43b..a0d6cad 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilter.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilter.java
@@ -97,7 +97,8 @@ public class DefaultPathFilter implements PathFilter {
      */
     @Override
     public boolean isAbsolute() {
-        return regex.pattern().startsWith("/");
+        String pattern = regex.pattern();
+        return pattern.startsWith("/") || pattern.startsWith("\\Q/");
     }
 
     /**
@@ -108,12 +109,11 @@ public class DefaultPathFilter implements PathFilter {
         if (mapping == null) {
             return this;
         }
-        String pattern = regex.pattern();
-        if (!pattern.startsWith("/")) {
+        if (!isAbsolute()) {
             return this;
         }
         try {
-            return new DefaultPathFilter(mapping.map(pattern));
+            return new DefaultPathFilter(mapping.map(regex.pattern()));
         } catch (ConfigurationException e) {
             // should not happen as pattern is always valid
             return this;
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilterTest.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilterTest.java
index 3a99259..19e6750 100644
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilterTest.java
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/fs/filter/DefaultPathFilterTest.java
@@ -17,7 +17,10 @@
 
 package org.apache.jackrabbit.vault.fs.filter;
 
+import java.util.regex.Pattern;
+
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.jackrabbit.vault.fs.config.ConfigurationException;
 import org.junit.Test;
@@ -66,11 +69,24 @@ public class DefaultPathFilterTest {
         test("/foo(/.*)?", "/foobar/foo", false);
     }
 
+    @Test
+    public void testQuotedPattern() throws ConfigurationException {
+        String path = "/some(loaded/path";
+        test(Pattern.quote(path), path, true);
+    }
+
+    @Test
+    public void testAbsoluteQuotedPattern() throws ConfigurationException {
+        String path = "/some(loaded/path";
+        assertTrue(new DefaultPathFilter(Pattern.quote(path)).isAbsolute());
+    }
+
+
     @Test(expected = ConfigurationException.class)
     public void testInvalidPattern() throws ConfigurationException {
         new DefaultPathFilter("[");
     }
-    
+
     private void test(String pattern, String path, boolean result) throws ConfigurationException {
         DefaultPathFilter f = new DefaultPathFilter(pattern);
         assertEquals("Pattern '" + pattern + "' matches '" + path + "'", result, f.matches(path));
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestExportWithQuotedPattern.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestExportWithQuotedPattern.java
new file mode 100644
index 0000000..74ba4ce
--- /dev/null
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestExportWithQuotedPattern.java
@@ -0,0 +1,126 @@
+/*
+ * 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.jackrabbit.vault.packaging.integration;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
+import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
+import org.apache.jackrabbit.vault.fs.config.ConfigurationException;
+import org.apache.jackrabbit.vault.fs.config.DefaultMetaInf;
+import org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter;
+import org.apache.jackrabbit.vault.fs.filter.DefaultPathFilter;
+import org.apache.jackrabbit.vault.packaging.ExportOptions;
+import org.apache.jackrabbit.vault.packaging.PackageException;
+import org.apache.jackrabbit.vault.packaging.VaultPackage;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TestExportWithQuotedPattern extends IntegrationTestBase {
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        setupContent(admin);
+        admin.save();
+    }
+
+    private static final String NAME = "f(o";
+
+    @Test
+    public void quotedPattern() throws IOException, RepositoryException, PackageException, ConfigurationException {
+        String path = "/tmp/" + NAME;
+
+        PathFilterSet nodes = new PathFilterSet(path);
+        nodes.addInclude(new DefaultPathFilter(quote(path)));
+
+        DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
+        filter.add(nodes);
+
+        // export and extract
+        File pkgFile = assemblePackage(filter);
+
+        clean(path);
+        try (VaultPackage vp = packMgr.open(pkgFile)) {
+            vp.extract(admin, getDefaultOptions());
+            // validate the extracted content
+            assertNodeExists(path);
+            assertNodeMissing(path + "/foo");
+            assertNodeMissing(path + "/bar");
+        } finally {
+            pkgFile.delete();
+        }
+    }
+
+    private String quote(String path) {
+        if (path == null) {
+            return null;
+        } else if (path.startsWith("/")) {
+            return "/" + Pattern.quote(path.substring(1));
+        } else {
+            return Pattern.quote(path);
+        }
+    }
+
+    private File assemblePackage(WorkspaceFilter filter)
+            throws IOException, RepositoryException {
+
+        File tmpFile = File.createTempFile("vaulttest", ".zip");
+
+        ExportOptions options = new ExportOptions();
+        DefaultMetaInf meta = new DefaultMetaInf();
+        meta.setFilter(filter);
+
+        Properties props = new Properties();
+        props.setProperty(VaultPackage.NAME_GROUP, "jackrabbit/test");
+        props.setProperty(VaultPackage.NAME_NAME, "quoted-pattern-export-package");
+        meta.setProperties(props);
+
+        options.setMetaInf(meta);
+
+        packMgr.assemble(admin, options, tmpFile).close();
+        return tmpFile;
+    }
+
+    private void setupContent(Session session)
+            throws RepositoryException {
+
+        Node root = session.getRootNode();
+        Node tmp = root.addNode("tmp", "nt:folder");
+        Node foo = tmp.addNode(NAME, "nt:folder");
+        Node bar = foo.addNode("bar", "nt:folder");
+        Node zoo = foo.addNode("zoo", "nt:folder");
+    }
+
+    public void assertNodeExists(String path) throws RepositoryException {
+        assertTrue(path + " should exist", admin.nodeExists(path));
+    }
+
+    public void assertNodeMissing(String path) throws RepositoryException {
+        assertFalse(path + " should not exist", admin.nodeExists(path));
+    }
+}