Search in sources :

Example 16 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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 17 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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 18 with ContentPolicy

use of com.day.cq.wcm.api.policies.ContentPolicy 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 19 with ContentPolicy

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

the class ImageImplTest method getImageUnderTest.

private Image getImageUnderTest(String resourcePath, WCMMode wcmMode) {
    Resource resource = aemContext.resourceResolver().getResource(resourcePath);
    ContentPolicyMapping mapping = resource.adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    SlingBindings slingBindings = new SlingBindings();
    if (contentPolicy != null) {
        when(contentPolicyManager.getPolicy(resource)).thenReturn(contentPolicy);
    }
    slingBindings.put(SlingBindings.RESOURCE, resource);
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(aemContext.resourceResolver(), aemContext.bundleContext());
    request.setContextPath(CONTEXT_PATH);
    request.setResource(resource);
    Page page = aemContext.pageManager().getPage(PAGE);
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    if (wcmMode != null) {
        request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, wcmMode);
    }
    slingBindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(request));
    slingBindings.put(WCMBindings.PAGE_MANAGER, aemContext.pageManager());
    Style style = mock(Style.class);
    when(style.get(anyString(), (Object) Matchers.anyObject())).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[1]);
    slingBindings.put(WCMBindings.CURRENT_STYLE, style);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return request.adaptTo(Image.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) Resource(org.apache.sling.api.resource.Resource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Style(com.day.cq.wcm.api.designer.Style) Page(com.day.cq.wcm.api.Page) SightlyWCMMode(com.adobe.cq.sightly.SightlyWCMMode)

Example 20 with ContentPolicy

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

the class PageImplTest method getPageUnderTest.

private Page getPageUnderTest(String pagePath, String designPath) {
    Resource resource = aemContext.currentResource(pagePath);
    com.day.cq.wcm.api.Page page = spy(aemContext.currentPage(pagePath));
    SlingBindings slingBindings = (SlingBindings) aemContext.request().getAttribute(SlingBindings.class.getName());
    Design design = mock(Design.class);
    if (designPath != null) {
        when(design.getPath()).thenReturn(designPath);
    } else {
        when(design.getPath()).thenReturn(Designer.DEFAULT_DESIGN_PATH);
    }
    Resource templateResource = aemContext.resourceResolver().getResource("/conf/coretest/settings/wcm/templates/product-page");
    Template template = mock(Template.class);
    when(template.hasStructureSupport()).thenReturn(true);
    when(template.adaptTo(Resource.class)).thenReturn(templateResource);
    when(page.getTemplate()).thenReturn(template);
    ContentPolicyMapping mapping = templateResource.getChild(POLICIES_MAPPING_PATH).adaptTo(ContentPolicyMapping.class);
    ContentPolicy contentPolicy = mapping.getPolicy();
    Style style;
    slingBindings.put(WCMBindings.CURRENT_DESIGN, design);
    if (contentPolicy != null) {
        Resource contentPolicyResource = aemContext.resourceResolver().getResource(contentPolicy.getPath());
        style = new MockStyle(contentPolicyResource, contentPolicyResource.adaptTo(ValueMap.class));
    } else {
        style = mock(Style.class);
        when(style.get(anyString(), Matchers.anyObject())).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[1]);
    }
    slingBindings.put(WCMBindings.CURRENT_STYLE, style);
    slingBindings.put(SlingBindings.RESOLVER, aemContext.request().getResourceResolver());
    slingBindings.put(WCMBindings.CURRENT_PAGE, page);
    slingBindings.put(WCMBindings.PAGE_MANAGER, aemContext.pageManager());
    slingBindings.put(SlingBindings.RESOURCE, resource);
    slingBindings.put(WCMBindings.PAGE_PROPERTIES, page.getProperties());
    MockSlingHttpServletRequest request = aemContext.request();
    request.setContextPath(CONTEXT_PATH);
    request.setResource(resource);
    return request.adaptTo(Page.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) Resource(org.apache.sling.api.resource.Resource) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) Template(com.day.cq.wcm.api.Template) Design(com.day.cq.wcm.api.designer.Design) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ContentPolicyMapping(com.day.cq.wcm.api.policies.ContentPolicyMapping) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle) Style(com.day.cq.wcm.api.designer.Style) MockStyle(com.adobe.cq.wcm.core.components.context.MockStyle)

Aggregations

ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)20 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)17 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)17 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)15 Test (org.junit.Test)15 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BufferedImage (java.awt.image.BufferedImage)10 Resource (org.apache.sling.api.resource.Resource)4 ContentPolicyManager (com.day.cq.wcm.api.policies.ContentPolicyManager)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 Page (com.day.cq.wcm.api.Page)2 Style (com.day.cq.wcm.api.designer.Style)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 SightlyWCMMode (com.adobe.cq.sightly.SightlyWCMMode)1 MockStyle (com.adobe.cq.wcm.core.components.context.MockStyle)1 DownloadResource (com.day.cq.commons.DownloadResource)1 ImageResource (com.day.cq.commons.ImageResource)1