Search in sources :

Example 16 with NonExistingResource

use of org.apache.sling.api.resource.NonExistingResource in project sling by apache.

the class MockResourceResolver method resolve.

@Override
public Resource resolve(final HttpServletRequest request, final String absPath) {
    String path = absPath;
    if (path == null) {
        path = "/";
    }
    // 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 : "");
    Resource resource = this.getResource(path);
    if (resource == null) {
        resource = new NonExistingResource(this, absPath);
    }
    return resource;
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource)

Example 17 with NonExistingResource

use of org.apache.sling.api.resource.NonExistingResource in project sling by apache.

the class SlingCrudResourceResolverTest method testResolveNonexistingResource.

@Test
public void testResolveNonexistingResource() {
    Resource resource = resourceResolver.resolve("/non/existing/path");
    assertTrue(resource instanceof NonExistingResource);
    assertEquals("/non/existing/path", resource.getPath());
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 18 with NonExistingResource

use of org.apache.sling.api.resource.NonExistingResource in project sling by apache.

the class ResourceResolverTest method test_resolve_with_sling_alias_limited_access.

@Test
public void test_resolve_with_sling_alias_limited_access() throws Exception {
    Principal testUserPrincipal = new Principal() {

        @Override
        public String getName() {
            return "testuser";
        }
    };
    AccessControlUtil.getUserManager(session).createUser("testuser", "password", testUserPrincipal, null);
    Node child = rootNode.addNode("child");
    Node grandChild = child.addNode("grandChild");
    grandChild.setProperty("sling:alias", "enkel");
    saveMappings(session);
    session.save();
    //deny jcr:all on /
    AccessControlUtil.replaceAccessControlEntry(session, "/", testUserPrincipal, null, new String[] { "jcr:all" }, null, "last");
    //grant read on /content/child
    AccessControlUtil.replaceAccessControlEntry(session, child.getPath(), testUserPrincipal, new String[] { "jcr:read" }, null, null, "last");
    session.save();
    try {
        final Map<String, Object> authInfo = new HashMap<String, Object>();
        authInfo.put(ResourceResolverFactory.USER, "testuser");
        authInfo.put(ResourceResolverFactory.PASSWORD, "password".toCharArray());
        ResourceResolver testUserResolver = resourceResolverFactory.getResourceResolver(authInfo);
        try {
            //testing map
            String path = grandChild.getPath();
            String mapped = testUserResolver.map(path);
            assertEquals("/child/enkel", mapped);
            //testing resolve
            path = grandChild.getPath();
            Resource res = testUserResolver.resolve(null, path);
            assertNotNull(res);
            assertFalse(res instanceof NonExistingResource);
            assertEquals(path, res.getPath());
            path = child.getPath() + "/enkel";
            res = testUserResolver.resolve(null, path);
            assertNotNull(res);
            assertFalse(res instanceof NonExistingResource);
            assertEquals(grandChild.getPath(), res.getPath());
        } finally {
            if (testUserResolver != null && testUserResolver.isLive()) {
                testUserResolver.close();
            }
        }
    } finally {
        removeAce(session, testUserPrincipal, "/");
        child.remove();
        Authorizable authorizable = AccessControlUtil.getUserManager(session).getAuthorizable("testuser");
        authorizable.remove();
        session.save();
    }
}
Also used : HashMap(java.util.HashMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) Principal(java.security.Principal) Test(org.junit.Test)

Example 19 with NonExistingResource

use of org.apache.sling.api.resource.NonExistingResource in project sling by apache.

the class ResourceResolverWithVanityBloomFilterTest method test_resolve_with_sling_alias_multi_value.

/*@Test public void test_resolve_with_sling_alias_limited_access() throws Exception {
        Principal testUserPrincipal = AccessControlUtil.getPrincipalManager(session).getPrincipal("testuser");

        Node child = rootNode.addNode("child");
        child.setProperty("sling:alias", "kind");
        AccessControlUtil.replaceAccessControlEntry(session, child.getPath(), testUserPrincipal, null, new String[] {"jcr:all"}, null, "last");
        session.save();
try {
        Session testUserSession = getRepository().login(new SimpleCredentials("testuser", "test".toCharArray()));
        final Map<String, Object> authInfo = new HashMap<String, Object>();
        authInfo.put(ResourceResolverFactory.USER, "admin");
        authInfo.put(ResourceResolverFactory.PASSWORD, "admin".toCharArray());
        authInfo.put("testAttributeString", "AStringValue");
        authInfo.put("testAttributeNumber", 999);
        ResourceResolver testUserResolver = resFac.getResourceResolver(authInfo);

        try {
            // expect child due to the aliased not not being visible and no parent
            // due to mapping the rootPath onto root
            String path = "/child";
            String mapped = testUserResolver.map(child.getPath());
            assertEquals(path, mapped);

            Resource res = testUserResolver.resolve(null, path);
            assertNotNull(res);
            assertTrue(res instanceof NonExistingResource);
            assertEquals("/child",
                res.getResourceMetadata().getResolutionPath());
            // TODO - is this correct?
            assertEquals(null, res.getResourceMetadata().getResolutionPathInfo());

            // second level alias
            Node grandchild = child.addNode("grandchild");
            grandchild.setProperty("sling:alias", "enkel");
            AccessControlUtil.replaceAccessControlEntry(session, grandchild.getPath(), testUserPrincipal, new String[] { "jcr:all" }, null, null, "first");
            session.save();

            // expect /child/enkel due to parent node not being
            // visible to the test user and no parent due to mapping
            // the rootPath onto root
            String pathEnkel = "/child/enkel";
            String mappedEnkel = testUserResolver.map(grandchild.getPath());
            assertEquals(pathEnkel, mappedEnkel);*/
//TODO already commented
/*
            Resource resEnkel = testUserResolver.resolve(null, pathEnkel);
            assertNotNull(resEnkel);
            assertEquals(rootNode.getPath() + "/kind/enkel",
                resEnkel.getResourceMetadata().getResolutionPath());
            assertEquals("", resEnkel.getResourceMetadata().getResolutionPathInfo());

            Node resNodeEnkel = resEnkel.adaptTo(Node.class);
            assertNotNull(resNodeEnkel);
            assertEquals(grandchild.getPath(), resNodeEnkel.getPath());
            */
/*} finally {
            testUserSession.logout();
        }
        } finally {
            child.remove();
            session.save();
        }
    }*/
@Test
public void test_resolve_with_sling_alias_multi_value() throws Exception {
    Node child = rootNode.addNode("child");
    child.setProperty("sling:alias", new String[] { "kind", "enfant" });
    try {
        saveMappings(session);
        // expect kind due to alias and no parent due to mapping
        // the rootPath onto root
        String path = "/kind";
        String mapped = resResolver.map(child.getPath());
        assertEquals(path, mapped);
        Resource res = resResolver.resolve(null, path);
        assertNotNull(res);
        assertEquals(rootNode.getPath() + "/kind", res.getResourceMetadata().getResolutionPath());
        assertEquals("", res.getResourceMetadata().getResolutionPathInfo());
        Node resNode = res.adaptTo(Node.class);
        assertNotNull(resNode);
        assertEquals(child.getPath(), resNode.getPath());
        // expect enfant due to alias and no parent due to mapping
        // the rootPath onto root
        String pathEnfant = "/enfant";
        String mappedEnfant = resResolver.map(child.getPath());
        // map always selects first alias
        assertEquals(path, mappedEnfant);
        Resource resEnfant = resResolver.resolve(null, pathEnfant);
        assertNotNull(resEnfant);
        assertEquals(rootNode.getPath() + "/enfant", resEnfant.getResourceMetadata().getResolutionPath());
        assertEquals("", resEnfant.getResourceMetadata().getResolutionPathInfo());
        Node resNodeEnfant = resEnfant.adaptTo(Node.class);
        assertNotNull(resNodeEnfant);
        assertEquals(child.getPath(), resNodeEnfant.getPath());
        // second level alias
        Node grandchild = child.addNode("grandchild");
        grandchild.setProperty("sling:alias", "enkel");
        saveMappings(session);
        // expect kind/enkel due to alias and no parent due to mapping
        // the rootPath onto root
        String pathEnkel = "/kind/enkel";
        String mappedEnkel = resResolver.map(grandchild.getPath());
        assertEquals(pathEnkel, mappedEnkel);
        Resource resEnkel = resResolver.resolve(null, pathEnkel);
        assertNotNull(resEnkel);
        assertEquals(rootNode.getPath() + "/kind/enkel", resEnkel.getResourceMetadata().getResolutionPath());
        assertEquals("", resEnkel.getResourceMetadata().getResolutionPathInfo());
        Node resNodeEnkel = resEnkel.adaptTo(Node.class);
        assertNotNull(resNodeEnkel);
        assertEquals(grandchild.getPath(), resNodeEnkel.getPath());
        // expect kind/enkel due to alias and no parent due to mapping
        // the rootPath onto root
        String pathEnfantEnkel = "/enfant/enkel";
        String mappedEnfantEnkel = resResolver.map(grandchild.getPath());
        // map always selects first alias
        assertEquals(pathEnkel, mappedEnfantEnkel);
        Resource resEnfantEnkel = resResolver.resolve(null, pathEnfantEnkel);
        assertNotNull(resEnfantEnkel);
        assertEquals(rootNode.getPath() + "/enfant/enkel", resEnfantEnkel.getResourceMetadata().getResolutionPath());
        assertEquals("", resEnfantEnkel.getResourceMetadata().getResolutionPathInfo());
        Node resNodeEnfantEnkel = resEnfantEnkel.adaptTo(Node.class);
        assertNotNull(resNodeEnfantEnkel);
        assertEquals(grandchild.getPath(), resNodeEnfantEnkel.getPath());
    } finally {
        child.remove();
        session.save();
    }
}
Also used : Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 20 with NonExistingResource

use of org.apache.sling.api.resource.NonExistingResource in project sling by apache.

the class ResourceResolverWithVanityBloomFilterTest method testResolveRemovedMixinVanityPath.

//see SLING-3558
@Ignore
@Test
public void testResolveRemovedMixinVanityPath() throws Exception {
    Node childNode = null;
    try {
        //create new child with vanity path without mixin
        childNode = maybeCreateNode(rootNode, "rootChild", "nt:unstructured");
        childNode.setProperty("sling:vanityPath", "childVanity");
        saveMappings(session);
        String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        HttpServletRequest request = new FakeSlingHttpServletRequest(path);
        Resource res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertTrue(res instanceof NonExistingResource);
        assertEquals("/childVanity.print.html", res.getPath());
        //add mixin
        childNode.addMixin("sling:VanityPath");
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode.getPath(), res.getPath());
        assertEquals(childNode.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(childNode.isSame(res.adaptTo(Node.class)));
        //remove mixin  
        childNode.removeMixin("sling:VanityPath");
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertTrue(res instanceof NonExistingResource);
        assertEquals("/childVanity.print.html", res.getPath());
    } finally {
        if (childNode != null) {
            childNode.remove();
            saveMappings(session);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

NonExistingResource (org.apache.sling.api.resource.NonExistingResource)24 Resource (org.apache.sling.api.resource.Resource)22 Test (org.junit.Test)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Node (javax.jcr.Node)10 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ValidationResult (org.apache.sling.validation.ValidationResult)3 ChildResource (org.apache.sling.validation.model.ChildResource)3 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)3 MergedResource (org.apache.sling.resourcemerger.impl.MergedResource)2 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)2 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)2 ValidationModel (org.apache.sling.validation.model.ValidationModel)2 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)2 Expectations (org.jmock.Expectations)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1