Search in sources :

Example 6 with ContentPolicyManager

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

the class ContentFragmentUtilsTest method getGridTypeSetInFragmentPolicy.

@Test
public void getGridTypeSetInFragmentPolicy() {
    // GIVEN
    ContentPolicyManager contentPolicyManager = Mockito.mock(ContentPolicyManager.class);
    ContentPolicy contentPolicy = Mockito.mock(ContentPolicy.class);
    ResourceResolver resourceResolver = Mockito.mock(ResourceResolver.class);
    Resource resource = Mockito.mock(Resource.class);
    ValueMap valueMap = new MockValueMap(resource);
    valueMap.put(PN_CFM_GRID_TYPE, "foobar");
    Mockito.when(resource.getResourceResolver()).thenReturn(resourceResolver);
    Mockito.when(resourceResolver.adaptTo(Mockito.eq(ContentPolicyManager.class))).thenReturn(contentPolicyManager);
    Mockito.when(contentPolicyManager.getPolicy(Mockito.eq(resource))).thenReturn(contentPolicy);
    Mockito.when(contentPolicy.getProperties()).thenReturn(valueMap);
    // WHEN
    String defaultGridResourceType = ContentFragmentUtils.getGridResourceType(resource);
    // THEN
    Assertions.assertEquals(defaultGridResourceType, "foobar");
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) ValueMap(org.apache.sling.api.resource.ValueMap) MockValueMap(org.apache.sling.testing.resourceresolver.MockValueMap) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.jupiter.api.Test)

Example 7 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 getAllowedRenditionWidths.

/**
     * Returns the list of allowed renditions sizes from this component's content policy. If the component doesn't have a content policy,
     * then the list will be empty. Rendition widths that are not valid {@link Integer} numbers will be ignored.
     *
     * @param request the request identifying the component
     * @return the list of the allowed widths; the list will be <i>empty</i> if the component doesn't have a content policy
     */
private List<Integer> getAllowedRenditionWidths(@Nonnull SlingHttpServletRequest request) {
    List<Integer> list = new ArrayList<>();
    ResourceResolver resourceResolver = request.getResourceResolver();
    ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
    if (policyManager != null) {
        ContentPolicy contentPolicy = policyManager.getPolicy(request.getResource());
        if (contentPolicy != null) {
            String[] allowedRenditionWidths = contentPolicy.getProperties().get(com.adobe.cq.wcm.core.components.models.Image.PN_DESIGN_ALLOWED_RENDITION_WIDTHS, new String[0]);
            for (String width : allowedRenditionWidths) {
                try {
                    list.add(Integer.parseInt(width));
                } catch (NumberFormatException e) {
                    LOGGER.warn("One of the configured widths ({}) from the {} content policy is not a valid Integer.", width, contentPolicy.getPath());
                    return list;
                }
            }
        }
    }
    return list;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy)

Example 8 with ContentPolicyManager

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

the class AllowedTitleSizesDataSourceServlet method getAllowedTypes.

private List<Resource> getAllowedTypes(@Nonnull SlingHttpServletRequest request) {
    List<Resource> allowedTypes = 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 props = policy.getProperties();
            if (props != null) {
                String[] titleTypes = props.get("allowedTypes", String[].class);
                if (titleTypes != null && titleTypes.length > 0) {
                    for (String titleType : titleTypes) {
                        allowedTypes.add(new TitleTypeResource(titleType, resolver));
                    }
                }
            }
        }
    }
    return allowedTypes;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy)

Example 9 with ContentPolicyManager

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

the class ImageImpl method initModel.

@PostConstruct
private void initModel() {
    boolean hasContent = false;
    if (StringUtils.isNotEmpty(fileReference)) {
        int dotIndex;
        if ((dotIndex = fileReference.lastIndexOf(DOT)) != -1) {
            extension = fileReference.substring(dotIndex + 1);
        }
        hasContent = true;
    } else {
        Resource file = resource.getChild(DownloadResource.NN_FILE);
        if (file != null) {
            extension = mimeTypeService.getExtension(PropertiesUtil.toString(file.getResourceMetadata().get(ResourceMetadata.CONTENT_TYPE), MIME_TYPE_IMAGE_JPEG));
            hasContent = true;
        }
    }
    if (hasContent) {
        long lastModifiedDate = 0;
        if (!wcmmode.isDisabled()) {
            ValueMap properties = resource.getValueMap();
            Calendar lastModified = properties.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
            if (lastModified == null) {
                lastModified = properties.get(NameConstants.PN_PAGE_LAST_MOD, Calendar.class);
            }
            if (lastModified != null) {
                lastModifiedDate = lastModified.getTimeInMillis();
            }
        }
        if (extension.equalsIgnoreCase("tif") || extension.equalsIgnoreCase("tiff")) {
            extension = DEFAULT_EXTENSION;
        }
        ResourceResolver resourceResolver = request.getResourceResolver();
        ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
        ContentPolicy contentPolicy = policyManager.getPolicy(resource);
        if (contentPolicy != null) {
            disableLazyLoading = contentPolicy.getProperties().get(PN_DESIGN_LAZY_LOADING_ENABLED, false);
        }
        Set<Integer> supportedRenditionWidths = getSupportedRenditionWidths(contentPolicy);
        smartImages = new String[supportedRenditionWidths.size()];
        smartSizes = new int[supportedRenditionWidths.size()];
        int index = 0;
        String escapedResourcePath = Text.escapePath(resource.getPath());
        for (Integer width : supportedRenditionWidths) {
            smartImages[index] = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + width + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
            smartSizes[index] = width;
            index++;
        }
        if (smartSizes.length == 0 || smartSizes.length >= 2) {
            src = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
        } else if (smartSizes.length == 1) {
            src = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + smartSizes[0] + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
        }
        if (!isDecorative) {
            Page page = pageManager.getPage(linkURL);
            if (page != null) {
                String vanityURL = page.getVanityUrl();
                linkURL = (vanityURL == null ? linkURL + ".html" : vanityURL);
            }
        } else {
            linkURL = null;
            alt = null;
        }
        buildJson();
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Calendar(java.util.Calendar) DownloadResource(com.day.cq.commons.DownloadResource) Resource(org.apache.sling.api.resource.Resource) ImageResource(com.day.cq.commons.ImageResource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Page(com.day.cq.wcm.api.Page) ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PostConstruct(javax.annotation.PostConstruct)

Example 10 with ContentPolicyManager

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

the class ContentFragmentUtils method getGridResourceType.

/**
 * Returns an optional grid type configured via {@link ContentPolicy content policy} property
 * ({@value #PN_CFM_GRID_TYPE}) or {@value #DEFAULT_GRID_TYPE} as default.
 *
 * @param fragmentResource the content fragment resource resource to be checked
 * @return the configured grid type of a default
 */
public static String getGridResourceType(Resource fragmentResource) {
    String gridResourceType = DEFAULT_GRID_TYPE;
    if (fragmentResource == null) {
        return gridResourceType;
    }
    ResourceResolver resourceResolver = fragmentResource.getResourceResolver();
    ContentPolicyManager contentPolicyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
    ContentPolicy fragmentContentPolicy = contentPolicyManager != null ? contentPolicyManager.getPolicy(fragmentResource) : null;
    if (fragmentContentPolicy != null) {
        ValueMap contentPolicyProperties = fragmentContentPolicy.getProperties();
        gridResourceType = contentPolicyProperties.get(PN_CFM_GRID_TYPE, DEFAULT_GRID_TYPE);
    }
    return gridResourceType;
}
Also used : ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ValueMap(org.apache.sling.api.resource.ValueMap) 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