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;
}
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");
}
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");
}
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());
}
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);
}
Aggregations