use of org.apache.sling.tenant.Tenant in project sling by apache.
the class TenantProviderImpl method getTenants.
@Override
public Iterator<Tenant> getTenants(final String tenantFilter) {
final Filter filter;
if (tenantFilter != null && tenantFilter.length() > 0) {
try {
filter = FrameworkUtil.createFilter(tenantFilter);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
} else {
filter = null;
}
Iterator<Tenant> result = call(new ResourceResolverTask<Iterator<Tenant>>() {
@SuppressWarnings("unchecked")
@Override
public Iterator<Tenant> call(ResourceResolver resolver) {
Resource tenantRootRes = resolver.getResource(tenantRootPath);
if (tenantRootRes != null) {
List<Tenant> tenantList = new ArrayList<Tenant>();
Iterator<Resource> tenantResourceList = tenantRootRes.listChildren();
while (tenantResourceList.hasNext()) {
Resource tenantRes = tenantResourceList.next();
if (filter == null || filter.matches(ResourceUtil.getValueMap(tenantRes))) {
TenantImpl tenant = new TenantImpl(tenantRes);
tenantList.add(tenant);
}
}
return tenantList.iterator();
}
return Collections.EMPTY_LIST.iterator();
}
});
if (result == null) {
// no filter or no resource resolver for calling
result = Collections.<Tenant>emptyList().iterator();
}
return result;
}
use of org.apache.sling.tenant.Tenant in project sling by apache.
the class WebConsolePlugin method doPost.
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
String msg = null;
final String cmd = req.getParameter("action");
if ("create".equals(cmd)) {
Tenant t = this.createTenant(req);
if (t != null) {
msg = String.format("Created Tenant %s (%s)", t.getName(), t.getDescription());
} else {
msg = "Cannot create tenant";
}
} else if ("remove".equals(cmd)) {
this.removeTenant(req);
} else {
msg = "Unknown command";
}
final String path = LABEL;
final String redirectTo;
if (msg == null) {
redirectTo = path;
} else {
redirectTo = path + "?message=" + msg;
}
resp.sendRedirect(redirectTo);
}
use of org.apache.sling.tenant.Tenant in project sling by apache.
the class TenantImplTest method test_name_description.
@Test
public void test_name_description() {
@SuppressWarnings("serial") Resource r = new MockResource(pt1, new HashMap<String, Object>() {
{
put(Tenant.PROP_NAME, n1);
put(Tenant.PROP_DESCRIPTION, d1);
}
});
Tenant tenant1 = new TenantImpl(r);
TestCase.assertEquals(t1, tenant1.getId());
TestCase.assertEquals(n1, tenant1.getName());
TestCase.assertEquals(d1, tenant1.getDescription());
Iterator<String> pi = tenant1.getPropertyNames();
TestCase.assertTrue(propNamesDefault.contains(pi.next()));
TestCase.assertTrue(propNamesDefault.contains(pi.next()));
TestCase.assertFalse(pi.hasNext());
TestCase.assertEquals(n1, tenant1.getProperty(Tenant.PROP_NAME));
TestCase.assertEquals(d1, tenant1.getProperty(Tenant.PROP_DESCRIPTION));
TestCase.assertNull(tenant1.getProperty(p1));
}
use of org.apache.sling.tenant.Tenant in project sling by apache.
the class TenantImplTest method test_property.
@Test
public void test_property() {
@SuppressWarnings("serial") Resource r = new MockResource(pt1, new HashMap<String, Object>() {
{
put(p1, p1);
}
});
Tenant tenant1 = new TenantImpl(r);
TestCase.assertEquals(t1, tenant1.getId());
TestCase.assertNull(tenant1.getName());
TestCase.assertNull(tenant1.getDescription());
Iterator<String> pi = tenant1.getPropertyNames();
TestCase.assertTrue(propNamesTest.contains(pi.next()));
TestCase.assertFalse(pi.hasNext());
TestCase.assertNull(tenant1.getProperty(Tenant.PROP_NAME));
TestCase.assertNull(tenant1.getProperty(Tenant.PROP_DESCRIPTION));
TestCase.assertEquals(p1, tenant1.getProperty(p1));
}
use of org.apache.sling.tenant.Tenant in project sling by apache.
the class TenantProviderImplTest method testListTenantsWithoutTenantRoot.
@Test
public void testListTenantsWithoutTenantRoot() throws Exception {
TenantProviderImpl provider = new TenantProviderImpl();
final ResourceResolverFactory rrf = Mockito.mock(ResourceResolverFactory.class);
final BundleContext context = Mockito.mock(BundleContext.class);
final ResourceResolver rr = Mockito.mock(ResourceResolver.class);
Mockito.when(rrf.getServiceResourceResolver(Mockito.anyMapOf(String.class, Object.class))).thenReturn(rr);
set(provider, "factory", rrf);
provider.activate(context, new HashMap<String, Object>());
Iterator<Tenant> tenants = provider.getTenants();
TestCase.assertNotNull(tenants);
TestCase.assertFalse(tenants.hasNext());
}
Aggregations