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;
}
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;
}
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();
}
}
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);
}
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);
}
Aggregations