Search in sources :

Example 16 with ResourceResolver

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

the class ResourceEliminateDuplicatesIteratorTest method testIterator.

@SuppressWarnings("unchecked")
@Test
public void testIterator() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a/b"), "/conf/a/b", 0), new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a"), null, 0), new ContextResource(rr.getResource("/content/a/b/c"), "/conf/a/b", 0));
    Iterator<Resource> result = toResourceIterator(new ResourceEliminateDuplicatesIterator(list.iterator()));
    assertThat(result, ResourceIteratorMatchers.paths("/content/a", "/content/a/b", "/content/a", "/content/a/b/c"));
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 17 with ResourceResolver

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

the class ResourcePathCollatingIteratorTest method testWithConfigRefAndServiceRanking.

@SuppressWarnings("unchecked")
@Test
public void testWithConfigRefAndServiceRanking() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 500));
    List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 100));
    Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
    ContextResource item1 = result.next();
    ContextResource item2 = result.next();
    assertFalse(result.hasNext());
    assertEquals("/content/a", item1.getResource().getPath());
    assertEquals("/conf/z", item1.getConfigRef());
    assertEquals("/content/a", item2.getResource().getPath());
    assertEquals("/conf/a", item2.getConfigRef());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 18 with ResourceResolver

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

the class ResourcePathCollatingIteratorTest method testWithConfigRef.

@SuppressWarnings("unchecked")
@Test
public void testWithConfigRef() {
    context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
    ResourceResolver rr = context.resourceResolver();
    List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 0));
    List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0));
    Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
    ContextResource item1 = result.next();
    ContextResource item2 = result.next();
    assertFalse(result.hasNext());
    assertEquals("/content/a", item1.getResource().getPath());
    assertEquals("/conf/a", item1.getConfigRef());
    assertEquals("/content/a", item2.getResource().getPath());
    assertEquals("/conf/z", item2.getConfigRef());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContextResource(org.apache.sling.caconfig.resource.spi.ContextResource) Test(org.junit.Test)

Example 19 with ResourceResolver

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

the class SyncTokenService method storeMySyncToken.

private boolean storeMySyncToken(String syncTokenId) {
    logger.trace("storeMySyncToken: start");
    if (slingId == null) {
        logger.info("storeMySyncToken: not yet activated (slingId is null)");
        return false;
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        final Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
        ModifiableValueMap syncTokens = resource.adaptTo(ModifiableValueMap.class);
        boolean updateToken = false;
        if (!syncTokens.containsKey(slingId)) {
            updateToken = true;
        } else {
            Object existingToken = syncTokens.get(slingId);
            if (existingToken == null || !existingToken.equals(syncTokenId)) {
                updateToken = true;
            }
        }
        if (updateToken) {
            syncTokens.put(slingId, syncTokenId);
            resourceResolver.commit();
            logger.info("storeMySyncToken: stored syncToken of slingId=" + slingId + " as=" + syncTokenId);
        } else {
            logger.info("storeMySyncToken: syncToken was left unchanged for slingId=" + slingId + " at=" + syncTokenId);
        }
        return true;
    } catch (LoginException e) {
        logger.error("storeMySyncToken: could not login for storing my syncToken: " + e, e);
        return false;
    } catch (PersistenceException e) {
        logger.error("storeMySyncToken: got PersistenceException while storing my syncToken: " + e, e);
        return false;
    } finally {
        logger.trace("storeMySyncToken: end");
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 20 with ResourceResolver

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

the class VotingHandler method handleEvent.

/**
     * handle repository changes and react to ongoing votings
     */
@Override
public void handleEvent(final Event event) {
    if (!activated) {
        return;
    }
    String resourcePath = (String) event.getProperty("path");
    String ongoingVotingsPath = config.getOngoingVotingsPath();
    if (resourcePath == null) {
        // not of my business
        return;
    }
    if (!resourcePath.startsWith(ongoingVotingsPath)) {
        // not of my business
        return;
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resolverFactory.getServiceResourceResolver(null);
    } catch (LoginException e) {
        logger.error("handleEvent: could not log in administratively: " + e, e);
        return;
    }
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("handleEvent: path = " + resourcePath + ", event = " + event);
        }
        analyzeVotings(resourceResolver);
    } catch (PersistenceException e) {
        logger.error("handleEvent: got a PersistenceException during votings analysis: " + e, e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15