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"));
}
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);
}
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));
}
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"));
}
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));
}
Aggregations