use of org.apache.sling.api.resource.ResourceResolver 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.ResourceResolver in project sling by apache.
the class JcrPropertyResourceTest method testCorrectUTF8ByteLength.
@Test
public void testCorrectUTF8ByteLength() throws RepositoryException, UnsupportedEncodingException {
final HashMap<Object, Integer> testData = new HashMap<Object, Integer>() {
{
put("some random ascii string", PropertyType.STRING);
put("русский язык", PropertyType.STRING);
put("贛語", PropertyType.STRING);
put("string with ümlaut", PropertyType.STRING);
put(true, PropertyType.BOOLEAN);
put(1000L, PropertyType.LONG);
put(BigDecimal.TEN, PropertyType.DECIMAL);
}
};
final ResourceResolver resolver = this.context.mock(ResourceResolver.class);
for (final Entry<Object, Integer> data : testData.entrySet()) {
final String stringValue = data.getKey().toString();
final long stringByteLength = stringValue.getBytes("UTF-8").length;
final Property property = this.context.mock(Property.class, stringValue);
this.context.checking(new Expectations() {
{
ignoring(resolver);
allowing(property).getParent();
allowing(property).getName();
allowing(property).isMultiple();
will(returnValue(false));
allowing(property).getLength();
will(returnValue((long) stringValue.length()));
allowing(property).getType();
will(returnValue(data.getValue()));
allowing(property).getString();
will(returnValue(stringValue));
}
});
final JcrPropertyResource propResource = new JcrPropertyResource(resolver, "/path/to/string-property", null, property);
assertEquals("Byte length of " + stringValue, stringByteLength, propResource.getResourceMetadata().getContentLength());
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MapEntries method getVanityPaths.
/**
* get the vanity paths Search for all nodes having a specific vanityPath
*/
private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
Map<String, List<MapEntry>> entryMap = new HashMap<>();
// sling:vanityPath (lowercase) is the property name
final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM nt:base WHERE sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath).replaceAll("'", "''") + "' OR sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath.substring(1)).replaceAll("'", "''") + "' ORDER BY sling:vanityOrder DESC";
ResourceResolver queryResolver = null;
try {
queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));
final Iterator<Resource> i = queryResolver.findResources(queryString, "sql");
while (i.hasNext()) {
final Resource resource = i.next();
boolean isValid = false;
for (final Path sPath : this.factory.getObservationPaths()) {
if (sPath.matches(resource.getPath())) {
isValid = true;
break;
}
}
if (isValid) {
if (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
loadVanityPath(resource, resolveMapsMap, vanityTargets, true, false);
entryMap = resolveMapsMap;
} else {
final Map<String, List<String>> targetPaths = new HashMap<>();
loadVanityPath(resource, entryMap, targetPaths, true, false);
}
}
}
} catch (LoginException e) {
log.error("Exception while obtaining queryResolver", e);
} finally {
if (queryResolver != null) {
queryResolver.close();
}
}
return entryMap;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MockedResourceResolverImplTest method testResolverMisc.
/**
* Misceleneous coverage.
* @throws LoginException
*/
@Test
public void testResolverMisc() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
try {
resourceResolver.getAttribute(null);
Assert.fail("Should have thrown a NPE");
} catch (NullPointerException e) {
// this is expected.
}
Assert.assertArrayEquals(new String[] { "/apps/", "/libs/" }, resourceResolver.getSearchPath());
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MockedResourceResolverImplTest method testGetResource.
/**
* Test getResource for a resource provided by a resource provider.
* @throws LoginException
*/
@Test
public void testGetResource() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
Assert.assertNotNull(resourceResolver);
Resource singleResource = buildResource("/single/test", EMPTY_RESOURCE_LIST, resourceResolver, resourceProvider);
Resource resource = resourceResolver.getResource("/single/test");
Assert.assertEquals(singleResource, resource);
}
Aggregations