Search in sources :

Example 36 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class AbstractNoSqlResourceProviderTransactionalTest method testUpdateWithoutCommit.

@Test
public void testUpdateWithoutCommit() throws PersistenceException {
    Resource node1 = context.resourceResolver().create(testRoot(), "node1", ImmutableMap.<String, Object>of("prop1", "value1"));
    assertEquals("value1", node1.getValueMap().get("prop1", String.class));
    ModifiableValueMap props = node1.adaptTo(ModifiableValueMap.class);
    props.put("prop1", "value2");
    node1 = testRoot().getChild("node1");
    assertEquals("value2", node1.getValueMap().get("prop1", String.class));
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 37 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class CRUDMergedResource method adaptTo.

/**
     * {@inheritDoc}
     */
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) {
    if (type == ModifiableValueMap.class) {
        final Iterator<Resource> iter = this.picker.pickResources(this.getResourceResolver(), this.relativePath, null).iterator();
        Resource highestRsrc = null;
        while (iter.hasNext()) {
            highestRsrc = iter.next();
        }
        if (ResourceUtil.isNonExistingResource(highestRsrc)) {
            final String[] paths = (String[]) this.getResourceMetadata().get(MergedResourceConstants.METADATA_RESOURCES);
            final Resource copyResource = this.getResourceResolver().getResource(paths[paths.length - 1]);
            try {
                final Resource newResource = ResourceUtil.getOrCreateResource(this.getResourceResolver(), highestRsrc.getPath(), copyResource.getResourceType(), null, false);
                final ModifiableValueMap target = newResource.adaptTo(ModifiableValueMap.class);
                if (target != null) {
                    return (AdapterType) new ModifiableProperties(this, target);
                }
            } catch (final PersistenceException pe) {
            // we ignore this for now
            }
            return super.adaptTo(type);
        }
        final ModifiableValueMap target = highestRsrc.adaptTo(ModifiableValueMap.class);
        if (target != null) {
            return (AdapterType) new ModifiableProperties(this, target);
        }
    }
    return super.adaptTo(type);
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 38 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class MessageStoreImpl method updateThread.

private static void updateThread(ResourceResolver resolver, Resource thread, Map<String, Object> msgProps) throws PersistenceException {
    final ModifiableValueMap thrdProps = thread.adaptTo(ModifiableValueMap.class);
    Long prop = (Long) thrdProps.get(MessageFieldName.LAST_UPDATE);
    Date updatedDate = null;
    if (prop != null) {
        updatedDate = new Date(prop);
    }
    final String msgProp = (String) msgProps.get(FieldName.DATE);
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    final Date msgDate = sdf.parse(msgProp, new ParsePosition(0));
    if (updatedDate == null || msgDate.after(updatedDate)) {
        thrdProps.put(MessageFieldName.LAST_UPDATE, msgDate.getTime());
        resolver.commit();
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 39 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class CommonMergedResourceProviderTest method testHideChildren.

@Test
public void testHideChildren() throws PersistenceException {
    // create new child nodes below base and overlay
    MockHelper.create(this.resolver).resource("/apps/base/child1").p("property1", "frombase").resource("/apps/base/child2").p("property1", "frombase").resource("/apps/overlay/child1").p("property1", "fromoverlay").resource("/apps/overlay/child3").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);
    // 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);
    // all overlay resource are still exposed, because hiding children by wildcard only hides children from underlying resources
    Assert.assertThat(iterable, Matchers.containsInAnyOrder(ResourceMatchers.nameAndProps("child1", Collections.singletonMap("property1", (Object) "fromoverlay")), ResourceMatchers.nameAndProps("child3", Collections.singletonMap("property1", (Object) "fromoverlay"))));
    // now hide by explicit value
    properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, "child1");
    resolver.commit();
    // child1 is no longer exposed from overlay, because hiding children by name hides children from underlying as well as from local resources, child2 is exposed from base
    iterable = new IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
    Assert.assertThat(iterable, Matchers.containsInAnyOrder(ResourceMatchers.name("child2"), ResourceMatchers.name("child3")));
    // now hide by negated value (hide all underlying children except for the one with name child2)
    properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, new String[] { "!child2", "*", "child3" });
    resolver.commit();
    iterable = new IteratorIterable<Resource>(provider.listChildren(ctx, mergedResource), true);
    Assert.assertThat(iterable, Matchers.containsInAnyOrder(ResourceMatchers.name("child2"), 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 40 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class CommonMergedResourceProviderTest method testHideProperties.

@Test
public void testHideProperties() {
    ModifiableValueMap properties = base.adaptTo(ModifiableValueMap.class);
    properties.put("property1", "frombase");
    properties.put("property2", "frombase");
    properties.put(MergedResourceConstants.PN_HIDE_CHILDREN, "some invalid resource");
    // hide with wildcard
    ModifiableValueMap overlayProperties = overlay.adaptTo(ModifiableValueMap.class);
    overlayProperties.put(MergedResourceConstants.PN_HIDE_PROPERTIES, "*");
    Map<String, Object> expectedProperties = new HashMap<String, Object>();
    expectedProperties.put("property1", "fromoverlay");
    expectedProperties.put("property3", "fromoverlay");
    overlayProperties.putAll(expectedProperties);
    this.provider = new CRUDMergingResourceProvider("/merged", new SimpleMergedResourcePicker(), false);
    Resource mergedResource = this.provider.getResource(ctx, "/merged", ResourceContext.EMPTY_CONTEXT, null);
    // property1 is still exposed from overlay, because hiding properties by wildcard only hides children from underlying resources
    Assert.assertThat(mergedResource, ResourceMatchers.props(expectedProperties));
    // all properties from underlying resource are hidden!
    Assert.assertThat(mergedResource, Matchers.not(ResourceMatchers.props(properties)));
    // make sure no special properties are exposed
    Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_CHILDREN));
    Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_PROPERTIES));
    // hide by value
    overlayProperties.put(MergedResourceConstants.PN_HIDE_PROPERTIES, new String[] { "property1" });
    mergedResource = this.provider.getResource(ctx, "/merged", ResourceContext.EMPTY_CONTEXT, null);
    expectedProperties.put("property2", "frombase");
    expectedProperties.remove("property1");
    // property2 and property 3 are still exposed
    Assert.assertThat(mergedResource, ResourceMatchers.props(expectedProperties));
    // property1 is no longer exposed from overlay nor base, because hiding properties by name also hides local properties
    Assert.assertThat(mergedResource, Matchers.not(ResourceMatchers.props(Collections.singletonMap("property1", (Object) "fromoverlay"))));
    // make sure no special properties are exposed
    Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_CHILDREN));
    Assert.assertFalse(mergedResource.getValueMap().containsKey(MergedResourceConstants.PN_HIDE_PROPERTIES));
}
Also used : HashMap(java.util.HashMap) 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)

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