Search in sources :

Example 91 with ValueMap

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

the class WritePipe method copyProperties.

/**
     * Write properties from the configuration to the target resource,
     * instantiating both property names & values
     *
     * @param conf configured resource that holds all properties to write (and children)
     * @param target target resource on which configured values will be written
     * @throws RepositoryException issues occuring when traversing nodes
     */
private void copyProperties(Resource conf, Resource target) throws RepositoryException {
    ValueMap writeMap = conf.adaptTo(ValueMap.class);
    ModifiableValueMap properties = target.adaptTo(ModifiableValueMap.class);
    //writing current node
    if (properties != null && writeMap != null) {
        for (Map.Entry<String, Object> entry : writeMap.entrySet()) {
            if (!IGNORED_PROPERTIES.contains(entry.getKey())) {
                String key = parent != null ? bindings.instantiateExpression(entry.getKey()) : entry.getKey();
                Object value = computeValue(target, key, entry.getValue());
                if (value == null) {
                    //null value are not handled by modifiable value maps,
                    //removing the property if it exists
                    addPropertyToRemove(target.getChild(key));
                } else {
                    logger.info("writing {}={}", target.getPath() + "@" + key, value);
                    if (!isDryRun()) {
                        properties.put(key, value);
                    }
                }
            }
        }
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 92 with ValueMap

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

the class WritePipeTest method assertPiped.

/**
     *
     * @param resource
     */
public static void assertPiped(Resource resource) {
    ValueMap properties = resource.adaptTo(ValueMap.class);
    String[] array = new String[] { "cabbage", "carrot" };
    assertArrayEquals("Second fruit should have been correctly instantiated & patched, added to the first", new String[] { "apple", "banana" }, properties.get("fruits", String[].class));
    assertArrayEquals("Fixed mv should be there", array, properties.get("fixedVegetables", String[].class));
    assertArrayEquals("Expr fixed mv should there and computed", array, properties.get("computedVegetables", String[].class));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap)

Example 93 with ValueMap

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

the class AbstractNoSqlResourceProviderTest method testCalendarPropertyToDate.

@Test
public void testCalendarPropertyToDate() throws IOException {
    Resource resource1 = context.resourceResolver().getResource(testRoot().getPath() + "/node1");
    ValueMap props = ResourceUtil.getValueMap(resource1);
    Date dateValue = props.get("calendarProp", Date.class);
    assertNotNull(dateValue);
    assertEquals(CALENDAR_VALUE.getTime(), dateValue);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Date(java.util.Date) Test(org.junit.Test)

Example 94 with ValueMap

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

the class AbstractContentLoaderJsonTest method testUTF8Chars.

@Test
public void testUTF8Chars() {
    Resource resource = context.resourceResolver().getResource(path + "/sample/en/jcr:content");
    ValueMap props = ResourceUtil.getValueMap(resource);
    assertEquals("äöü߀", props.get("utf8Property"));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 95 with ValueMap

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

the class AbstractContentLoaderJsonTest method assertPrimaryNodeType.

private void assertPrimaryNodeType(final Resource resource, final String nodeType) throws RepositoryException {
    Node node = resource.adaptTo(Node.class);
    if (node != null) {
        assertEquals(nodeType, node.getPrimaryNodeType().getName());
    } else {
        ValueMap props = ResourceUtil.getValueMap(resource);
        assertEquals(nodeType, props.get(JcrConstants.JCR_PRIMARYTYPE));
    }
}
Also used : Node(javax.jcr.Node) ValueMap(org.apache.sling.api.resource.ValueMap)

Aggregations

ValueMap (org.apache.sling.api.resource.ValueMap)278 Resource (org.apache.sling.api.resource.Resource)205 Test (org.junit.Test)160 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)59 HashMap (java.util.HashMap)54 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)44 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)26 Map (java.util.Map)21 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)21 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)20 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)20 Expectations (org.jmock.Expectations)20 ArrayList (java.util.ArrayList)18 PersistenceException (org.apache.sling.api.resource.PersistenceException)17 Calendar (java.util.Calendar)16 Date (java.util.Date)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 InputStream (java.io.InputStream)11 Node (javax.jcr.Node)11