Search in sources :

Example 1 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormStructureHelperImpl method updateFormStructure.

@Override
public Resource updateFormStructure(Resource formResource) {
    if (formResource != null) {
        ResourceResolver resolver = formResource.getResourceResolver();
        if (isFormContainer(formResource)) {
            // add default action type, form id and action path
            ModifiableValueMap formProperties = formResource.adaptTo(ModifiableValueMap.class);
            if (formProperties != null) {
                try {
                    if (formProperties.get(FormsConstants.START_PROPERTY_ACTION_TYPE, String.class) == null) {
                        formProperties.put(FormsConstants.START_PROPERTY_ACTION_TYPE, FormsConstants.DEFAULT_ACTION_TYPE);
                        String defaultContentPath = "/content/usergenerated" + formResource.getPath().replaceAll("^.content", "").replaceAll("jcr.content.*", "") + "cq-gen" + System.currentTimeMillis() + "/";
                        formProperties.put(FormsConstants.START_PROPERTY_ACTION_PATH, defaultContentPath);
                    }
                    resolver.commit();
                } catch (PersistenceException e) {
                    LOGGER.error("Unable to add default action type and form id " + formResource, e);
                }
            } else {
                LOGGER.error("Resource is not adaptable to ValueMap - unable to add default action type and " + "form id for " + formResource);
            }
        }
    }
    return null;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 2 with ModifiableValueMap

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

the class ResourceAccessSecurityImplTests method testCanUpdateUsingReadableResource.

@Test
public void testCanUpdateUsingReadableResource() {
    // one needs to have also read rights to obtain the resource
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    ModifiableValueMap resultValueMap = readableResource.adaptTo(ModifiableValueMap.class);
    resultValueMap.put("modified", "value");
    verify(valueMap, times(1)).put("modified", "value");
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 3 with ModifiableValueMap

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

the class ResourceAccessSecurityImplTests method testCannotUpdateAccidentallyUsingReadableResourceIfCannotUpdate.

@Test
public void testCannotUpdateAccidentallyUsingReadableResourceIfCannotUpdate() {
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    ValueMap resultValueMap = readableResource.adaptTo(ValueMap.class);
    resultValueMap.put("modified", "value");
    verify(valueMap, times(0)).put("modified", "value");
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 4 with ModifiableValueMap

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

the class JcrModifiableValueMapTest method testDateConversion.

/**
     * Test date conversions from Date to Calendar and vice versa when reading or writing from value maps.
     */
public void testDateConversion() throws Exception {
    this.rootNode.getSession().refresh(false);
    // prepare some date values
    Date dateValue1 = new Date(10000);
    Calendar calendarValue1 = Calendar.getInstance();
    calendarValue1.setTime(dateValue1);
    Date dateValue2 = new Date(20000);
    Calendar calendarValue2 = Calendar.getInstance();
    calendarValue2.setTime(dateValue2);
    Date dateValue3 = new Date(30000);
    Calendar calendarValue3 = Calendar.getInstance();
    calendarValue3.setTime(dateValue3);
    // write property
    final Node testNode = this.rootNode.addNode("dateConversionTest" + System.currentTimeMillis());
    testNode.setProperty(PROP1, calendarValue1);
    testNode.getSession().save();
    // write with property map
    final ModifiableValueMap pvm = new JcrModifiableValueMap(testNode, getHelperData());
    pvm.put(PROP2, dateValue2);
    pvm.put(PROP3, calendarValue3);
    getSession().save();
    // read with property map
    final ValueMap vm = new JcrModifiableValueMap(testNode, getHelperData());
    assertEquals(dateValue1, vm.get(PROP1, Date.class));
    assertEqualsCalendar(calendarValue1, vm.get(PROP1, Calendar.class));
    assertEquals(dateValue2, vm.get(PROP2, Date.class));
    assertEqualsCalendar(calendarValue2, vm.get(PROP2, Calendar.class));
    assertEquals(dateValue3, vm.get(PROP3, Date.class));
    assertEqualsCalendar(calendarValue3, vm.get(PROP3, Calendar.class));
    // check types
    assertTrue(vm.get(PROP1) instanceof Calendar);
    assertTrue(vm.get(PROP2) instanceof InputStream);
    assertTrue(vm.get(PROP3) instanceof Calendar);
    // read properties
    assertEqualsCalendar(calendarValue1, testNode.getProperty(PROP1).getDate());
    assertEqualsCalendar(calendarValue3, testNode.getProperty(PROP3).getDate());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AssertCalendar.assertEqualsCalendar(org.apache.sling.jcr.resource.internal.AssertCalendar.assertEqualsCalendar) Calendar(java.util.Calendar) Node(javax.jcr.Node) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 5 with ModifiableValueMap

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

the class JcrModifiableValueMapTest method testPut.

public void testPut() throws Exception {
    getSession().refresh(false);
    final ModifiableValueMap pvm = new JcrModifiableValueMap(this.rootNode, getHelperData());
    assertContains(pvm, initialSet());
    assertNull(pvm.get("something"));
    // now put two values and check set again
    pvm.put("something", "Another value");
    pvm.put("string", "overwrite");
    final Map<String, Object> currentlyStored = this.initialSet();
    currentlyStored.put("something", "Another value");
    currentlyStored.put("string", "overwrite");
    assertContains(pvm, currentlyStored);
    getSession().save();
    assertContains(pvm, currentlyStored);
    final ModifiableValueMap pvm2 = new JcrModifiableValueMap(this.rootNode, getHelperData());
    assertContains(pvm2, currentlyStored);
}
Also used : 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