Search in sources :

Example 1 with ResourceResolver

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;
}
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 ResourceResolver

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());
    }
}
Also used : Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Property(javax.jcr.Property) Test(org.junit.Test)

Example 3 with ResourceResolver

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;
}
Also used : Path(org.apache.sling.api.resource.path.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LoginException(org.apache.sling.api.resource.LoginException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with ResourceResolver

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());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 5 with ResourceResolver

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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)514 Resource (org.apache.sling.api.resource.Resource)258 Test (org.junit.Test)170 HashMap (java.util.HashMap)86 LoginException (org.apache.sling.api.resource.LoginException)80 PersistenceException (org.apache.sling.api.resource.PersistenceException)68 Session (javax.jcr.Session)53 ValueMap (org.apache.sling.api.resource.ValueMap)48 ArrayList (java.util.ArrayList)45 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)34 RepositoryException (javax.jcr.RepositoryException)33 IOException (java.io.IOException)29 Map (java.util.Map)26 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 Before (org.junit.Before)25 List (java.util.List)24 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 HashSet (java.util.HashSet)18 Node (javax.jcr.Node)18