Search in sources :

Example 1 with ContentPolicyManager

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

the class AmpUtil method getAmpPropertyFromPolicy.

/**
 * Retrieves the value of the amp property from the request resource's content policy.
 * @param slingRequest The request used to get the resource and its content policy.
 * @return The value of the property of the resource's content policy. Returns empty String if fails to read the content
 * policy.
 */
@NotNull
private static String getAmpPropertyFromPolicy(@NotNull SlingHttpServletRequest slingRequest) {
    String ampProperty = StringUtils.EMPTY;
    ContentPolicyManager policyManager = slingRequest.getResourceResolver().adaptTo(ContentPolicyManager.class);
    if (policyManager != null) {
        ContentPolicy contentPolicy = policyManager.getPolicy(slingRequest.getResource());
        if (contentPolicy != null) {
            ampProperty = contentPolicy.getProperties().get(AMP_MODE_PROP, StringUtils.EMPTY);
        }
    }
    return ampProperty;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ContentPolicyManager

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

the class AllowedEmbeddablesDataSourceServlet method getAllowedEmbeddables.

private List<Resource> getAllowedEmbeddables(@NotNull SlingHttpServletRequest request) {
    List<Resource> allowedEmbeddableResources = new ArrayList<>();
    ResourceResolver resolver = request.getResourceResolver();
    Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
    ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
    if (policyManager == null || contentResource == null) {
        return allowedEmbeddableResources;
    }
    ValueMap properties = null;
    ContentPolicy policy = policyManager.getPolicy(contentResource);
    if (policy != null) {
        properties = policy.getProperties();
    } else {
        Designer designer = resolver.adaptTo(Designer.class);
        if (designer != null) {
            properties = designer.getStyle(contentResource);
        }
    }
    if (properties == null) {
        return allowedEmbeddableResources;
    }
    String[] allowedEmbeddableResourceTypes = properties.get(Embed.PN_DESIGN_ALLOWED_EMBEDDABLES, String[].class);
    if (allowedEmbeddableResourceTypes != null && allowedEmbeddableResourceTypes.length > 0) {
        allowedEmbeddableResources.add(new AllowedEmbeddableResource("Select", "", resolver));
        for (String embeddableResourceType : allowedEmbeddableResourceTypes) {
            Resource componentResource = resolver.getResource(embeddableResourceType);
            if (componentResource != null) {
                allowedEmbeddableResources.add(new AllowedEmbeddableResource(componentResource.getValueMap().get(JcrConstants.JCR_TITLE, componentResource.getName()), embeddableResourceType, resolver));
            }
        }
    }
    return allowedEmbeddableResources;
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ValueMap(org.apache.sling.api.resource.ValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) 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)

Example 3 with ContentPolicyManager

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

the class AllowedColorSwatchesDataSourceServlet method getAllowedColorSwatches.

protected List<Resource> getAllowedColorSwatches(@NotNull SlingHttpServletRequest request) {
    List<Resource> colors = new ArrayList<>();
    ResourceResolver resolver = request.getResourceResolver();
    Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
    ContentPolicyManager policyMgr = resolver.adaptTo(ContentPolicyManager.class);
    if (policyMgr != null) {
        ContentPolicy policy = policyMgr.getPolicy(contentResource);
        if (policy != null) {
            ValueMap color = null;
            ValueMap properties = policy.getProperties();
            if (properties != null) {
                String[] allowedColorSwatches = properties.get(PN_ALLOWED_COLOR_SWATCHES, String[].class);
                if (allowedColorSwatches != null && allowedColorSwatches.length > 0) {
                    for (String allowedColorSwatch : allowedColorSwatches) {
                        color = new ValueMapDecorator(new HashMap<String, Object>());
                        color.put(PN_COLOR_VALUE, allowedColorSwatch);
                        colors.add(new ValueMapResource(resolver, new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, color));
                    }
                }
            }
        }
    }
    return colors;
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource)

Example 4 with ContentPolicyManager

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

the class AdaptiveImageServlet method getContentPolicy.

/**
 * Returns the content policy bound to the given component.
 *
 * @param imageResource the resource identifying the accessed image component
 * @return the content policy. May be {@code nulll} in case no content policy can be found.
 */
private ContentPolicy getContentPolicy(@NotNull Resource imageResource) {
    ResourceResolver resourceResolver = imageResource.getResourceResolver();
    ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
    if (policyManager != null) {
        ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
        if (componentManager != null) {
            com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(imageResource);
            if (component != null && component.getProperties() != null) {
                String delegatingResourceType = component.getProperties().get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class);
                if (StringUtils.isNotEmpty(delegatingResourceType)) {
                    imageResource = new CoreResourceWrapper(imageResource, delegatingResourceType);
                }
            }
        }
        return policyManager.getPolicy(imageResource);
    } else {
        LOGGER.warn("Could not get policy manager from resource resolver!");
    }
    return null;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ComponentManager(com.day.cq.wcm.api.components.ComponentManager) CoreResourceWrapper(com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper)

Example 5 with ContentPolicyManager

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

the class EmbeddableOptionsDataSourceServlet method getEmbeddableOptions.

private List<Resource> getEmbeddableOptions(@NotNull SlingHttpServletRequest request) {
    List<Resource> embeddableOptionsResources = new ArrayList<>();
    ResourceResolver resolver = request.getResourceResolver();
    Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
    ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
    if (policyManager != null) {
        ValueMap properties = null;
        ContentPolicy policy = policyManager.getPolicy(contentResource);
        if (policy != null) {
            properties = policy.getProperties();
        } else {
            Designer designer = resolver.adaptTo(Designer.class);
            if (designer != null) {
                properties = designer.getStyle(contentResource);
            }
        }
        if (properties != null) {
            String[] allowedEmbeddables = properties.get(Embed.PN_DESIGN_ALLOWED_EMBEDDABLES, String[].class);
            if (allowedEmbeddables != null && allowedEmbeddables.length > 0) {
                for (String allowedEmbeddable : allowedEmbeddables) {
                    Resource dialogResource = resolver.getResource(allowedEmbeddable + "/" + NN_DIALOG);
                    if (dialogResource != null) {
                        embeddableOptionsResources.add(dialogResource);
                    }
                }
            }
        }
    }
    return embeddableOptionsResources;
}
Also used : Designer(com.day.cq.wcm.api.designer.Designer) 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