Search in sources :

Example 11 with ContentPolicyManager

use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AllowedHeadingElementsDataSourceServlet method getAllowedHeadingElements.

private List<Resource> getAllowedHeadingElements(@NotNull SlingHttpServletRequest request) {
    List<Resource> allowedHeadingElements = new ArrayList<>();
    ResourceResolver resolver = request.getResourceResolver();
    Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
    ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
    if (contentResource != null && policyManager != null) {
        ContentPolicy policy = policyManager.getPolicy(contentResource);
        if (policy != null) {
            ValueMap props = policy.getProperties();
            if (props != null) {
                String[] headingElements = props.get(PN_ALLOWED_HEADING_ELEMENTS, String[].class);
                String[] allowedTypes = props.get(PN_ALLOWED_TYPES, String[].class);
                String defaultHeadingElement = props.get(PN_DEFAULT_HEADING_ELEMENT, props.get(PN_DEFAULT_TYPE, StringUtils.EMPTY));
                if (headingElements == null || headingElements.length == 0) {
                    headingElements = allowedTypes;
                }
                if (headingElements != null && headingElements.length > 0) {
                    for (String headingElement : headingElements) {
                        allowedHeadingElements.add(new HeadingElementResource(headingElement, StringUtils.equals(headingElement, defaultHeadingElement), resolver));
                    }
                }
            }
        }
    }
    return allowedHeadingElements;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy)

Aggregations

ContentPolicyManager (com.day.cq.wcm.api.policies.ContentPolicyManager)11 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)10 ValueMap (org.apache.sling.api.resource.ValueMap)8 Resource (org.apache.sling.api.resource.Resource)7 ArrayList (java.util.ArrayList)6 Designer (com.day.cq.wcm.api.designer.Designer)2 CoreResourceWrapper (com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper)1 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)1 DownloadResource (com.day.cq.commons.DownloadResource)1 ImageResource (com.day.cq.commons.ImageResource)1 Page (com.day.cq.wcm.api.Page)1 ComponentManager (com.day.cq.wcm.api.components.ComponentManager)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1 MockValueMap (org.apache.sling.testing.resourceresolver.MockValueMap)1