use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class MessageStoreImplRepositoryTest method assertSaveMessage.
private void assertSaveMessage(String messageFile) throws MimeException, IOException, FileNotFoundException {
MessageBuilder builder = new DefaultMessageBuilder();
Message msg = builder.parseMessage(new FileInputStream(new File(TU.TEST_FOLDER, messageFile)));
store.save(msg);
final Resource r = resolver.getResource(getResourcePath(msg, store));
assertNotNull("Expecting non-null Resource", r);
final ModifiableValueMap m = r.adaptTo(ModifiableValueMap.class);
File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, BODY_SUFFIX));
if (bodyFile.exists()) {
String expectedBody = readTextFile(bodyFile);
assertValueMap(m, "Body", expectedBody);
}
File headersFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, HEADERS_SUFFIX));
if (headersFile.exists()) {
MessageStoreImplRepositoryTestUtil.assertHeaders(headersFile, m);
}
// test at least something
assertTrue(headersFile.exists() || bodyFile.exists());
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class MessageStoreImplRepositoryTestUtil method assertLayer.
static void assertLayer(Resource root, List<String> types, int depth) {
for (Resource child : root.getChildren()) {
final ModifiableValueMap m = child.adaptTo(ModifiableValueMap.class);
if (m.keySet().contains(MessageStoreImplRepositoryTest.TEST_RT_KEY)) {
String type = m.get(MessageStoreImplRepositoryTest.TEST_RT_KEY, String.class);
assertEquals(String.format("Expecting %s to have %s type", child.getPath(), types.get(depth)), types.get(depth), type);
}
if (child.getChildren().iterator().hasNext()) {
assertLayer(child, types, depth + 1);
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class CommonMergedResourceProviderTest method testHideChildrenBeingSetOnParent.
@Test
public void testHideChildrenBeingSetOnParent() throws PersistenceException {
// create new child nodes below base and overlay
MockHelper.create(this.resolver).resource("/apps/base/child").p("property1", "frombase").resource("grandchild").p("property1", "frombase").resource("/apps/base/child2").p("property1", "frombase").resource("/apps/overlay/child").p("property1", "fromoverlay").commit();
ModifiableValueMap properties = overlay.adaptTo(ModifiableValueMap.class);
properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, "*");
resolver.commit();
Resource mergedResource = this.provider.getResource(ctx, "/merged", ResourceContext.EMPTY_CONTEXT, null);
// the child was hidden on the parent (but only for the underlying resource), the local child from the overlay is still exposed
Assert.assertThat(provider.getResource(ctx, "/merged/child", ResourceContext.EMPTY_CONTEXT, mergedResource), ResourceMatchers.nameAndProps("child", Collections.singletonMap("property1", (Object) "fromoverlay")));
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class CommonMergedResourceProviderTest method testHideChildrenWithResourceNamesStartingWithExclamationMark.
@Test
public void testHideChildrenWithResourceNamesStartingWithExclamationMark() throws PersistenceException {
// create new child nodes below base and overlay
MockHelper.create(this.resolver).resource("/apps/base/!child1").p("property1", "frombase").resource("/apps/overlay/!child1").p("property1", "fromoverlay").resource("/apps/overlay/!child3").p("property1", "fromoverlay").commit();
ModifiableValueMap properties = overlay.adaptTo(ModifiableValueMap.class);
// escape the resource name with another exclamation mark
properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, "!!child3");
resolver.commit();
Resource mergedResource = this.provider.getResource(ctx, "/merged", ResourceContext.EMPTY_CONTEXT, null);
// convert the iterator returned by list children into an iterable (to be able to perform some tests)
IteratorIterable<Resource> iterable = new IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
// the resource named "!child3" should be hidden
Assert.assertThat(iterable, Matchers.contains(ResourceMatchers.nameAndProps("!child1", Collections.singletonMap("property1", (Object) "fromoverlay"))));
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class CRUDMergingResourceProvider method create.
@Override
public Resource create(final ResolveContext<Void> ctx, final String path, final Map<String, Object> properties) throws PersistenceException {
final ResourceResolver resolver = ctx.getResourceResolver();
// check if the resource exists
final Resource mountResource = this.getResource(ctx, path, ResourceContext.EMPTY_CONTEXT, null);
if (mountResource != null) {
throw new PersistenceException("Resource at " + path + " already exists.", null, path, null);
}
// creation of the root mount resource is not supported
final String relativePath = getRelativePath(path);
if (relativePath == null || relativePath.length() == 0) {
throw new PersistenceException("Resource at " + path + " can't be created.", null, path, null);
}
final ExtendedResourceHolder holder = this.getAllResources(resolver, path, relativePath);
// we only support modifications if there is more than one location merged
if (holder.count < 2) {
throw new PersistenceException("Modifying is only supported with at least two potentially merged resources.", null, path, null);
}
if (holder.resources.size() == 0 || (holder.resources.size() < holder.count && !holder.resources.get(holder.resources.size() - 1).getPath().equals(holder.highestResourcePath))) {
final String createPath = holder.highestResourcePath;
final Resource parentResource = ResourceUtil.getOrCreateResource(resolver, ResourceUtil.getParent(createPath), (String) null, null, false);
resolver.create(parentResource, ResourceUtil.getName(createPath), properties);
} else {
final Resource hidingResource = resolver.getResource(holder.highestResourcePath);
if (hidingResource != null) {
final ModifiableValueMap mvm = hidingResource.adaptTo(ModifiableValueMap.class);
mvm.remove(MergedResourceConstants.PN_HIDE_RESOURCE);
mvm.putAll(properties);
}
// TODO check parent hiding
}
return this.getResource(ctx, path, ResourceContext.EMPTY_CONTEXT, null);
}
Aggregations