Search in sources :

Example 31 with ValueMap

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

the class FileResource method getValueMap.

@Override
public ValueMap getValueMap() {
    if (valueMap == null) {
        // we should simulate the corresponding JCR properties in a value map as well
        if (file.exists() && file.canRead()) {
            Map<String, Object> props = new HashMap<String, Object>();
            props.put("jcr:primaryType", file.isFile() ? RESOURCE_TYPE_FILE : RESOURCE_TYPE_FOLDER);
            props.put("jcr:createdBy", "system");
            Calendar lastModifed = Calendar.getInstance();
            lastModifed.setTimeInMillis(file.lastModified());
            props.put("jcr:created", lastModifed);
            // overlay properties with those from node descriptor content file, if it exists
            ContentFile contentFile = getNodeDescriptorContentFile();
            if (contentFile != null) {
                for (Map.Entry<String, Object> entry : contentFile.getValueMap().entrySet()) {
                    // skip primary type if it is the default type assigned by contentparser when none is defined
                    if (StringUtils.equals(entry.getKey(), "jcr:primaryType") && StringUtils.equals((String) entry.getValue(), ParserOptions.DEFAULT_PRIMARY_TYPE)) {
                        continue;
                    }
                    props.put(entry.getKey(), entry.getValue());
                }
            }
            valueMap = new ValueMapDecorator(props);
        }
    }
    return valueMap;
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) ValueMapDecorator(org.apache.sling.fsprovider.internal.mapper.valuemap.ValueMapDecorator) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with ValueMap

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

the class ContentFileTest method testContentLevel1.

@Test
public void testContentLevel1() {
    File file = new File("src/test/resources/fs-test/folder2/content.json");
    ContentFile underTest = new ContentFile(file, "/fs-test/folder2/content", "jcr:content", contentFileCache);
    assertEquals(file, underTest.getFile());
    assertEquals("jcr:content", underTest.getSubPath());
    assertTrue(underTest.hasContent());
    ContentElement content = underTest.getContent();
    assertEquals("app:PageContent", content.getProperties().get("jcr:primaryType"));
    ValueMap props = underTest.getValueMap();
    assertEquals("app:PageContent", props.get("jcr:primaryType"));
}
Also used : ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ValueMap(org.apache.sling.api.resource.ValueMap) File(java.io.File) Test(org.junit.Test)

Example 33 with ValueMap

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

the class ValueMapUtilTest method testToValueMap.

@Test
public void testToValueMap() {
    Map<String, Object> content = new HashMap<>();
    content.put("stringProp", "abc");
    content.put("intProp", 123);
    content.put("stringArray", new String[] { "a", "b", "c" });
    content.put("stringList", ImmutableList.of("ab", "cd"));
    content.put("intList", ImmutableList.of(12, 34));
    ValueMap props = ValueMapUtil.toValueMap(content);
    assertEquals("abc", props.get("stringProp", String.class));
    assertEquals((Integer) 123, props.get("intProp", 0));
    assertArrayEquals(new String[] { "a", "b", "c" }, props.get("stringArray", String[].class));
    assertArrayEquals(new String[] { "ab", "cd" }, props.get("stringList", String[].class));
    assertArrayEquals(new Integer[] { 12, 34 }, props.get("intList", Integer[].class));
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) Test(org.junit.Test)

Example 34 with ValueMap

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

the class ContentFileTest method testContentLevel5.

@Test
public void testContentLevel5() {
    File file = new File("src/test/resources/fs-test/folder2/content.json");
    ContentFile underTest = new ContentFile(file, "/fs-test/folder2/content", "jcr:content/par/image/file/jcr:content", contentFileCache);
    assertEquals(file, underTest.getFile());
    assertEquals("jcr:content/par/image/file/jcr:content", underTest.getSubPath());
    assertTrue(underTest.hasContent());
    ContentElement content = underTest.getContent();
    assertEquals("nt:resource", content.getProperties().get("jcr:primaryType"));
    ValueMap props = underTest.getValueMap();
    assertEquals("nt:resource", props.get("jcr:primaryType"));
}
Also used : ContentElement(org.apache.sling.fsprovider.internal.parser.ContentElement) ValueMap(org.apache.sling.api.resource.ValueMap) File(java.io.File) Test(org.junit.Test)

Example 35 with ValueMap

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

the class JcrXmlContentTest method testContent_Datatypes.

@Test
public void testContent_Datatypes() {
    Resource underTest = fsroot.getChild("folder3/content/jcr:content");
    ValueMap props = underTest.getValueMap();
    assertEquals("en", props.get("jcr:title", String.class));
    assertEquals(true, props.get("includeAside", false));
    assertEquals((Long) 1234567890123L, props.get("longProp", Long.class));
    assertEquals((Double) 1.2345d, props.get("decimalProp", Double.class), 0.00001d);
    assertArrayEquals(new String[] { "aa", "bb", "cc" }, props.get("stringPropMulti", String[].class));
    assertArrayEquals(new Long[] { 1234567890123L, 55L }, props.get("longPropMulti", Long[].class));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

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