Search in sources :

Example 81 with ModifiableValueMap

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());
}
Also used : MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Resource(org.apache.sling.api.resource.Resource) MessageStoreImplRepositoryTestUtil.readTextFile(org.apache.sling.mailarchiveserver.impl.MessageStoreImplRepositoryTestUtil.readTextFile) File(java.io.File) FileInputStream(java.io.FileInputStream) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 82 with ModifiableValueMap

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);
        }
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 83 with ModifiableValueMap

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")));
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 84 with ModifiableValueMap

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"))));
}
Also used : IteratorIterable(org.apache.commons.collections4.iterators.IteratorIterable) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 85 with ModifiableValueMap

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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6