Search in sources :

Example 31 with Resource

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

the class ResourceAccessSecurityImplTests method testCannotUpdateAccidentallyUsingReadableResourceIfCannotUpdate.

@Test
public void testCannotUpdateAccidentallyUsingReadableResourceIfCannotUpdate() {
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    ValueMap resultValueMap = readableResource.adaptTo(ValueMap.class);
    resultValueMap.put("modified", "value");
    verify(valueMap, times(0)).put("modified", "value");
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 32 with Resource

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

the class ResourceAccessSecurityImplTests method testCannotUpdateUsingReadableResourceIfCannotRead.

@Test
public void testCannotUpdateUsingReadableResourceIfCannotRead() {
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    assertNull(readableResource);
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 33 with Resource

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

the class JcrNodeResourceIteratorTest method testRoot.

public void testRoot() throws RepositoryException {
    String path = "/child";
    Node node = new MockNode(path);
    NodeIterator ni = new MockNodeIterator(new Node[] { node });
    JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, "/", null, ni, getHelperData(), null);
    assertTrue(ri.hasNext());
    Resource res = ri.next();
    assertEquals(path, res.getPath());
    assertEquals(node.getPrimaryNodeType().getName(), res.getResourceType());
    assertFalse(ri.hasNext());
    try {
        ri.next();
        fail("Expected no element in the iterator");
    } catch (NoSuchElementException nsee) {
    // expected
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNode(org.apache.sling.commons.testing.jcr.MockNode) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) MockNode(org.apache.sling.commons.testing.jcr.MockNode) JcrNodeResourceIterator(org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 34 with Resource

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

the class JcrNodeResourceIteratorTest method testMulti.

public void testMulti() throws RepositoryException {
    int numNodes = 10;
    String pathBase = "/parent/path/node/";
    Node[] nodes = new Node[numNodes];
    for (int i = 0; i < nodes.length; i++) {
        nodes[i] = new MockNode(pathBase + i, "some:type" + i);
    }
    NodeIterator ni = new MockNodeIterator(nodes);
    JcrNodeResourceIterator ri = new JcrNodeResourceIterator(null, null, null, ni, getHelperData(), null);
    for (int i = 0; i < nodes.length; i++) {
        assertTrue(ri.hasNext());
        Resource res = ri.next();
        assertEquals(pathBase + i, res.getPath());
        assertEquals(nodes[i].getPrimaryNodeType().getName(), res.getResourceType());
    }
    assertFalse(ri.hasNext());
    try {
        ri.next();
        fail("Expected no element in the iterator");
    } catch (NoSuchElementException nsee) {
    // expected
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNodeIterator(org.apache.sling.commons.testing.jcr.MockNodeIterator) MockNode(org.apache.sling.commons.testing.jcr.MockNode) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) MockNode(org.apache.sling.commons.testing.jcr.MockNode) JcrNodeResourceIterator(org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 35 with Resource

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

the class MapEntries method getVanityPaths.

/**
     * get the vanity paths  Search for all nodes having a specific vanityPath
     */
private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
    Map<String, List<MapEntry>> entryMap = new HashMap<>();
    // sling:vanityPath (lowercase) is the property name
    final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM nt:base WHERE sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath).replaceAll("'", "''") + "' OR sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath.substring(1)).replaceAll("'", "''") + "' ORDER BY sling:vanityOrder DESC";
    ResourceResolver queryResolver = null;
    try {
        queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));
        final Iterator<Resource> i = queryResolver.findResources(queryString, "sql");
        while (i.hasNext()) {
            final Resource resource = i.next();
            boolean isValid = false;
            for (final Path sPath : this.factory.getObservationPaths()) {
                if (sPath.matches(resource.getPath())) {
                    isValid = true;
                    break;
                }
            }
            if (isValid) {
                if (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
                    loadVanityPath(resource, resolveMapsMap, vanityTargets, true, false);
                    entryMap = resolveMapsMap;
                } else {
                    final Map<String, List<String>> targetPaths = new HashMap<>();
                    loadVanityPath(resource, entryMap, targetPaths, true, false);
                }
            }
        }
    } catch (LoginException e) {
        log.error("Exception while obtaining queryResolver", e);
    } finally {
        if (queryResolver != null) {
            queryResolver.close();
        }
    }
    return entryMap;
}
Also used : Path(org.apache.sling.api.resource.path.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LoginException(org.apache.sling.api.resource.LoginException) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1569 Test (org.junit.Test)695 ValueMap (org.apache.sling.api.resource.ValueMap)338 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)260 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)164 HashMap (java.util.HashMap)160 Node (javax.jcr.Node)151 ArrayList (java.util.ArrayList)137 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)119 PersistenceException (org.apache.sling.api.resource.PersistenceException)114 Test (org.junit.jupiter.api.Test)72 Map (java.util.Map)70 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 LoginException (org.apache.sling.api.resource.LoginException)58 RepositoryException (javax.jcr.RepositoryException)54 Calendar (java.util.Calendar)50 Session (javax.jcr.Session)49