Search in sources :

Example 6 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class ResourceProviderEntryTest method testRemoveProviders.

@Test
public void testRemoveProviders() throws LoginException {
    String firstPath = "/rootel";
    String thirdPath = "/apps/sling/sample";
    String secondPath = firstPath + "/child";
    final ResourceResolver firstResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> first = Mockito.mock(ResourceProvider.class);
    Mockito.when(first.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(firstPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(firstResolver));
    final ResourceResolver secondResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> second = Mockito.mock(ResourceProvider.class);
    Mockito.when(second.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(secondPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(secondResolver));
    final ResourceResolver thirdResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> third = Mockito.mock(ResourceProvider.class);
    Mockito.when(third.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(thirdPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(thirdResolver));
    final Map<String, Object> firstProps = new HashMap<String, Object>();
    firstProps.put(Constants.SERVICE_ID, (long) 1);
    final Map<String, Object> secondProps = new HashMap<String, Object>();
    secondProps.put(Constants.SERVICE_ID, (long) 2);
    final Map<String, Object> thirdProps = new HashMap<String, Object>();
    thirdProps.put(Constants.SERVICE_ID, (long) 3);
    ResourceProviderHandler firstH = createRPHandler(first, "rp1", 1, firstPath);
    providers.add(firstH);
    providers.add(createRPHandler(second, "rp2", 2, secondPath));
    providers.add(createRPHandler(third, "rp3", 3, thirdPath));
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    providers.remove(firstH);
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/rootel/sddsf/sdfsdf/html.js"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    providers.add(firstH);
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) AbstractResource(org.apache.sling.api.resource.AbstractResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 7 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class ResourceResolverControl method listChildren.

/**
     * This method asks all matching resource providers for the children iterators,
     * merges them, adds {@link SyntheticResource}s (see
     * {@link #getResource(String, Resource, Map, boolean)} for more details),
     * filters out the duplicates and returns the resulting iterator. All
     * transformations are done lazily, during the {@link Iterator#hasNext()}
     * invocation on the result.
     */
@SuppressWarnings("unchecked")
public Iterator<Resource> listChildren(final ResourceResolverContext context, final Resource parent) {
    final String parentPath = parent.getPath();
    // 3 sources are combined: children of the provider which owns 'parent',
    // providers which are directly mounted at a child path,
    // synthetic resources for providers mounted at a lower level
    // children of the 'parent' provider
    Iterator<Resource> realChildren = null;
    final AuthenticatedResourceProvider provider = this.getBestMatchingProvider(context, parentPath);
    if (provider != null) {
        realChildren = provider.listChildren(parent);
    }
    final Set<String> visitedNames = new HashSet<>();
    IteratorChain chain = new IteratorChain();
    if (realChildren != null) {
        chain.addIterator(realChildren);
    }
    // synthetic and providers are done in one loop
    final Node<ResourceProviderHandler> node = getResourceProviderStorage().getTree().getNode(parent.getPath());
    if (node != null) {
        final List<Resource> syntheticList = new ArrayList<>();
        final List<Resource> providerList = new ArrayList<>();
        for (final Entry<String, Node<ResourceProviderHandler>> entry : node.getChildren().entrySet()) {
            final String name = entry.getKey();
            final ResourceProviderHandler handler = entry.getValue().getValue();
            PathBuilder pathBuilder = new PathBuilder(parent.getPath());
            pathBuilder.append(name);
            final String childPath = pathBuilder.toString();
            if (handler == null) {
                syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
            } else {
                Resource rsrc = null;
                try {
                    final AuthenticatedResourceProvider rp = context.getProviderManager().getOrCreateProvider(handler, this);
                    rsrc = rp == null ? null : rp.getResource(childPath, parent, null);
                } catch (final LoginException ignore) {
                // ignore
                }
                if (rsrc != null) {
                    providerList.add(rsrc);
                } else {
                    // otherwise we need to make sure that no one else is providing this child
                    if (entry.getValue().getChildren().isEmpty()) {
                        syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath, ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
                    } else {
                        visitedNames.add(name);
                    }
                }
            }
        }
        if (!providerList.isEmpty()) {
            chain.addIterator(providerList.iterator());
        }
        if (!syntheticList.isEmpty()) {
            chain.addIterator(syntheticList.iterator());
        }
    }
    if (chain.size() == 0) {
        return Collections.EMPTY_LIST.iterator();
    }
    return new UniqueResourceIterator(visitedNames, chain);
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) Node(org.apache.sling.resourceresolver.impl.providers.tree.Node) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ArrayList(java.util.ArrayList) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider) IteratorChain(org.apache.commons.collections4.iterators.IteratorChain) PathBuilder(org.apache.sling.api.resource.path.PathBuilder) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class FactoryPreconditionsTest method testPIDs.

@Test
public void testPIDs() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, new HashSet<>(Arrays.asList("pid1", "pid3")), null, tracker);
    final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlers(new String[] { "pid2" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
    assertFalse(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] { "pid1", "pid2", "pid3" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
    assertTrue(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlers(new String[] { "pid1" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
    assertFalse(conditions.checkPreconditions(null, null));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Example 9 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class FactoryPreconditionsTest method testUnregisteringService.

@Test
public void testUnregisteringService() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, new HashSet<>(Arrays.asList("pid1", "pid3")), null, tracker);
    final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] { "pid1", "pid2", "pid3" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
    assertTrue(conditions.checkPreconditions(null, null));
    assertTrue(conditions.checkPreconditions(null, "pid2"));
    assertFalse(conditions.checkPreconditions(null, "pid1"));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Example 10 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class FactoryPreconditionsTest method testNames.

@Test
public void testNames() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, null, new HashSet<>(Arrays.asList("n1", "n2")), tracker);
    final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlersWithNames(new String[] { "n2" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
    assertFalse(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlersWithNames(new String[] { "n1", "n2", "n3" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
    assertTrue(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlersWithNames(new String[] { "n1" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
    assertFalse(conditions.checkPreconditions(null, null));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Aggregations

ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)18 Resource (org.apache.sling.api.resource.Resource)9 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)7 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 Test (org.junit.Test)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)5 ResourceProviderTracker (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker)5 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)5 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)5 Before (org.junit.Before)5 ArrayList (java.util.ArrayList)4 ResourceProviderInfo (org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo)4 ServiceReference (org.osgi.framework.ServiceReference)4 LoginException (org.apache.sling.api.resource.LoginException)3 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)3 ResourceAccessSecurityTracker (org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker)3 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)3 QueryLanguageProvider (org.apache.sling.spi.resource.provider.QueryLanguageProvider)3 AbstractResource (org.apache.sling.api.resource.AbstractResource)2