sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-testing-resourceresolver-mock] 04/25: SLING-3889 Add optional support for namespace mangling and unmangling
Date Tue, 07 Nov 2017 10:20:43 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-1.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git

commit 62799ae335019aca310864d124b8c8310ef5e1cd
Author: Stefan Seifert <sseifert@apache.org>
AuthorDate: Wed Sep 17 13:59:50 2014 +0000

    SLING-3889 Add optional support for namespace mangling and unmangling
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1625594
13f79535-47bb-0310-9956-ffa450edef68
---
 .../resourceresolver/MockResourceResolver.java     | 47 +++++++++++--
 .../MockResourceResolverFactoryOptions.java        | 11 +++
 .../testing/resourceresolver/NamespaceMangler.java | 80 ++++++++++++++++++++++
 .../resourceresolver/NamespaceManglerTest.java     | 51 ++++++++++++++
 .../NamespaceManglingResourceResolverTest.java     | 56 +++++++++++++++
 5 files changed, 240 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index c79839c..1fee01f 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -18,6 +18,9 @@
  */
 package org.apache.sling.testing.resourceresolver;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -62,12 +65,30 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public Resource resolve(final HttpServletRequest request, final String absPath) {
-        return this.getResource(absPath);
+        String path = absPath;
+
+        // split off query string or fragment that may be appendend to the URL
+        String urlRemainder = null;
+        int urlRemainderPos = Math.min(path.indexOf('?'), path.indexOf('#'));
+        if (urlRemainderPos >= 0) {
+          urlRemainder = path.substring(urlRemainderPos);
+          path = path.substring(0, urlRemainderPos);
+        }
+        
+        // unmangle namespaces
+        if (options.isMangleNamespacePrefixes()) {
+            path = NamespaceMangler.unmangleNamespaces(path);
+        }
+
+        // build full path again
+        path = path + (urlRemainder != null ? urlRemainder : "");
+
+        return this.getResource(path);
     }
 
     @Override
     public Resource resolve(final String absPath) {
-        return this.getResource(absPath);
+        return resolve(null, absPath);
     }
 
     @Override
@@ -78,14 +99,30 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public String map(final String resourcePath) {
-        return resourcePath;
+        return map(null, resourcePath);
     }
 
     @Override
     public String map(final HttpServletRequest request, final String resourcePath) {
-        return resourcePath;
-    }
+        String path = resourcePath;
+
+        // split off query string or fragment that may be appendend to the URL
+        String urlRemainder = null;
+        int urlRemainderPos = Math.min(path.indexOf('?'), path.indexOf('#'));
+        if (urlRemainderPos >= 0) {
+          urlRemainder = path.substring(urlRemainderPos);
+          path = path.substring(0, urlRemainderPos);
+        }
+        
+        // mangle namespaces
+        if (options.isMangleNamespacePrefixes()) {
+            path = NamespaceMangler.mangleNamespaces(path);
+        }
 
+        // build full path again
+        return path + (urlRemainder != null ? urlRemainder : "");
+    }
+    
     @Override
     public Resource getResource(final String path) {
         Resource resource = getResourceInternal(path);
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
index 734d804..d173953 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
@@ -28,6 +28,8 @@ public class MockResourceResolverFactoryOptions {
     private EventAdmin eventAdmin;
 
     private String[] searchPaths = new String[] {"/apps/", "/libs/"};
+    
+    private boolean mangleNamespacePrefixes;
 
     public EventAdmin getEventAdmin() {
         return eventAdmin;
@@ -49,4 +51,13 @@ public class MockResourceResolverFactoryOptions {
         this.searchPaths = searchPaths;
         return this;
     }
+
+    public boolean isMangleNamespacePrefixes() {
+        return mangleNamespacePrefixes;
+    }
+
+    public void setMangleNamespacePrefixes(boolean mangleNamespacePrefixes) {
+        this.mangleNamespacePrefixes = mangleNamespacePrefixes;
+    }
+    
 }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java
b/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java
new file mode 100644
index 0000000..5348312
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java
@@ -0,0 +1,80 @@
+/*
+ * 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.sling.testing.resourceresolver;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+final class NamespaceMangler {
+
+    private static final String MANGLED_NAMESPACE_PREFIX = "_";
+    private static final String MANGLED_NAMESPACE_SUFFIX = "_";
+    private static final char NAMESPACE_SEPARATOR = ':';
+    private static final Pattern NAMESPACE_PATTERN = Pattern.compile("/([^:/]+):");
+    private static final Pattern MANGLED_NAMESPACE_PATTERN = Pattern.compile("/_([^_/]+)_");
+
+    private NamespaceMangler() {
+        // static methods only
+    }
+
+    /**
+     * Mangle the namespaces in the given path for usage in sling-based URLs.
+     * <p>
+     * Example: /path/jcr:content to /path/_jcr_content
+     * </p>
+     * @param path Path to mangle
+     * @return Mangled path
+     */
+    public static String mangleNamespaces(String path) {
+        if (path == null) {
+            return null;
+        }
+        Matcher matcher = NAMESPACE_PATTERN.matcher(path);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String replacement = "/" + MANGLED_NAMESPACE_PREFIX + matcher.group(1) + MANGLED_NAMESPACE_SUFFIX;
+            matcher.appendReplacement(sb, replacement);
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+    /**
+     * Unmangle the namespaces in the given path for usage in sling-based URLs.
+     * <p>
+     * Example: /path/_jcr_content to /path/jcr:content
+     * </p>
+     * @param path Path to unmangle
+     * @return Unmangled path
+     */
+    public static String unmangleNamespaces(String path) {
+        if (path == null) {
+            return null;
+        }
+        Matcher matcher = MANGLED_NAMESPACE_PATTERN.matcher(path);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String replacement = "/" + matcher.group(1) + NAMESPACE_SEPARATOR;
+            matcher.appendReplacement(sb, replacement);
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java
b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java
new file mode 100644
index 0000000..53d11bb
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.sling.testing.resourceresolver;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class NamespaceManglerTest {
+    
+    private Map<String, String> TEST_PATHS = ImmutableMap.<String, String>builder()
+            .put("/content/aa/bb/content.png", "/content/aa/bb/content.png")
+            .put("/content/aa/bb/jcr:content.png", "/content/aa/bb/_jcr_content.png")
+            .put("/content/aa/bb/jcr:content/anotherpath/xyz:abc", "/content/aa/bb/_jcr_content/anotherpath/_xyz_abc")
+            .build();
+
+    @Test
+    public void testMangleNamespaces() throws Exception {
+        for (Map.Entry<String, String> entry : TEST_PATHS.entrySet()) {
+            assertEquals(entry.getValue(), NamespaceMangler.mangleNamespaces(entry.getKey()));
+        }
+    }
+
+    @Test
+    public void testUnmangleNamespaces() throws Exception {
+        for (Map.Entry<String, String> entry : TEST_PATHS.entrySet()) {
+            assertEquals(entry.getKey(), NamespaceMangler.unmangleNamespaces(entry.getValue()));
+        }
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java
b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java
new file mode 100644
index 0000000..033db8a
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.sling.testing.resourceresolver;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Before;
+import org.junit.Test;
+
+public class NamespaceManglingResourceResolverTest {
+
+    private ResourceResolver resolver;
+    
+    @Before
+    public void setUp() throws Exception {
+        MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
+        options.setMangleNamespacePrefixes(true);
+        ResourceResolverFactory factory = new MockResourceResolverFactory(options);
+        resolver = factory.getResourceResolver(null);
+        
+        Resource res1 = resolver.create(resolver.getResource("/"), "res1", ValueMap.EMPTY);
+        Resource content = resolver.create(res1, "jcr:content", ValueMap.EMPTY);
+        resolver.create(content, "res2", ValueMap.EMPTY);
+    }
+    
+    @Test
+    public void testMap() {
+        assertEquals("/res1/_jcr_content/res2", resolver.map("/res1/jcr:content/res2"));
+    }
+    
+    @Test
+    public void testResolve() {
+        assertEquals("/res1/jcr:content/res2", resolver.resolve("/res1/_jcr_content/res2").getPath());
+    }
+    
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message