Author: sseifert Date: Wed Sep 17 13:59:50 2014 New Revision: 1625594 URL: http://svn.apache.org/r1625594 Log: SLING-3889 Add optional support for namespace mangling and unmangling
Added: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java (with props) sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java (with props) sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java (with props) Modified: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java Modified: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java?rev=1625594&r1=1625593&r2=1625594&view=diff ============================================================================== --- sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java (original) +++ sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java Wed Sep 17 13:59:50 2014 @@ -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 extend @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 extend @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); Modified: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java?rev=1625594&r1=1625593&r2=1625594&view=diff ============================================================================== --- sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java (original) +++ sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java Wed Sep 17 13:59:50 2014 @@ -28,6 +28,8 @@ public class MockResourceResolverFactory private EventAdmin eventAdmin; private String[] searchPaths = new String[] {"/apps/", "/libs/"}; + + private boolean mangleNamespacePrefixes; public EventAdmin getEventAdmin() { return eventAdmin; @@ -49,4 +51,13 @@ public class MockResourceResolverFactory this.searchPaths = searchPaths; return this; } + + public boolean isMangleNamespacePrefixes() { + return mangleNamespacePrefixes; + } + + public void setMangleNamespacePrefixes(boolean mangleNamespacePrefixes) { + this.mangleNamespacePrefixes = mangleNamespacePrefixes; + } + } Added: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java?rev=1625594&view=auto ============================================================================== --- sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java (added) +++ sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java Wed Sep 17 13:59:50 2014 @@ -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(); + } + +} Propchange: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java ------------------------------------------------------------------------------ --- svn:keywords (added) +++ svn:keywords Wed Sep 17 13:59:50 2014 @@ -0,0 +1 @@ +LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author Propchange: sling/trunk/testing/resourceresolver-mock/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java?rev=1625594&view=auto ============================================================================== --- sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java (added) +++ sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java Wed Sep 17 13:59:50 2014 @@ -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())); + } + } + +} Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java ------------------------------------------------------------------------------ --- svn:keywords (added) +++ svn:keywords Wed Sep 17 13:59:50 2014 @@ -0,0 +1 @@ +LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java?rev=1625594&view=auto ============================================================================== --- sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java (added) +++ sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java Wed Sep 17 13:59:50 2014 @@ -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()); + } + +} Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java ------------------------------------------------------------------------------ --- svn:keywords (added) +++ svn:keywords Wed Sep 17 13:59:50 2014 @@ -0,0 +1 @@ +LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author Propchange: sling/trunk/testing/resourceresolver-mock/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain